Skip to content

Commit bc4e3e9

Browse files
committed
fix: honor NEXT_PUBLIC_WEB_PORT in website fallback
1 parent b551e6e commit bc4e3e9

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

sdk/src/constants.ts

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,37 @@ export const IS_DEV = process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
44
export const IS_TEST = process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'test'
55
export const IS_PROD = !IS_DEV && !IS_TEST
66

7-
export const WEBSITE_URL =
8-
process.env.NEXT_PUBLIC_CODEBUFF_APP_URL ||
9-
(IS_PROD ? 'https://codebuff.com' : 'http://localhost:3000')
7+
const normalizeLocalWebPort = (env: NodeJS.ProcessEnv): string => {
8+
const port = env.NEXT_PUBLIC_WEB_PORT
9+
if (port && /^\d+$/.test(port)) {
10+
return port
11+
}
12+
return '3000'
13+
}
14+
15+
export const resolveWebsiteUrl = (
16+
env: NodeJS.ProcessEnv = process.env,
17+
): string => {
18+
const explicitUrl = env.NEXT_PUBLIC_CODEBUFF_APP_URL
19+
if (explicitUrl) {
20+
return explicitUrl
21+
}
22+
23+
const envName = env.NEXT_PUBLIC_CB_ENVIRONMENT
24+
const isProdEnv =
25+
envName === undefined
26+
? IS_PROD
27+
: envName !== 'dev' && envName !== 'test'
28+
29+
if (isProdEnv) {
30+
return 'https://codebuff.com'
31+
}
32+
33+
const port = normalizeLocalWebPort(env)
34+
return `http://localhost:${port}`
35+
}
36+
37+
export const WEBSITE_URL = resolveWebsiteUrl()
1038

1139
const DEFAULT_BACKEND_URL = 'manicode-backend.onrender.com'
1240
const DEFAULT_BACKEND_URL_DEV = 'localhost:4242'

0 commit comments

Comments
 (0)