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:
parent
e932ee3007
commit
84e4e2acae
2 changed files with 73 additions and 12 deletions
|
|
@ -18,6 +18,10 @@
|
|||
the app's own not-found page with a 404 status, so search engines
|
||||
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
|
||||
/api path is still the API's JSON 404.
|
||||
═══════════════════════════════════════════════════════════════ */
|
||||
|
|
@ -49,12 +53,28 @@ function indexHtml() {
|
|||
|
||||
const pages = new Hono();
|
||||
|
||||
pages.get("/sitemap.xml", (c) =>
|
||||
c.body(sitemap(c.get("db")), 200, {
|
||||
/* Warned once per cause rather than on every request, so a missing
|
||||
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",
|
||||
"Cache-Control": CACHE,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
pages.get("*", (c, next) => {
|
||||
const pathname = c.req.path;
|
||||
|
|
@ -70,8 +90,24 @@ pages.get("*", (c, next) => {
|
|||
return c.text("The site hasn't been built.", 503);
|
||||
}
|
||||
|
||||
const meta = metaFor(c.get("db"), pathname);
|
||||
return c.html(renderPage(html, meta), meta.status, {
|
||||
// The metadata is an extra. If it can't be built (site.json
|
||||
// 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",
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue