Add Site settings: name, description, icons, page titles, indexing

Panel → Site settings (migration 035, superadmins only) edits the
site's name, description, preview image, favicon and home-screen
icon, each static page's title and intro, and whether search engines
may list the site. Every value overrides site.json; a blank one falls
back to it. The indexing switch is per database, so the dev server
can be off while production is on from one build.

The server merges the two (siteFor in pageMeta.js) for page metadata,
the sitemap and a new /robots.txt route, and writes the merged
wording into each page as window.__NGU_SITE__, which site.ts reads so
headings match with no extra request. Entities can now require a
higher role to save (writeRole), checked on both sides. Panel is a
menu group with Accounts and server, and Site settings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Zaldimmar 2026-09-28 00:36:58 -05:00
parent 8ec34ea185
commit 47df7b1fe0
15 changed files with 393 additions and 32 deletions

View file

@ -13,9 +13,13 @@
── Where it comes from ──
/site.json (SITE_CONFIG): the site's name, address, default
description and image, and each static page's title and
description. The pages read the same file for their heading and
intro, so the two can't drift.
description and image, icons, and each static page's title and
description. Laid over it: Panel → Site settings (the
site_settings table, 035), where anything set wins and anything
blank falls back to site.json. siteFor(db) does the merge. The
pages read the same merged values (renderPage writes them into
the HTML as window.__NGU_SITE__, see src/lib/site.ts), so a
heading and its search title can't drift.
The database: records' names, taglines, first paragraphs and
images. A blank site description falls back to the front page's
subhead.
@ -63,6 +67,57 @@ export function loadSite() {
const origin = (site) => (site.url ?? "").replace(/\/$/, "");
/* site.json with the admin's Site settings laid over it: a value set
there wins, a blank one falls back. indexing 'on' / 'off' overrides
robots.index for this database only. Page overrides apply only to
pages site.json knows. */
export function siteFor(db) {
const base = loadSite();
const row = db.prepare(`SELECT * FROM site_settings WHERE id = 'site'`).get() ?? {};
const pages = { ...(base.pages ?? {}) };
const overrides = db
.prepare(`SELECT path, title, description FROM site_setting_pages WHERE settings_id = 'site'`)
.all();
for (const page of overrides) {
const known = pages[page.path];
if (!known) continue;
pages[page.path] = {
title: page.title || known.title,
description: page.description || known.description,
};
}
return {
...base,
title: row.site_name || base.title,
description: row.description || base.description,
openGraph: { ...base.openGraph, image: row.preview_image || base.openGraph?.image },
icons: {
...base.icons,
icon: row.favicon || base.icons?.icon,
apple: row.apple_touch_icon || base.icons?.apple,
},
robots: {
...base.robots,
index: row.indexing ? row.indexing === "on" : base.robots?.index !== false,
},
pages,
};
}
/* What the browser app reads for headings and titles. */
const forBrowser = (site) => ({ title: site.title, pages: site.pages });
/* robots.txt for this database's setting, as the build writes it. */
export function robotsTxt(site) {
if (site.robots?.index === false) return "User-agent: *\nDisallow: /\n";
return (
"User-agent: *\nDisallow: /admin\nDisallow: /api\n" +
(site.url ? `\nSitemap: ${origin(site)}/sitemap.xml\n` : "")
);
}
/* ── Helpers ───────────────────────────────────────────────── */
/* Text as a description: the formatting marks content blocks allow
@ -112,6 +167,7 @@ function defaults(site, db, pathname) {
type: "website",
noindex: false,
jsonLd: null,
site,
};
}
@ -277,7 +333,7 @@ function blogMeta(site, db, id, meta) {
/* The metadata for one request path. */
export function metaFor(db, rawPath) {
const site = loadSite();
const site = siteFor(db);
// Canonical form: no trailing slash (but "/" itself), no query.
const pathname = rawPath.split(/[?#]/)[0].replace(/(.)\/+$/, "$1") || "/";
const meta = defaults(site, db, pathname);
@ -352,10 +408,15 @@ const REPLACED = [
/<meta\s+(?:name|property)="(?:description|robots|og:[^"]+|twitter:[^"]+)"[^>]*>\s*/gi,
/<link\s+rel="canonical"[^>]*>\s*/gi,
/<script\s+type="application\/ld\+json"[^>]*>[\s\S]*?<\/script>\s*/gi,
/<link\s+rel="(?:icon|apple-touch-icon)"[^>]*>\s*/gi,
];
/* A value for a <script>: JSON with "<" escaped, so nothing in it
can end the element early. */
const scriptJson = (value) => JSON.stringify(value).replace(/</g, "\\u003c");
export function renderPage(html, meta) {
const site = loadSite();
const site = meta.site;
let out = html;
for (const pattern of REPLACED) out = out.replace(pattern, "");
@ -374,12 +435,17 @@ export function renderPage(html, meta) {
meta.image && `<meta name="twitter:image" content="${escapeHtml(meta.image)}">`,
// "</" can't appear inside the script, or the browser would
// end the element there.
meta.jsonLd &&
`<script type="application/ld+json">${JSON.stringify(meta.jsonLd).replace(/</g, "\\u003c")}</script>`,
site.icons?.icon && `<link rel="icon" href="${escapeHtml(site.icons.icon)}">`,
site.icons?.apple && `<link rel="apple-touch-icon" href="${escapeHtml(site.icons.apple)}">`,
meta.jsonLd && `<script type="application/ld+json">${scriptJson(meta.jsonLd)}</script>`,
// Read by src/lib/site.ts before the app's module runs (a plain
// script in the head runs during parsing; modules wait).
`<script>window.__NGU_SITE__ = ${scriptJson(forBrowser(site))}</script>`,
].filter(Boolean);
// A blocked site (site.json robots.index false) stays blocked on
// every page, whatever the record says.
// A blocked site (Site settings indexing off, or site.json's
// robots.index false) stays blocked on every page, whatever the
// record says.
if (site.robots?.index === false && !meta.noindex) {
tags.splice(2, 0, `<meta name="robots" content="noindex, nofollow">`);
}
@ -390,7 +456,7 @@ export function renderPage(html, meta) {
/* ── Sitemap ───────────────────────────────────────────────── */
export function sitemap(db) {
const site = loadSite();
const site = siteFor(db);
const base = origin(site);
const day = (value) => (value ? String(value).slice(0, 10) : null);