Files
poli0981andClaude Opus 5.5 3743cfbfe0 v1.1: native view transitions, CSP-first head, PWA, search, i18n/a11y, display menu, data rules, e2e (#23)
- fix(check-csp): match end tags the way the HTML tokenizer does
- ci(deps): keep @types/node on the Node major in .nvmrc
- test: Playwright + axe e2e, script tests, island coverage, CI gates
- fix(sort): pin the category-page and API orders to the data
- fix(a11y): keep the visible EN/VI code in the switcher's accessible name
- fix(nav): reliable cross-document View Transitions opt-in
- feat(data): enforce the data rules CONTRIBUTING promised; fix quarantine flow
- feat(prefs): display menu with theme, custom scrollbar and motion choices
- fix(i18n,a11y): real small type, translated chips and errors, nav semantics
- fix(search): race-free search with URL state, stable sort and relevance
- fix(pwa): installable app, styled offline page, prompt SW updates, head fixes
- refactor(scripts): hashed pre-paint scripts, CSP meta first, richer error log
- refactor(nav): replace <ClientRouter /> with native cross-document view transitions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-21 22:22:48 +07:00

53 lines
2.0 KiB
JavaScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 poli0981 (SkullMute)
/**
* Post-build HTML fix-ups. Runs between `astro build` and the dist/ checks.
*
* 1. Move the CSP <meta> to the top of <head>.
*
* A meta-tag CSP only governs what the parser meets AFTER it. Astro appends
* its generated tag at the END of <head>, behind every blocking and
* deferred script there (errors.js, theme.js, registerSW.js) — so those
* loaded under no policy at all. They are our own same-origin files and
* would pass `script-src 'self'` anyway; the point is that NOTHING in the
* document should precede the policy that is supposed to cover it. Placing
* the tag right after `<meta charset>` (which must stay first, within the
* first 1024 bytes) makes the whole head subject to it. check-csp.mjs
* asserts the result on every page.
*
* Idempotent: a page whose meta already follows the charset is left alone.
*/
import { readFileSync, readdirSync, statSync, writeFileSync } from 'node:fs';
import { join } from 'node:path';
const DIST = join(process.cwd(), 'dist');
const CHARSET = '<meta charset="utf-8">';
const CSP_META = /<meta http-equiv="content-security-policy" content="[^"]*">/i;
function walk(dir) {
const out = [];
for (const entry of readdirSync(dir)) {
const p = join(dir, entry);
if (statSync(p).isDirectory()) out.push(...walk(p));
else if (p.endsWith('.html')) out.push(p);
}
return out;
}
let moved = 0;
let pages = 0;
for (const file of walk(DIST)) {
pages++;
const html = readFileSync(file, 'utf8');
const meta = CSP_META.exec(html)?.[0];
const at = html.indexOf(CHARSET);
if (!meta || at === -1) continue; // check-csp.mjs reports both cases
if (html.indexOf(meta) === at + CHARSET.length) continue;
const without = html.replace(meta, '');
const insertAt = without.indexOf(CHARSET) + CHARSET.length;
writeFileSync(file, without.slice(0, insertAt) + meta + without.slice(insertAt));
moved++;
}
console.log(`postbuild — CSP meta moved to the top of <head> on ${moved} of ${pages} pages`);