domma-cms 1.60.0 → 1.61.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. package/CHANGELOG.md +28 -0
  2. package/CLAUDE.md +1 -1
  3. package/admin/index.html +7 -1
  4. package/admin/js/app.js +2 -2
  5. package/admin/js/lib/ai-assist.js +1 -1
  6. package/admin/js/templates/ai-assist-builders.html +29 -0
  7. package/admin/js/templates/docs/usage-chat.html +5 -2
  8. package/admin/js/views/ai-assist-builders.js +1 -0
  9. package/admin/js/views/ai-assist-settings.js +1 -1
  10. package/admin/js/views/index.js +1 -1
  11. package/admin/js/views/login.js +1 -1
  12. package/docs/DommaFeatures.md +4 -2
  13. package/docs/cache.md +24 -0
  14. package/docs/configuration.md +1 -1
  15. package/docs/plugin-development.md +26 -0
  16. package/package.json +1 -1
  17. package/public/js/effects.js +1 -1
  18. package/public/js/site.js +1 -1
  19. package/server/config.js +37 -0
  20. package/server/routes/api/admin-assistant.js +11 -3
  21. package/server/routes/api/auth.js +4 -1
  22. package/server/routes/api/chats.js +3 -1
  23. package/server/routes/api/components.js +2 -1
  24. package/server/routes/api/content-api.js +1 -1
  25. package/server/routes/api/content-public.js +2 -2
  26. package/server/routes/api/content-writes.js +1 -1
  27. package/server/routes/api/effects.js +4 -9
  28. package/server/routes/api/layouts.js +1 -1
  29. package/server/routes/api/pages.js +6 -5
  30. package/server/routes/api/search.js +3 -3
  31. package/server/routes/api/sidebar-counts.js +2 -0
  32. package/server/routes/api/theme.js +4 -7
  33. package/server/routes/api/views.js +2 -1
  34. package/server/routes/public.js +4 -4
  35. package/server/server.js +14 -7
  36. package/server/services/aiAssist.js +67 -0
  37. package/server/services/cache/adminChange.js +28 -0
  38. package/server/services/chat/ai.js +1 -1
  39. package/server/services/content.js +73 -10
  40. package/server/services/effectsSettings.js +25 -0
  41. package/server/services/health.js +2 -2
  42. package/server/services/hooks.js +1 -1
  43. package/server/services/htmlResponse.js +33 -4
  44. package/server/services/listTags.js +1 -1
  45. package/server/services/markdown.js +56 -22
  46. package/server/services/pluginGuides.js +2 -2
  47. package/server/services/plugins.js +34 -3
  48. package/server/services/projects.js +39 -4
  49. package/server/services/publicAssets.js +488 -0
  50. package/server/services/renderer.js +10 -8
  51. package/server/services/seo/index.js +3 -3
  52. package/server/services/sitemap.js +1 -1
  53. package/server/services/themeSettings.js +11 -0
  54. package/server/services/versions.js +2 -2
  55. package/server/services/wpImport/index.js +2 -2
  56. package/server/templates/page.html +3 -2
@@ -0,0 +1,488 @@
1
+ /**
2
+ * Long-lived browser caching for what a public page links: its stylesheets,
3
+ * its scripts, and the site's custom CSS.
4
+ *
5
+ * @fastify/static serves every file `Cache-Control: public, max-age=0`, so a
6
+ * visitor going from one page to the next asked the server about every asset
7
+ * again - some twenty-five conditional requests a page, each answered 304.
8
+ * Nothing was downloaded twice, but every one of them is a round trip before
9
+ * the page can finish.
10
+ *
11
+ * Here a rendered page's local stylesheet and script URLs gain a fingerprint,
12
+ * `_pv=<hash of the file>`. A request whose `_pv` matches the file as it is
13
+ * NOW is answered
14
+ * `Cache-Control: public, max-age=31536000, immutable`, so the browser never
15
+ * asks again; change the file and the next page drawn links a new URL.
16
+ *
17
+ * It cannot serve the wrong thing. The file is still served by @fastify/static
18
+ * (and services/assetCache.js) exactly as before - this only decides the
19
+ * cache-control. A `_pv` that does not match - a page kept in the response
20
+ * cache or in a browser tab from before the file changed - gets the current
21
+ * file with the usual `max-age=0`, never a 404 and never a long cache under
22
+ * the wrong name.
23
+ *
24
+ * Modules are different: a module is identified by its URL, and site.js
25
+ * imports forms.js by the same `?v=` URL the page links it with - a
26
+ * fingerprint on one and not the other would run the module twice. So a
27
+ * `<script type="module">` tag whose URL something also imports is never
28
+ * rewritten (one that nothing imports is, and is mapped too). The page gets an
29
+ * import map: every module its entry scripts import, however deep, mapped
30
+ * from the URL it is imported by to that URL with its fingerprint. The
31
+ * browser applies the map to every import of that URL, so all importers still
32
+ * share one instance, and the thirty-odd modules behind site.js stop being
33
+ * asked about on every page. The entry tags' own URLs are left out of the map
34
+ * (so a module that is both linked and imported stays one), a browser too old
35
+ * for import maps ignores it and loads everything as before, and a page that
36
+ * brings its own import map is left alone.
37
+ *
38
+ * Left alone on purpose:
39
+ * - Images. A page's scripts draw images of their own from data (a collection
40
+ * browser's cards, the navbar's logo) by the plain URL; the same picture
41
+ * under two URLs is downloaded twice, which costs a first visit far more
42
+ * than a 304 costs a second.
43
+ * - Anything that is not a .css, .js or .mjs file under public/, Domma's dist
44
+ * or a plugin's public/ folder.
45
+ *
46
+ * The site's custom CSS (content/custom.css) used to be written into every
47
+ * page inside a <style> tag. Past a few kilobytes that is most of the page -
48
+ * sent again with every page view, and held once per page in the response
49
+ * cache. customCssTag() links it as `/content/custom.css` instead, served
50
+ * here with the same fingerprint; a small one stays inline, where it costs
51
+ * less than a request.
52
+ */
53
+ import fs from 'node:fs/promises';
54
+ import path from 'node:path';
55
+ import {createHash} from 'node:crypto';
56
+ import {encode, pickEncoding} from './htmlResponse.js';
57
+
58
+ export const IMMUTABLE = 'public, max-age=31536000, immutable';
59
+ export const VERSION_PARAM = '_pv';
60
+ /** The URL the site's custom CSS is served at once it is too big to inline. */
61
+ export const CUSTOM_CSS_URL = '/content/custom.css';
62
+ /** Custom CSS up to this many bytes stays inline: a request costs more than it saves. */
63
+ export const CUSTOM_CSS_INLINE_MAX = 4096;
64
+
65
+ const TEXT_TYPES = new Set(['.css', '.js', '.mjs']);
66
+
67
+ /** @type {{root: string, dommaDist: string, pluginsDir: string, customCss: string}|null} */
68
+ let _roots = null;
69
+ /** @type {Map<string, {signature: string, version: string, specs?: string[]}>} file -> its version (and, for a script, what it imports), while the file is unchanged */
70
+ const _versions = new Map();
71
+ const VERSIONS_MAX = 5000;
72
+ /** @type {{signature: string, body: Buffer, version: string, encoded: Map<string, Promise<Buffer>>}|null} */
73
+ let _customCss = null;
74
+
75
+ /**
76
+ * Where the files are. Until this is called nothing is fingerprinted and
77
+ * custom CSS stays inline - a render outside a running server (a test, a
78
+ * script) is unchanged.
79
+ *
80
+ * @param {{root: string, dommaDist: string, pluginsDir?: string}|null} roots
81
+ */
82
+ export function configurePublicAssets(roots) {
83
+ _versions.clear();
84
+ _customCss = null;
85
+ if (!roots) { _roots = null; return; }
86
+ _roots = {
87
+ root: path.resolve(roots.root),
88
+ dommaDist: path.resolve(roots.dommaDist),
89
+ pluginsDir: path.resolve(roots.pluginsDir || path.join(roots.root, 'plugins')),
90
+ customCss: path.resolve(roots.root, 'content', 'custom.css')
91
+ };
92
+ }
93
+
94
+ export function publicAssetsConfigured() {
95
+ return _roots !== null;
96
+ }
97
+
98
+ /**
99
+ * The file a public URL path is served from, or null when it is not one this
100
+ * module fingerprints. Mirrors the static mounts in server.js.
101
+ *
102
+ * @param {string} pathname - decoded, no query
103
+ * @returns {{file: string}|null}
104
+ */
105
+ export function resolvePublicAsset(pathname) {
106
+ if (!_roots || typeof pathname !== 'string' || !pathname.startsWith('/')) return null;
107
+ if (pathname.includes('\\') || pathname.includes('\0') || pathname.split('/').includes('..')) return null;
108
+ if (!TEXT_TYPES.has(path.extname(pathname).toLowerCase())) return null;
109
+ if (pathname === CUSTOM_CSS_URL) return {file: _roots.customCss};
110
+ let base;
111
+ let relative;
112
+ if (pathname.startsWith('/dist/domma/')) { base = _roots.dommaDist; relative = pathname.slice(12); }
113
+ else if (pathname.startsWith('/public/')) { base = path.join(_roots.root, 'public'); relative = pathname.slice(8); }
114
+ else if (/^\/plugins\/[^/]+\/public\//.test(pathname)) { base = _roots.pluginsDir; relative = pathname.slice(9); }
115
+ else return null;
116
+ const file = path.resolve(base, relative);
117
+ return file.startsWith(base + path.sep) ? {file} : null;
118
+ }
119
+
120
+ /**
121
+ * A file's version as it is now, or null when it cannot be read. Kept per
122
+ * file while its mtime and size are unchanged, so a file is hashed once.
123
+ *
124
+ * @param {string} file
125
+ * @returns {Promise<string|null>}
126
+ */
127
+ async function versionOf(file) {
128
+ return (await moduleInfo(file))?.version ?? null;
129
+ }
130
+
131
+ /** Read a text asset once: its version and, for a script, the specifiers it imports. */
132
+ async function describe(file, signature) {
133
+ let body;
134
+ try { body = await fs.readFile(file); } catch { return null; }
135
+ const entry = {signature, version: createHash('sha256').update(body).digest('hex').slice(0, 16)};
136
+ if (/\.m?js$/i.test(file)) entry.specs = importSpecifiers(body.toString('utf8'));
137
+ _versions.set(file, entry);
138
+ while (_versions.size > VERSIONS_MAX) _versions.delete(_versions.keys().next().value);
139
+ return entry;
140
+ }
141
+
142
+ /** A file's version (and a script's imports), as it is now; null when it cannot be read. */
143
+ async function moduleInfo(file) {
144
+ let st;
145
+ try { st = await fs.stat(file); } catch { return null; }
146
+ if (!st.isFile()) return null;
147
+ const signature = `${st.mtimeMs}:${st.size}`;
148
+ const kept = _versions.get(file);
149
+ return kept && kept.signature === signature ? kept : describe(file, signature);
150
+ }
151
+
152
+ const IMPORT_FROM = /\b(?:import|export)\b[^'"`;()]*?\bfrom\s*(['"])([^'"\n]+)\1/g;
153
+ const IMPORT_BARE = /\bimport\s*(['"])([^'"\n]+)\1/g;
154
+ const IMPORT_CALL = /\bimport\s*\(\s*(['"])([^'"\n]+)\1\s*\)/g;
155
+
156
+ /**
157
+ * The literal specifiers a script imports - `import ... from`, `export ... from`,
158
+ * `import '...'` and `import('...')` - that name a path (`/`, `./`, `../`).
159
+ * Read with patterns, not a parser: one found in a comment only adds a map
160
+ * entry nothing asks for.
161
+ *
162
+ * @param {string} source
163
+ * @returns {string[]}
164
+ */
165
+ export function importSpecifiers(source) {
166
+ const out = new Set();
167
+ for (const re of [IMPORT_FROM, IMPORT_BARE, IMPORT_CALL]) {
168
+ for (const m of source.matchAll(re)) {
169
+ if (/^\.{0,2}\//.test(m[2]) && !m[2].startsWith('//')) out.add(m[2]);
170
+ }
171
+ }
172
+ return [...out];
173
+ }
174
+
175
+ /** A specifier as the browser resolves it from a module at `fromPath`: `path?query`, or null. */
176
+ function resolveSpecifier(spec, fromPath) {
177
+ try {
178
+ const u = new URL(spec, `http://local${fromPath}`);
179
+ if (u.host !== 'local' || u.hash) return null;
180
+ return u.pathname + u.search;
181
+ } catch { return null; }
182
+ }
183
+
184
+ const MODULE_GRAPH_MAX = 400;
185
+ // Opening tags only, each on its own: pairing a tag with the next </script> would let a stray
186
+ // "<script>" in a comment swallow the real tag after it, and that tag's module would go unnoticed.
187
+ const SCRIPT_OPEN = /<script\b([^>]*)>/gi;
188
+
189
+ /**
190
+ * The import map for a page: every module its module scripts import, however
191
+ * deep, from the URL it is imported by to that URL fingerprinted. '' when
192
+ * there is nothing to map or the page must be left alone (it has an import
193
+ * map of its own, or a module script from somewhere this cannot follow).
194
+ *
195
+ * @param {string} html
196
+ * @returns {Promise<{tag: string, rewrite: Set<string>}>} the <script type="importmap"> tag ('' for none), and
197
+ * the module script URLs whose own tags may be fingerprinted
198
+ */
199
+ async function importMapFor(html) {
200
+ const none = {tag: '', rewrite: new Set()};
201
+ if (!html.includes('module') || /<script\b[^>]*\btype\s*=\s*["']?importmap/i.test(html)) return none;
202
+ const classics = new Set();
203
+ const entries = new Set();
204
+ /** URLs something imports - as against URLs the page itself links. */
205
+ const imported = new Set();
206
+ const queue = [];
207
+ for (const m of html.matchAll(SCRIPT_OPEN)) {
208
+ const tag = `<script${m[1]}>`;
209
+ const type = (read(tag, 'type')?.value || '').trim().toLowerCase();
210
+ const src = read(tag, 'src')?.value;
211
+ if (type !== 'module') {
212
+ // A classic script can still import(): effects.js loads its modules that way. Followed, never mapped itself.
213
+ const url = (!type || type === 'text/javascript' || type === 'application/javascript') && src ? src.replaceAll('&amp;', '&') : '';
214
+ if (url.startsWith('/') && !url.startsWith('//') && !url.includes('#')) { classics.add(url); queue.push(url); }
215
+ continue;
216
+ }
217
+ if (src) {
218
+ const url = src.replaceAll('&amp;', '&');
219
+ // A module from somewhere else could import ours by another route: leave the page alone.
220
+ if (!url.startsWith('/') || url.startsWith('//') || url.includes('#')) return none;
221
+ entries.add(url);
222
+ queue.push(url);
223
+ } else {
224
+ const from = m.index + m[0].length;
225
+ const close = html.indexOf('</script', from);
226
+ for (const spec of importSpecifiers(html.slice(from, close < 0 ? from : close))) {
227
+ if (!spec.startsWith('/')) return none; // relative to the page: not followed
228
+ const url = resolveSpecifier(spec, '/');
229
+ if (url) { imported.add(url); queue.push(url); }
230
+ }
231
+ }
232
+ }
233
+ if (!queue.length) return none;
234
+ /** @type {Map<string, string>} the URL a module is imported by -> its version */
235
+ const found = new Map();
236
+ const walked = new Set();
237
+ while (queue.length && walked.size < MODULE_GRAPH_MAX) {
238
+ const url = queue.pop();
239
+ let pathname;
240
+ try { pathname = decodeURIComponent(url.split('?')[0]); } catch { continue; }
241
+ const asset = resolvePublicAsset(pathname);
242
+ if (!asset || !/\.m?js$/i.test(asset.file)) continue;
243
+ const info = await moduleInfo(asset.file);
244
+ if (!info) continue;
245
+ if (!url.includes(`${VERSION_PARAM}=`)) found.set(url, info.version);
246
+ if (walked.has(asset.file)) continue;
247
+ walked.add(asset.file);
248
+ for (const spec of info.specs || []) {
249
+ const next = resolveSpecifier(spec, pathname);
250
+ if (next) { imported.add(next); queue.push(next); }
251
+ }
252
+ }
253
+ for (const url of classics) found.delete(url);
254
+ // A module the page links AND something imports by that same URL keeps its URL everywhere:
255
+ // it must stay one module, in a browser without import maps too. A linked module nothing
256
+ // imports is fingerprinted in its tag, and mapped as well - so an import this could not see
257
+ // (one built from strings) still lands on the same instance wherever import maps work.
258
+ const rewrite = new Set();
259
+ for (const url of entries) {
260
+ if (imported.has(url)) found.delete(url);
261
+ else if (found.has(url)) rewrite.add(url);
262
+ }
263
+ if (!found.size) return none;
264
+ const imports = {};
265
+ for (const url of [...found.keys()].sort()) {
266
+ imports[url] = `${url}${url.includes('?') ? '&' : '?'}${VERSION_PARAM}=${found.get(url)}`;
267
+ }
268
+ return {tag: `<script type="importmap">${JSON.stringify({imports}).replace(/</g, '\\u003c')}</script>`, rewrite};
269
+ }
270
+
271
+ /** Where an import map must go: before the first script or module preload, and inside the head. */
272
+ function importMapOffset(html) {
273
+ const head = html.search(/<\/head\s*>/i);
274
+ if (head < 0) return -1;
275
+ const first = html.search(/<script\b|<link\b[^>]*\brel\s*=\s*["']?modulepreload/i);
276
+ return first >= 0 && first < head ? first : head;
277
+ }
278
+
279
+ /**
280
+ * The version a URL as written in a page (attribute text, `&amp;` and all)
281
+ * should carry, or null to leave it alone.
282
+ */
283
+ async function versionForAttr(raw) {
284
+ if (!raw.startsWith('/') || raw.startsWith('//') || raw.includes('#')) return null;
285
+ if (raw.includes(`${VERSION_PARAM}=`) || raw.includes('_av=')) return null;
286
+ let pathname;
287
+ try { pathname = decodeURIComponent(raw.split('?')[0]); } catch { return null; }
288
+ const found = resolvePublicAsset(pathname);
289
+ return found ? versionOf(found.file) : null;
290
+ }
291
+
292
+ const withVersion = (raw, version) => `${raw}${raw.includes('?') ? '&amp;' : '?'}${VERSION_PARAM}=${version}`;
293
+
294
+ const TAG = /<(script|link)\b[^>]*>/gi;
295
+ const ATTRS = {
296
+ src: /\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)')/i,
297
+ href: /\shref\s*=\s*(?:"([^"]*)"|'([^']*)')/i,
298
+ type: /\stype\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i,
299
+ rel: /\srel\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s>]+))/i
300
+ };
301
+ const read = (tag, name) => {
302
+ const m = ATTRS[name].exec(tag);
303
+ if (!m) return null;
304
+ const value = m[1] ?? m[2] ?? m[3] ?? '';
305
+ return {value, start: m.index + m[0].indexOf(value, m[0].indexOf('=') + 1)};
306
+ };
307
+
308
+ /** The URLs in one tag that may be fingerprinted: `{start, end, raw}` offsets within the tag. */
309
+ function urlsIn(tag, name) {
310
+ const out = [];
311
+ const push = (attr) => { if (attr && attr.value) out.push({start: attr.start, end: attr.start + attr.value.length, raw: attr.value}); };
312
+ if (name === 'script') {
313
+ const type = (read(tag, 'type')?.value || '').trim().toLowerCase();
314
+ if (type === 'module') {
315
+ // Only where the import map says so (see importMapFor).
316
+ const src = read(tag, 'src');
317
+ if (src && src.value) out.push({start: src.start, end: src.start + src.value.length, raw: src.value, module: true});
318
+ return out;
319
+ }
320
+ if (type && type !== 'text/javascript' && type !== 'application/javascript') return out;
321
+ push(read(tag, 'src'));
322
+ } else if (name === 'link') {
323
+ const rel = (read(tag, 'rel')?.value || '').toLowerCase().split(/\s+/);
324
+ if (!rel.includes('stylesheet')) return out;
325
+ push(read(tag, 'href'));
326
+ }
327
+ return out;
328
+ }
329
+
330
+ /**
331
+ * A rendered page with its local stylesheets and classic scripts
332
+ * fingerprinted, and an import map for its modules. Unchanged when configurePublicAssets() has not run, and on
333
+ * any failure - a page must never fail to render over its cache headers.
334
+ *
335
+ * @param {string} html
336
+ * @returns {Promise<string>}
337
+ */
338
+ export async function fingerprintHtml(html) {
339
+ if (!_roots || typeof html !== 'string' || !html) return html;
340
+ try {
341
+ /** @type {{start: number, end: number, raw: string}[]} absolute offsets */
342
+ const spots = [];
343
+ for (const m of html.matchAll(TAG)) {
344
+ for (const u of urlsIn(m[0], m[1].toLowerCase())) {
345
+ spots.push({start: m.index + u.start, end: m.index + u.end, raw: u.raw, module: u.module});
346
+ }
347
+ }
348
+ const importMap = await importMapFor(html);
349
+ const mapAt = importMap.tag ? importMapOffset(html) : -1;
350
+ if (mapAt >= 0) spots.push({start: mapAt, end: mapAt, raw: '', insert: `${importMap.tag}\n`});
351
+ // A module tag is fingerprinted only with the map in the page that covers it.
352
+ const moduleOk = (raw) => mapAt >= 0 && importMap.rewrite.has(raw.replaceAll('&amp;', '&'));
353
+ for (let i = spots.length - 1; i >= 0; i--) if (spots[i].module && !moduleOk(spots[i].raw)) spots.splice(i, 1);
354
+ if (!spots.length) return html;
355
+ spots.sort((a, b) => a.start - b.start);
356
+ const unique = [...new Set(spots.filter(s => !s.insert).map(s => s.raw))];
357
+ const versions = new Map(await Promise.all(unique.map(async raw => [raw, await versionForAttr(raw)])));
358
+ let out = '';
359
+ let last = 0;
360
+ for (const s of spots) {
361
+ if (s.insert) {
362
+ out += html.slice(last, s.start) + s.insert;
363
+ last = s.start;
364
+ continue;
365
+ }
366
+ const version = versions.get(s.raw);
367
+ if (!version || html.slice(s.start, s.end) !== s.raw) continue;
368
+ out += html.slice(last, s.start) + withVersion(s.raw, version);
369
+ last = s.end;
370
+ }
371
+ return out ? out + html.slice(last) : html;
372
+ } catch {
373
+ return html;
374
+ }
375
+ }
376
+
377
+ /**
378
+ * The site's custom CSS for a page's head: a <link> to CUSTOM_CSS_URL once it
379
+ * is big enough to be worth a request of its own (and this module is
380
+ * configured), otherwise the <style> tag it always was. `inline: true` keeps
381
+ * the <style> whatever the size - a preview is drawn where the page's own
382
+ * requests may not be allowed.
383
+ *
384
+ * @param {string} css - content/custom.css
385
+ * @param {{inline?: boolean}} [opts]
386
+ * @returns {string}
387
+ */
388
+ export function customCssTag(css, {inline = false} = {}) {
389
+ const text = String(css || '');
390
+ if (!text.trim()) return '';
391
+ // A relative url() or @import resolves against the PAGE while the CSS is inline; linked, it
392
+ // would resolve against /content/. Such a stylesheet stays inline, exactly as it was.
393
+ if (!inline && _roots && Buffer.byteLength(text) > CUSTOM_CSS_INLINE_MAX && !RELATIVE_CSS_URL.test(text)) {
394
+ return `<link rel="stylesheet" href="${CUSTOM_CSS_URL}">`;
395
+ }
396
+ return `<style>${text.replace(/<\/style>/gi, '<\\/style>')}</style>`;
397
+ }
398
+
399
+ /** A url(...) or @import "..." that is not absolute (/, a scheme, //), a data: URI or a #fragment. */
400
+ const RELATIVE_CSS_URL = /(?:url\(\s*|@import\s+)(?:["']\s*)?(?!\/|[a-z][a-z0-9+.-]*:|#|["']?\s*\)|url\()[^\s"')]/i;
401
+
402
+ async function readCustomCss() {
403
+ let st;
404
+ try { st = await fs.stat(_roots.customCss); } catch { return null; }
405
+ const signature = `${st.mtimeMs}:${st.size}`;
406
+ if (_customCss && _customCss.signature === signature) return _customCss;
407
+ let body;
408
+ try { body = await fs.readFile(_roots.customCss); } catch { return null; }
409
+ const version = createHash('sha256').update(body).digest('hex').slice(0, 16);
410
+ _versions.set(_roots.customCss, {signature, version});
411
+ _customCss = {signature, body, version, encoded: new Map()};
412
+ return _customCss;
413
+ }
414
+
415
+ /**
416
+ * Wire it into the server: the immutable cache-control for a matching
417
+ * fingerprint, and the route that serves the custom CSS.
418
+ *
419
+ * @param {import('fastify').FastifyInstance} app
420
+ * @param {{root: string, dommaDist: string, pluginsDir?: string}} roots
421
+ * @returns {{serve: () => void}} call serve() once the plugins are registered
422
+ */
423
+ export function registerPublicAssets(app, roots) {
424
+ configurePublicAssets(roots);
425
+
426
+ /** Does this request's `_pv` name the file as it is now? */
427
+ async function matches(rawUrl) {
428
+ const q = rawUrl.indexOf('?');
429
+ if (q < 0 || !rawUrl.includes(`${VERSION_PARAM}=`, q)) return false;
430
+ let pathname;
431
+ let asked;
432
+ try {
433
+ pathname = decodeURIComponent(rawUrl.slice(0, q));
434
+ asked = new URLSearchParams(rawUrl.slice(q + 1)).get(VERSION_PARAM);
435
+ } catch { return false; }
436
+ if (!asked) return false;
437
+ const found = resolvePublicAsset(pathname);
438
+ if (!found) return false;
439
+ return (await versionOf(found.file)) === asked;
440
+ }
441
+
442
+ app.decorateRequest('dmImmutableAsset', false);
443
+ // Added by serve(), after the plugins, as adminAssets' is: a plugin's onRequest hook that
444
+ // answers a request itself (site-manager proxying another site's host) must run first, or
445
+ // this would judge that site's fingerprint against THIS install's files.
446
+ const serve = () => {
447
+ app.addHook('onRequest', async (request) => {
448
+ if (request.method !== 'GET' && request.method !== 'HEAD') return;
449
+ if (!request.raw.url.includes(`${VERSION_PARAM}=`)) return;
450
+ if (request.raw.url.startsWith(CUSTOM_CSS_URL)) return; // its route decides for itself
451
+ if (await matches(request.raw.url)) request.dmImmutableAsset = true;
452
+ });
453
+ app.addHook('onSend', async (request, reply, payload) => {
454
+ if (request.dmImmutableAsset && (reply.statusCode === 200 || reply.statusCode === 304)) {
455
+ reply.header('cache-control', IMMUTABLE);
456
+ }
457
+ return payload;
458
+ });
459
+ };
460
+
461
+ app.route({
462
+ method: ['GET', 'HEAD'],
463
+ url: CUSTOM_CSS_URL,
464
+ handler: async (request, reply) => {
465
+ const css = await readCustomCss();
466
+ if (!css) return reply.code(404).type('text/plain; charset=utf-8').send('Not found\n');
467
+ const asked = (request.query || {})[VERSION_PARAM];
468
+ const encoding = css.body.length >= 1024 ? pickEncoding(request.headers['accept-encoding']) : null;
469
+ const etag = `"${css.version}${encoding ? `-${encoding}` : ''}"`;
470
+ reply.header('cache-control', asked === css.version ? IMMUTABLE : 'public, max-age=0');
471
+ reply.header('etag', etag);
472
+ reply.header('vary', 'accept-encoding');
473
+ reply.type('text/css; charset=utf-8');
474
+ const seen = String(request.headers['if-none-match'] || '').split(',').map(t => t.trim().replace(/^W\//, ''));
475
+ if (seen.some(t => t === `"${css.version}"` || t.startsWith(`"${css.version}-`))) return reply.code(304).send();
476
+ if (!encoding) return reply.send(css.body);
477
+ if (!css.encoded.has(encoding)) {
478
+ const made = encode(css.body, encoding);
479
+ css.encoded.set(encoding, made);
480
+ made.catch(() => css.encoded.delete(encoding));
481
+ }
482
+ const body = await css.encoded.get(encoding);
483
+ reply.header('content-encoding', encoding);
484
+ return reply.send(body);
485
+ }
486
+ });
487
+ return {serve};
488
+ }
@@ -25,6 +25,8 @@ import {cssClasses} from '../../public/js/css-classes.mjs';
25
25
  import {fontFamilyValue, fontStylesheetHref, isFontStylesheet} from '../../public/js/font-families.mjs';
26
26
  import {CMS_WEB_FONTS} from '../../public/js/text-style.mjs';
27
27
  import {parseMarkdown} from './markdown.js';
28
+ import {customCssTag as buildCustomCssTag, fingerprintHtml} from './publicAssets.js';
29
+ import {effectsSettingsScript} from './effectsSettings.js';
28
30
 
29
31
  const VALID_LAYOUT_WIDTHS = new Set(['narrow', 'normal', 'wide', 'full']);
30
32
  const CUSTOM_CSS_PATH = new URL('../../content/custom.css', import.meta.url).pathname;
@@ -553,9 +555,8 @@ export async function renderPage(page, opts = {}) {
553
555
 
554
556
  const {navbarFontLink, navbarStyleTag} = buildNavbarStyleTag(navigation, themeView.cfg.font);
555
557
 
556
- const customCssTag = customCss.trim()
557
- ? `<style>${customCss.replace(/<\/style>/gi, '<\\/style>')}</style>`
558
- : '';
558
+ // Linked once it is big; inline in a preview, which may be drawn where the page's own requests are refused.
559
+ const customCssTag = buildCustomCssTag(customCss, {inline: Boolean(opts.preview || opts.noTrack)});
559
560
 
560
561
  // Compose window.__CMS_FOOTER__ from the menus mapped to footer-primary / footer-legal.
561
562
  const footer = {
@@ -643,6 +644,7 @@ export async function renderPage(page, opts = {}) {
643
644
  footerFallbackHtml,
644
645
  footerScript: footerJson ? `window.__CMS_FOOTER__ = ${footerJson};` : '',
645
646
  ctxMenusScript: ctxMenusScript,
647
+ effectsScript: effectsSettingsScript(),
646
648
  ctxMenusModule: ctxMenusModule,
647
649
  imageZoomModule: imageZoomModule(page.html, overlayMenus, footerBlocksHtml),
648
650
  // Every `<` escaped, not only `</script>`: a label holding `<!--<script` would
@@ -698,7 +700,8 @@ export async function renderPage(page, opts = {}) {
698
700
 
699
701
  const finalVars = applyTransforms('render:beforeRender', vars, {page});
700
702
  const html = interpolate(template, finalVars);
701
- return applyTransforms('render:afterRender', html, {page});
703
+ // Last of all, so what a Tool adds is covered too: local assets fingerprinted for long caching (publicAssets.js).
704
+ return fingerprintHtml(applyTransforms('render:afterRender', html, {page}));
702
705
  }
703
706
 
704
707
  /**
@@ -1355,9 +1358,7 @@ export async function renderBlogPage(templatePath, data = {}, seoMeta = {}) {
1355
1358
  const fontLink = contentFontLinks(`${renderedFragment}${overlayMenus}${footerBlocksHtml}`, themeView.fontLink);
1356
1359
  const {navbarFontLink, navbarStyleTag} = buildNavbarStyleTag(navigation, themeView.cfg.font);
1357
1360
 
1358
- const customCssTag = customCss.trim()
1359
- ? `<style>${customCss.replace(/<\/style>/gi, '<\\/style>')}</style>`
1360
- : '';
1361
+ const customCssTag = buildCustomCssTag(customCss);
1361
1362
 
1362
1363
  // Compose window.__CMS_FOOTER__ from the menus mapped to footer-primary / footer-legal.
1363
1364
  const footer = {
@@ -1433,6 +1434,7 @@ export async function renderBlogPage(templatePath, data = {}, seoMeta = {}) {
1433
1434
  footerFallbackHtml,
1434
1435
  footerScript: footerJson ? `window.__CMS_FOOTER__ = ${footerJson};` : '',
1435
1436
  ctxMenusScript: ctxMenusScript,
1437
+ effectsScript: effectsSettingsScript(),
1436
1438
  ctxMenusModule: ctxMenusModule,
1437
1439
  imageZoomModule: imageZoomModule(renderedFragment, overlayMenus, footerBlocksHtml),
1438
1440
  // Every `<` escaped, as on the page path above.
@@ -1472,7 +1474,7 @@ export async function renderBlogPage(templatePath, data = {}, seoMeta = {}) {
1472
1474
 
1473
1475
  // The same last step as a page: a Tool's render:afterRender sees a blog post or a Tool's page too. Not a
1474
1476
  // bare page: its address is a credential, so nothing of a Tool's goes on it.
1475
- return bare ? interpolate(template, vars) : applyTransforms('render:afterRender', interpolate(template, vars), {page: null});
1477
+ return fingerprintHtml(bare ? interpolate(template, vars) : applyTransforms('render:afterRender', interpolate(template, vars), {page: null}));
1476
1478
  }
1477
1479
 
1478
1480
  /**
@@ -210,7 +210,7 @@ async function seo(fastify, {enabled = true, getBaseUrl = null, rolesService = n
210
210
  seen.add(e.urlPath);
211
211
  list.push({urlPath: e.urlPath, source: e.source, title: e.title});
212
212
  }
213
- for (const p of await listPages()) {
213
+ for (const p of await listPages({render: false})) {
214
214
  if (seen.has(p.urlPath) || !isPublic(p)) continue;
215
215
  list.push({urlPath: p.urlPath, source: 'pages', title: p.title, noindex: p.seo?.noindex === true, unlisted: true});
216
216
  }
@@ -218,7 +218,7 @@ async function seo(fastify, {enabled = true, getBaseUrl = null, rolesService = n
218
218
  }
219
219
  async function focusMap() {
220
220
  const map = {};
221
- for (const p of await listPages()) if (p.seo?.focus) map[p.urlPath] = p.seo.focus;
221
+ for (const p of await listPages({render: false})) if (p.seo?.focus) map[p.urlPath] = p.seo.focus;
222
222
  for (const [p, o] of Object.entries(store.overrides())) if (o.focus) map[p] = o.focus;
223
223
  return map;
224
224
  }
@@ -301,7 +301,7 @@ async function seo(fastify, {enabled = true, getBaseUrl = null, rolesService = n
301
301
 
302
302
  /** The sitemap entries before SEO's filter (pages + every source), to show what it leaves out. */
303
303
  async function rawEntries(origin) {
304
- const out = (await listPages()).filter(p => isPublic(p) && !p.seo?.noindex).map(p => ({urlPath: p.urlPath, title: p.title, source: 'pages'}));
304
+ const out = (await listPages({render: false})).filter(p => isPublic(p) && !p.seo?.noindex).map(p => ({urlPath: p.urlPath, title: p.title, source: 'pages'}));
305
305
  const seen = new Set(out.map(p => p.urlPath));
306
306
  for (const s of getSitemapSources()) {
307
307
  try {
@@ -55,7 +55,7 @@ function pageEntry(page) {
55
55
  */
56
56
  export async function listSitemapEntries(baseUrl, {log = console} = {}) {
57
57
  const normalised = normaliseBaseUrl(baseUrl);
58
- const pages = (await listPages()).filter(shouldIncludeInSitemap);
58
+ const pages = (await listPages({render: false})).filter(shouldIncludeInSitemap);
59
59
  const offline = await offlinePageUrls(pages);
60
60
  const entries = pages.filter(p => !offline.has(p.urlPath)).map(pageEntry);
61
61
  const seen = new Set(entries.map(e => e.urlPath));
@@ -368,6 +368,17 @@ export function compileCustomThemeCss(ids = null) {
368
368
  .join('\n\n');
369
369
  }
370
370
 
371
+ /** Presentation shared by authenticated admin and anonymous sign-in screens.
372
+ * Callers choose the id; sign-in exposes only the saved admin theme.
373
+ */
374
+ export function adminThemePresentation(id, cfg = {}) {
375
+ const theme = id || cfg.adminTheme || DEFAULT_THEME;
376
+ const {dommaTheme, customClass} = resolveThemeClasses(theme, cfg);
377
+ return {theme, dommaTheme, customClass, css: customClass ? compileCustomThemeCss([theme]) : '',
378
+ font: {family: cfg.font?.adminFamily || '', stylesheet: cfg.font?.stylesheet || ''},
379
+ sidebar: cfg.adminSidebar !== false};
380
+ }
381
+
371
382
  /**
372
383
  * The page-head `<style>` for the custom themes a page can show, or ''. Only
373
384
  * those: every theme a Roller keeps is ~250 tokens, and an unused draft's
@@ -256,8 +256,8 @@ export async function deleteVersion(urlPath, filename) {
256
256
  */
257
257
  async function keepLiveAdminTags(livePath, restored) {
258
258
  let live;
259
- try { live = matter(await fs.readFile(livePath, 'utf8')).data?.adminTags; } catch { return restored; }
260
- const old = matter(restored);
259
+ try { live = matter(await fs.readFile(livePath, 'utf8'), {}).data?.adminTags; } catch { return restored; }
260
+ const old = matter(restored, {});
261
261
  if (JSON.stringify(old.data?.adminTags ?? null) === JSON.stringify(live ?? null)) return restored;
262
262
  const data = {...old.data};
263
263
  if (Array.isArray(live) && live.length) data.adminTags = live;
@@ -216,7 +216,7 @@ async function pageFingerprint(urlPath) {
216
216
  try {
217
217
  const file = await resolveExistingFilePath(urlPath);
218
218
  const raw = await fs.readFile(file, 'utf8');
219
- const parsed = matter(raw);
219
+ const parsed = matter(raw, {});
220
220
  return fingerprint(parsed.data?.title, parsed.content);
221
221
  } catch { return ''; }
222
222
  }
@@ -238,7 +238,7 @@ export function footerBlockList(site = null) {
238
238
 
239
239
  /** The site's state as the planner needs it. */
240
240
  export async function siteState(mapping = {}, source = '') {
241
- const pages = await listPages();
241
+ const pages = await listPages({render: false});
242
242
  const pagePaths = new Set(pages.map(p => String(p.urlPath || '/').toLowerCase()));
243
243
  const homePage = pages.find(p => String(p.urlPath || '/') === '/');
244
244
  const pageHashes = {};
@@ -158,6 +158,7 @@
158
158
  window.__CMS_SITE__ = {{siteJson}};
159
159
  {{footerScript}}
160
160
  {{ctxMenusScript}}
161
+ {{effectsScript}}
161
162
  (function () {
162
163
  var c = window.__CMS_SITE__ && window.__CMS_SITE__.autoTheme;
163
164
  if (!c || !c.enabled) return;
@@ -188,7 +189,7 @@
188
189
  <script src="/public/js/form-logic-engine.js?v=20261031-forms-reduce-motion" defer></script>
189
190
 
190
191
  <!-- Site initialisation -->
191
- <script src="/public/js/site.js?v=20261110-theme-preference" type="module"></script>
192
+ <script src="/public/js/site.js?v=20261111-one-menu-float" type="module"></script>
192
193
  <script src="/public/js/collection-context.js?v=20261010-utf8-payloads" type="module"></script>
193
194
  {{ctxMenusModule}}
194
195
  <!-- [image zoom] pictures' full-size view - only on a page that has one -->
@@ -203,7 +204,7 @@
203
204
  <script src="/public/js/forms.js?v=20261109-form-embed" type="module"></script>
204
205
 
205
206
  <!-- Core effects runtime -->
206
- <script src="/public/js/effects.js?v=20261029-all-cms-fonts" defer></script>
207
+ <script src="/public/js/effects.js?v=20261111-inline-settings" defer></script>
207
208
 
208
209
  <!-- Plugin body-end injection -->
209
210
  {{bodyEndInject}}