Three sections: Mission, Vision & Values from the national organization record's own description and content blocks; Leadership as that record's teams with their people; Details & Contact with NGU's address, nonprofit status, email and the record's contact links. /leadership answers with a 301 to /organization (MOVED in routes/pages.js), and a page title or intro saved for it in Site settings moves with it (migration 054). OrganizationDetail's team list and contact buttons are now shared components (OrgTeams, OrgContact), and NGU's address and email live once in src/data/organizationDetails.ts for the footer, the page and ComingSoon. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
146 lines
5.2 KiB
JavaScript
146 lines
5.2 KiB
JavaScript
/* ═══════════════════════════════════════════════════════════════
|
|
PAGE ROUTES: the site's HTML, with each page's metadata
|
|
|
|
GET /sitemap.xml every public, listed page
|
|
GET /robots.txt this database's indexing setting (Site
|
|
settings, else site.json), so a dev server
|
|
can say no while production says yes
|
|
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.
|
|
|
|
A page that moved answers with a 301 to where it is now (MOVED),
|
|
so old links, QR codes and search results keep working.
|
|
|
|
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.
|
|
═══════════════════════════════════════════════════════════════ */
|
|
|
|
import { readFileSync, statSync } from "node:fs";
|
|
import path from "node:path";
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { Hono } from "hono";
|
|
|
|
import { metaFor, renderPage, robotsTxt, siteFor, sitemap } from "../pageMeta.js";
|
|
import { CACHE, NO_STORE } from "../shape.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"),
|
|
);
|
|
|
|
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();
|
|
|
|
/* 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("/robots.txt", (c) => {
|
|
let text;
|
|
try {
|
|
text = robotsTxt(siteFor(c.get("db")));
|
|
} catch (err) {
|
|
warnOnce(`robots:${err.message}`, `pages: can't build robots.txt: ${err.message}`);
|
|
return c.text("robots.txt isn't available.", 503);
|
|
}
|
|
return c.text(text, 200, { "Cache-Control": CACHE });
|
|
});
|
|
|
|
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 that have moved: old path → new, query string kept. */
|
|
const MOVED = {
|
|
"/leadership": "/organization",
|
|
};
|
|
|
|
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();
|
|
|
|
const moved = MOVED[pathname.replace(/(.)\/+$/, "$1")];
|
|
if (moved) {
|
|
const query = new URL(c.req.url).search;
|
|
return c.redirect(moved + query, 301);
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
// 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, NO_STORE);
|
|
}
|
|
|
|
// no-cache: a browser may keep the page but asks again each time.
|
|
// HTML names the build's hashed /assets files, and after a deploy
|
|
// those are gone, so a cached old page would load nothing. Pages
|
|
// are rebuilt in well under a millisecond.
|
|
return c.html(page, meta.status, {
|
|
"Cache-Control": meta.status === 200 ? "no-cache" : "no-store",
|
|
});
|
|
});
|
|
|
|
export default pages;
|