Serve pages even when their metadata can't be built

With no site.json on the server, every page request threw and came
back as the API's JSON 500, so the site didn't load. The page route
now serves index.html as built, with the default tags, and logs the
cause once; the sitemap answers 503. The nginx snippet in docs/seo.md
also falls back to the plain page on a 500, sends the home page
through the API, and gives uploads their own body-size limit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-27 22:15:38 -05:00
parent e932ee3007
commit 84e4e2acae
2 changed files with 73 additions and 12 deletions

View file

@ -49,10 +49,24 @@ with it off it blocks everything.
so the site still loads (with default previews) during a restart: so the site still loads (with default previews) during a restart:
```nginx ```nginx
# Shared by every block that sends a page to the API.
# (Or repeat these lines in each block, as nginx-dev does.)
# proxy_http_version 1.1;
# proxy_set_header Host $host;
# proxy_set_header X-Real-IP $remote_addr;
# proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# proxy_set_header X-Forwarded-Proto $scheme;
location = /sitemap.xml { location = /sitemap.xml {
proxy_pass http://127.0.0.1:3001; proxy_pass http://127.0.0.1:3001;
proxy_set_header Host $host; }
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# The home page needs its own block: try_files would otherwise find
# the site's folder and serve index.html without asking the API.
location = / {
proxy_pass http://127.0.0.1:3001;
proxy_intercept_errors on;
error_page 500 502 503 504 = @static;
} }
location / { location / {
@ -61,19 +75,30 @@ with it off it blocks everything.
location @page { location @page {
proxy_pass http://127.0.0.1:3001; proxy_pass http://127.0.0.1:3001;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_intercept_errors on; proxy_intercept_errors on;
error_page 502 503 504 = @static; error_page 500 502 503 504 = @static;
} }
location @static { location @static {
try_files /index.html =404; try_files /index.html =404;
} }
# Uploads are bigger than the /api/ block's 64k limit allows.
location = /api/admin/media {
proxy_pass http://127.0.0.1:3001;
client_max_body_size 26m;
proxy_read_timeout 60s;
}
``` ```
Keep the existing `/api/` and `/uploads/` blocks; they're more Keep the existing `/api/` and `/uploads/` blocks; they're more
specific and still win. Use the port the API listens on. specific and still win. Use the port the API listens on. A 404
from the API (a record that doesn't exist) goes through as it is;
500 to 504 fall back to the plain page.
A dev or test server should also send `X-Robots-Tag: noindex,
nofollow` and its own blocking `robots.txt`, since it shares the
production build's settings: see `nginx-dev` in the repo root.
3. Check it: `curl -s https://nextgenerationofunity.org/events/<id> | grep og:title` 3. Check it: `curl -s https://nextgenerationofunity.org/events/<id> | grep og:title`
should show the event's name, and Facebook's Sharing Debugger or should show the event's name, and Facebook's Sharing Debugger or

View file

@ -18,6 +18,10 @@
the app's own not-found page with a 404 status, so search engines the app's own not-found page with a 404 status, so search engines
drop it. drop it.
If the metadata can't be built (no site.json at SITE_CONFIG, say),
the page is still served, as built, and the cause is logged once:
a preview is never worth a page that won't load.
Mounted last, after every /api and /uploads route; an unknown Mounted last, after every /api and /uploads route; an unknown
/api path is still the API's JSON 404. /api path is still the API's JSON 404.
═══════════════════════════════════════════════════════════════ */ ═══════════════════════════════════════════════════════════════ */
@ -49,12 +53,28 @@ function indexHtml() {
const pages = new Hono(); const pages = new Hono();
pages.get("/sitemap.xml", (c) => /* Warned once per cause rather than on every request, so a missing
c.body(sitemap(c.get("db")), 200, { site.json doesn't bury the log. */
const warned = new Set();
function warnOnce(key, message) {
if (warned.has(key)) return;
warned.add(key);
console.error(message);
}
pages.get("/sitemap.xml", (c) => {
let xml;
try {
xml = sitemap(c.get("db"));
} catch (err) {
warnOnce(`sitemap:${err.message}`, `pages: can't build the sitemap: ${err.message}`);
return c.text("The sitemap isn't available.", 503);
}
return c.body(xml, 200, {
"Content-Type": "application/xml; charset=utf-8", "Content-Type": "application/xml; charset=utf-8",
"Cache-Control": CACHE, "Cache-Control": CACHE,
}), });
); });
pages.get("*", (c, next) => { pages.get("*", (c, next) => {
const pathname = c.req.path; const pathname = c.req.path;
@ -70,8 +90,24 @@ pages.get("*", (c, next) => {
return c.text("The site hasn't been built.", 503); return c.text("The site hasn't been built.", 503);
} }
const meta = metaFor(c.get("db"), pathname); // The metadata is an extra. If it can't be built (site.json
return c.html(renderPage(html, meta), meta.status, { // missing, a query failing), the page goes out as it was built,
// with the site's default tags, rather than not at all.
let meta;
let page;
try {
meta = metaFor(c.get("db"), pathname);
page = renderPage(html, meta);
} catch (err) {
warnOnce(
`meta:${err.message}`,
`pages: serving ${pathname} without its metadata: ${err.message}` +
(err.code === "ENOENT" ? " (set SITE_CONFIG to the deployed site.json)" : ""),
);
return c.html(html, 200, { "Cache-Control": "no-store" });
}
return c.html(page, meta.status, {
"Cache-Control": meta.status === 200 ? CACHE : "no-store", "Cache-Control": meta.status === 200 ? CACHE : "no-store",
}); });
}); });