Skip to content

Commit 121e40a

Browse files
committed
build(vercel): Add script that should run before vercel starts the app
1 parent f0d23d3 commit 121e40a

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"dev:debug": "NODE_OPTIONS='--inspect' next dev",
1111
"build": "next build",
1212
"start": "next start",
13+
"start:vercel": "node vercel.prestart.js && next start",
1314
"tsc": "tsc --pretty --noEmit",
1415
"lint": "eslint lint --max-warnings=0",
1516
"format": "eslint --fix --max-warnings=0",

vercel.prestart.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const fs = require('fs');
2+
3+
const ENV = process.env.VERCEL_ENV;
4+
const URL = process.env.VERCEL_URL;
5+
const REPO_SLUG = process.env.VERCEL_GIT_REPO_SLUG;
6+
const REPO_OWNER = process.env.VERCEL_GIT_REPO_OWNER;
7+
const COMMIT_REF = process.env.VERCEL_GIT_COMMIT_REF;
8+
9+
const domain = 'vercel.app';
10+
11+
const url = () => {
12+
if (ENV === 'production' && REPO_SLUG) {
13+
return `${REPO_SLUG}.${domain}`;
14+
}
15+
16+
if (COMMIT_REF !== 'master' && COMMIT_REF !== 'main' && REPO_SLUG && COMMIT_REF && REPO_OWNER) {
17+
return `${REPO_SLUG}-git-${COMMIT_REF}-${REPO_OWNER}.${domain}`;
18+
}
19+
20+
return URL ?? '';
21+
};
22+
23+
fs.appendFileSync('.env', `\nVERCEL_URL="${url()}"\n`);

0 commit comments

Comments
 (0)