fix: ensure URL paths start with public path and redirect public path without trailing slash

This commit is contained in:
chenos 2025-01-10 10:13:44 +08:00
parent fa60e339b4
commit 5ecd48ad15
4 changed files with 17 additions and 5 deletions

View File

@ -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`,

View File

@ -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) {

View File

@ -65,6 +65,8 @@ server {
}
}
{{otherLocation}}
location ^~ {{publicPath}}api/ {
proxy_pass http://127.0.0.1:{{apiPort}};
proxy_http_version 1.1;

View File

@ -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);
});