ws4kp-linhanced/server/scripts/modules/utils/base-path.mjs
mrkmntal ea8c3bf602
Some checks are pending
build-docker / Build Image (push) Waiting to run
Makes the webpack build use relative paths to allow for deploying on web subdirectories
2026-04-08 21:31:56 -04:00

25 lines
752 B
JavaScript

const normalizeBasePath = (pathname) => {
if (!pathname) return '/';
if (pathname.endsWith('/index.html')) {
const trimmed = pathname.slice(0, -'index.html'.length);
return trimmed.endsWith('/') ? trimmed : `${trimmed}/`;
}
if (pathname.endsWith('/')) return pathname;
const lastSlash = pathname.lastIndexOf('/');
if (lastSlash === -1) return '/';
return `${pathname.slice(0, lastSlash + 1)}`;
};
const getBasePath = () => normalizeBasePath(window.location.pathname);
const withBasePath = (relativePath = '') => {
const sanitizedPath = relativePath.replace(/^\/+/, '');
const basePath = getBasePath();
if (basePath === '/') return `/${sanitizedPath}`;
return `${basePath}${sanitizedPath}`;
};
export {
getBasePath,
withBasePath,
};