fix: use NextJS basePath and WEB_PREFIX to support custom prefix (#19497)
Co-authored-by: johnny0120 <15564476+johnny0120@users.noreply.github.com>
This commit is contained in:
@@ -3,29 +3,44 @@ import { AgentStrategy } from '@/types/app'
|
||||
import { PromptRole } from '@/models/debug'
|
||||
|
||||
export let apiPrefix = ''
|
||||
export let webPrefix = ''
|
||||
export let publicApiPrefix = ''
|
||||
export let publicWebPrefix = ''
|
||||
export let marketplaceApiPrefix = ''
|
||||
export let marketplaceUrlPrefix = ''
|
||||
|
||||
// NEXT_PUBLIC_API_PREFIX=/console/api NEXT_PUBLIC_PUBLIC_API_PREFIX=/api npm run start
|
||||
if (process.env.NEXT_PUBLIC_API_PREFIX && process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX) {
|
||||
if (
|
||||
process.env.NEXT_PUBLIC_API_PREFIX
|
||||
&& process.env.NEXT_PUBLIC_WEB_PREFIX
|
||||
&& process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX
|
||||
&& process.env.NEXT_PUBLIC_PUBLIC_WEB_PREFIX
|
||||
) {
|
||||
apiPrefix = process.env.NEXT_PUBLIC_API_PREFIX
|
||||
webPrefix = process.env.NEXT_PUBLIC_WEB_PREFIX
|
||||
publicApiPrefix = process.env.NEXT_PUBLIC_PUBLIC_API_PREFIX
|
||||
publicWebPrefix = process.env.NEXT_PUBLIC_PUBLIC_WEB_PREFIX
|
||||
}
|
||||
else if (
|
||||
globalThis.document?.body?.getAttribute('data-api-prefix')
|
||||
&& globalThis.document?.body?.getAttribute('data-web-prefix')
|
||||
&& globalThis.document?.body?.getAttribute('data-pubic-api-prefix')
|
||||
&& globalThis.document?.body?.getAttribute('data-pubic-web-prefix')
|
||||
) {
|
||||
// Not build can not get env from process.env.NEXT_PUBLIC_ in browser https://nextjs.org/docs/basic-features/environment-variables#exposing-environment-variables-to-the-browser
|
||||
apiPrefix = globalThis.document.body.getAttribute('data-api-prefix') as string
|
||||
webPrefix = globalThis.document.body.getAttribute('data-web-prefix') as string
|
||||
publicApiPrefix = globalThis.document.body.getAttribute('data-pubic-api-prefix') as string
|
||||
publicWebPrefix = globalThis.document.body.getAttribute('data-pubic-web-prefix') as string
|
||||
}
|
||||
else {
|
||||
// const domainParts = globalThis.location?.host?.split('.');
|
||||
// in production env, the host is dify.app . In other env, the host is [dev].dify.app
|
||||
// const env = domainParts.length === 2 ? 'ai' : domainParts?.[0];
|
||||
apiPrefix = 'http://localhost:5001/console/api'
|
||||
webPrefix = 'http://localhost:3000'
|
||||
publicApiPrefix = 'http://localhost:5001/api' // avoid browser private mode api cross origin
|
||||
publicWebPrefix = 'http://localhost:3000'
|
||||
marketplaceApiPrefix = 'http://localhost:5002/api'
|
||||
}
|
||||
|
||||
@@ -39,7 +54,9 @@ else {
|
||||
}
|
||||
|
||||
export const API_PREFIX: string = apiPrefix
|
||||
export const WEB_PREFIX: string = webPrefix
|
||||
export const PUBLIC_API_PREFIX: string = publicApiPrefix
|
||||
export const PUBLIC_WEB_PREFIX: string = publicWebPrefix
|
||||
export const MARKETPLACE_API_PREFIX: string = marketplaceApiPrefix
|
||||
export const MARKETPLACE_URL_PREFIX: string = marketplaceUrlPrefix
|
||||
|
||||
|
Reference in New Issue
Block a user