fix: support authenticated web deployments under a reverse proxy path

This commit is contained in:
AttilaNet
2026-11-10 20:01:44 +08:00
parent 0230a933b0
commit 99aff423d8
12 changed files with 529 additions and 31 deletions
+13
View File
@@ -0,0 +1,13 @@
// The server supplies this value; never infer it from the URL or proxy headers.
const basePath = (document.querySelector('meta[name="ranch-web-base-path"]')?.content || '').replace(/\/+$/, '');
export const webPath = path => `${basePath}${path}`;
export function apiPath(path) {
const url = new URL(path, location.origin);
if (url.origin !== location.origin || url.username || url.password) throw new TypeError('Invalid API URL');
let pathname = url.pathname;
if (basePath && pathname.startsWith(`${basePath}/`)) pathname = pathname.slice(basePath.length);
if (!pathname.startsWith('/api/')) throw new TypeError('Invalid API path');
return webPath(pathname) + url.search;
}