Fix client base path handling via explicit meta

This commit is contained in:
2026-02-05 16:42:31 +01:00
parent a6265e8656
commit f381c945a7
4 changed files with 12 additions and 9 deletions

View File

@@ -10,7 +10,7 @@
<link href="https://fonts.googleapis.com/css2?family=Noto+Color+Emoji&display=swap" rel="stylesheet">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="styles.css">
<!-- Optional: set data-app-base if served from a subfolder (e.g., /vote) -->
<!-- Set to "/picknplay" in production; leave blank for localhost/root -->
<meta name="app-base" content="">
</head>
<body class="page">

View File

@@ -1,13 +1,15 @@
const defaultHeaders = { "Content-Type": "application/json" };
const metaBase = document.querySelector('meta[name="app-base"]')?.content || "";
const autoBase = (() => {
const parts = window.location.pathname.split("/").filter(Boolean);
return parts.length ? `/${parts[0]}` : "";
})();
const basePath = metaBase || autoBase;
const rawBase = document.querySelector('meta[name="app-base"]')?.content || "";
const basePath = normalizeBase(rawBase);
const withBase = (path) => `${basePath}${path}`;
function normalizeBase(value) {
if (!value) return "";
if (!value.startsWith("/")) return `/${value}`;
return value.endsWith("/") ? value.slice(0, -1) : value;
}
async function request(path, { method = "GET", body } = {}) {
const res = await fetch(withBase(path), {
method,