From 5ecd48ad1530ca5756826fec67c62a53cb1a4fbb Mon Sep 17 00:00:00 2001 From: chenos Date: Fri, 10 Jan 2025 10:13:44 +0800 Subject: [PATCH] fix: ensure URL paths start with public path and redirect public path without trailing slash --- packages/core/app/client/.umirc.ts | 2 +- packages/core/app/client/public/browser-checker.js | 7 +++++-- packages/core/cli/nocobase.conf.tpl | 2 ++ packages/core/cli/src/commands/create-nginx-conf.js | 11 +++++++++-- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/packages/core/app/client/.umirc.ts b/packages/core/app/client/.umirc.ts index cf0eb89dd8..5bdd20fff8 100644 --- a/packages/core/app/client/.umirc.ts +++ b/packages/core/app/client/.umirc.ts @@ -36,7 +36,7 @@ export default defineConfig({ `, }, { - src: `${appPublicPath}browser-checker.js`, + src: `${appPublicPath}browser-checker.js?v=1`, }, ], cacheDirectoryPath: process.env.APP_CLIENT_CACHE_DIR || `node_modules/.cache`, diff --git a/packages/core/app/client/public/browser-checker.js b/packages/core/app/client/public/browser-checker.js index 6a14d0d6e5..39ed61e6c4 100644 --- a/packages/core/app/client/public/browser-checker.js +++ b/packages/core/app/client/public/browser-checker.js @@ -1,9 +1,12 @@ const basename = window['__nocobase_public_path__'] || '/'; let currentPath = window.location.pathname; -if (!currentPath.startsWith(basename)) { +if (currentPath === basename.slice(0, -1)) { + const newUrl = `${window.location.origin}${basename}${window.location.search}${window.location.hash}`; + window.location.replace(newUrl); +} else if (!currentPath.startsWith(basename)) { let newPath = basename + (currentPath.startsWith('/') ? currentPath.slice(1) : currentPath); let newUrl = window.location.origin + newPath + window.location.search + window.location.hash; - window.location.href = newUrl; + window.location.replace(newUrl); } showLog = true; function log(m) { diff --git a/packages/core/cli/nocobase.conf.tpl b/packages/core/cli/nocobase.conf.tpl index edfce983e9..e0735adfd7 100644 --- a/packages/core/cli/nocobase.conf.tpl +++ b/packages/core/cli/nocobase.conf.tpl @@ -65,6 +65,8 @@ server { } } + {{otherLocation}} + location ^~ {{publicPath}}api/ { proxy_pass http://127.0.0.1:{{apiPort}}; proxy_http_version 1.1; diff --git a/packages/core/cli/src/commands/create-nginx-conf.js b/packages/core/cli/src/commands/create-nginx-conf.js index 65f065b2d4..50bade8d9e 100644 --- a/packages/core/cli/src/commands/create-nginx-conf.js +++ b/packages/core/cli/src/commands/create-nginx-conf.js @@ -19,11 +19,18 @@ module.exports = (cli) => { cli.command('create-nginx-conf').action(async (name, options) => { const file = resolve(__dirname, '../../nocobase.conf.tpl'); const data = readFileSync(file, 'utf-8'); + let otherLocation = ''; + if (process.env.APP_PUBLIC_PATH !== '/') { + otherLocation = `location / { + alias {{cwd}}/node_modules/@nocobase/app/dist/client/; + try_files $uri $uri/ /index.html; + }`; + } const replaced = data .replace(/\{\{cwd\}\}/g, '/app/nocobase') .replace(/\{\{publicPath\}\}/g, process.env.APP_PUBLIC_PATH) - .replace(/\{\{apiPort\}\}/g, process.env.APP_PORT); - + .replace(/\{\{apiPort\}\}/g, process.env.APP_PORT) + .replace(/\{\{otherLocation\}\}/g, otherLocation); const targetFile = resolve(process.cwd(), 'storage', 'nocobase.conf'); writeFileSync(targetFile, replaced); });