fix: support authenticated web deployments under a reverse proxy path
This commit is contained in:
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user