diff --git a/src/gateway/public-gateway.mjs b/src/gateway/public-gateway.mjs index a5627e6..4bf0dca 100644 --- a/src/gateway/public-gateway.mjs +++ b/src/gateway/public-gateway.mjs @@ -235,15 +235,28 @@ async function serve(res, name, reqPath, baseHref) { : reqPath.slice(1); const key = base.endsWith("/") ? base + rel : base; let up = await aws.fetch(`${S3_ENDPOINT}/${key}`); - // SPA fallback — if the extension-less path 404s, serve the domain's - // own index.html so client-side routers (History API) can pick up - // /katalogos, /anthemus, /artemision, etc. under any BNS mount. - // We only fall back when the requested path has no file extension — - // requests for missing images / json / js still 404 as they should. + // Fallback chain for missing extensionless paths (order matters): + // 1) directory-index: try "/index.html" — matches nginx + // try_files. This handles static sub-sites like /about that + // have their own index.html. + // 2) SPA fallback: serve the domain's ROOT index.html so + // client-side routers can pick up /katalogos, /anthemus, etc. + // Requests with a file extension (missing .png / .js / .json) are + // NOT rescued — those should never be routed by a client-side + // router and swallowing them would hide real bugs. if (up.status === 404 && !/\.[a-z0-9]+$/i.test(reqPath) && reqPath !== "/") { - const fallbackKey = (base.endsWith("/") ? base : base + "/") + "index.html"; - const fb = await aws.fetch(`${S3_ENDPOINT}/${fallbackKey}`); - if (fb.ok) { up = fb; } + // Try directory-index first + if (!reqPath.endsWith("/")) { + const dirKey = (base.endsWith("/") ? base : base + "/") + reqPath.slice(1).replace(/\/+$/, '') + "/index.html"; + const dir = await aws.fetch(`${S3_ENDPOINT}/${dirKey}`); + if (dir.ok) { up = dir; } + } + // Then SPA root fallback + if (up.status === 404) { + const fallbackKey = (base.endsWith("/") ? base : base + "/") + "index.html"; + const fb = await aws.fetch(`${S3_ENDPOINT}/${fallbackKey}`); + if (fb.ok) { up = fb; } + } } const buf = Buffer.from(await up.arrayBuffer()); let ct = up.headers.get("content-type"); if (!ct || ct === "application/octet-stream") ct = guess(key);