fix(argus): try dir-index before SPA fallback for extensionless paths

Two-tier fallback for extensionless paths that miss in Sia. The
previous change went straight to root index.html, which meant a real
sub-site living at "<name>/about/index.html" got shadowed by the SPA
root — visitors requesting "/about" got served the app instead of
the landing page.

New chain (matches nginx try_files order):
  1) request path              → /about
     hit: bns/potidaea/about   → 404
  2) directory index attempt   → /about/
     hit: bns/potidaea/about/index.html → 200 · SERVE
  3) if still 404, SPA root    → bns/potidaea/index.html → 200

Trailing-slash requests still short-circuit to their own index.html
via the existing `rel` derivation (unchanged), so /about/ has been
working the whole time; only the bare /about was falling through
to the SPA rescue.

Deployed to silentmode 2026-09-16:
  scp Argus/src/gateway/public-gateway.mjs
      silentmode:/tmp/public-gateway.mjs
  sudo cp … /opt/bns-gateway/public-gateway.mjs
  sudo systemctl restart bns-gateway
This commit is contained in:
Local Dev 2026-09-16 19:24:29 +02:00
parent 3e6eb1d392
commit c68eaf53c2

View file

@ -235,15 +235,28 @@ async function serve(res, name, reqPath, baseHref) {
: reqPath.slice(1); : reqPath.slice(1);
const key = base.endsWith("/") ? base + rel : base; const key = base.endsWith("/") ? base + rel : base;
let up = await aws.fetch(`${S3_ENDPOINT}/${key}`); let up = await aws.fetch(`${S3_ENDPOINT}/${key}`);
// SPA fallback — if the extension-less path 404s, serve the domain's // Fallback chain for missing extensionless paths (order matters):
// own index.html so client-side routers (History API) can pick up // 1) directory-index: try "<path>/index.html" — matches nginx
// /katalogos, /anthemus, /artemision, etc. under any BNS mount. // try_files. This handles static sub-sites like /about that
// We only fall back when the requested path has no file extension — // have their own index.html.
// requests for missing images / json / js still 404 as they should. // 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 !== "/") { if (up.status === 404 && !/\.[a-z0-9]+$/i.test(reqPath) && reqPath !== "/") {
const fallbackKey = (base.endsWith("/") ? base : base + "/") + "index.html"; // Try directory-index first
const fb = await aws.fetch(`${S3_ENDPOINT}/${fallbackKey}`); if (!reqPath.endsWith("/")) {
if (fb.ok) { up = fb; } 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()); const buf = Buffer.from(await up.arrayBuffer());
let ct = up.headers.get("content-type"); if (!ct || ct === "application/octet-stream") ct = guess(key); let ct = up.headers.get("content-type"); if (!ct || ct === "application/octet-stream") ct = guess(key);