Page requests now go through the API (routes/pages.js), which fills the built index.html in for that path before the app loads: title, description, preview image, canonical URL, and structured data (Organization, Event, BlogPosting). Shared links preview the page itself. Unlisted records are noindex; missing ones return 404. /sitemap.xml lists every public, listed page. site.json is the one place for the site's name, address, default description and preview image, and each static page's title and intro, which the pages now read too. Indexing is switched on: robots.txt allows all but /admin and /api and names the sitemap. The browser tab title follows the page. nginx setup, with a fallback to the plain page if the API is down, is in docs/seo.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
79 lines
2.9 KiB
JavaScript
79 lines
2.9 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
PAGE ROUTES: the site's HTML, with each page's metadata
|
|
|
|
GET /sitemap.xml every public, listed page
|
|
GET /anything the built index.html, its title, description,
|
|
preview image, canonical link and structured
|
|
data filled in for that path (pageMeta.js)
|
|
|
|
In production nginx serves files that exist (the JS, CSS,
|
|
uploads) itself and sends everything else here, so a shared link
|
|
carries the right preview; see docs/seo.md for the block. The app
|
|
in the page then takes over as usual. In development Vite serves
|
|
index.html itself and this is never reached.
|
|
|
|
SITE_INDEX is the built index.html (default: dist/ at the repo
|
|
root). It's reread when the file changes, so a deploy needs no
|
|
restart for the page to update. A record that doesn't exist gets
|
|
the app's own not-found page with a 404 status, so search engines
|
|
drop it.
|
|
|
|
Mounted last, after every /api and /uploads route; an unknown
|
|
/api path is still the API's JSON 404.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { readFileSync, statSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { Hono } from "hono";
|
|
|
|
import { metaFor, renderPage, sitemap } from "../pageMeta.js";
|
|
|
|
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
|
|
export const SITE_INDEX = path.resolve(
|
|
process.env.SITE_INDEX ?? path.join(here, "../../../dist/index.html"),
|
|
);
|
|
|
|
const CACHE = "public, max-age=60, stale-while-revalidate=300";
|
|
|
|
let cached = { mtime: 0, html: null };
|
|
|
|
function indexHtml() {
|
|
const mtime = statSync(SITE_INDEX).mtimeMs;
|
|
if (cached.html && cached.mtime === mtime) return cached.html;
|
|
cached = { mtime, html: readFileSync(SITE_INDEX, "utf8") };
|
|
return cached.html;
|
|
}
|
|
|
|
const pages = new Hono();
|
|
|
|
pages.get("/sitemap.xml", (c) =>
|
|
c.body(sitemap(c.get("db")), 200, {
|
|
"Content-Type": "application/xml; charset=utf-8",
|
|
"Cache-Control": CACHE,
|
|
}),
|
|
);
|
|
|
|
pages.get("*", (c, next) => {
|
|
const pathname = c.req.path;
|
|
// The API's own paths, and an upload that isn't there, stay plain
|
|
// 404s rather than becoming a page.
|
|
if (/^\/(api|uploads)(\/|$)/.test(pathname)) return next();
|
|
|
|
let html;
|
|
try {
|
|
html = indexHtml();
|
|
} catch (err) {
|
|
console.error(`pages: can't read ${SITE_INDEX}: ${err.message}`);
|
|
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, {
|
|
"Cache-Control": meta.status === 200 ? CACHE : "no-store",
|
|
});
|
|
});
|
|
|
|
export default pages;
|