Files
SluijsensandClaude Sonnet 5.5 7737104a77 Lets the CI preview server stand in for the contact API
With contactForm.enabled=true the e2e and Lighthouse runs asked a token route
that CI does not have (404, console error, run 666). The preview server gets an
opt-in --contact-stub (token 200 without captcha, submit 202); the e2e server
helper and the Lighthouse start command switch it on. Tests that need another
answer still replace it per page with page.route.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-11 19:39:03 +01:00

185 lines
6.9 KiB
JavaScript

// Lighthouse CI tegen de gebouwde site (`pnpm preview`), zodat de echte
// securityheaders en de 404-afhandeling meetellen. Doelen: NFR-P-01, NFR-A-01,
// NFR-S-01/02. Dit is de mobiele meting; lighthouserc.desktop.cjs bouwt hierop
// voort voor desktop. `pnpm lighthouse` draait beide.
const origin = 'http://localhost:4173';
// Moet gelijk lopen met src/sitePages.ts (een test bewaakt dat)
const indexedPaths = [
'/',
'/over-mij',
'/portfolio',
'/soorten-fotoshoots',
'/soorten-fotoshoots/newborn',
'/soorten-fotoshoots/zwangerschap',
'/soorten-fotoshoots/gezin',
'/soorten-fotoshoots/loveshoot',
'/soorten-fotoshoots/portret',
'/contact',
];
const noindexPaths = ['/privacyverklaring'];
// Lighthouse refuses a main document with an error status, so the preview
// serves the 404 page with status 200 on this route only, and only when it is
// started with --audit-route (BR-HST-22). /404.html itself is a real 404.
const notFoundPath = '/__audit/404';
// Mobile performance exceptions, keyed by path. Every entry needs a recorded
// decision and a floor below 0.9 (BR-HST-23); a page without an entry must
// reach 0.9. Decided by the owner on 2026-11-01 after the final measurement
// of prerendering (measurement-final.md):
// - /over-mij, /soorten-fotoshoots and the newborn page (0.91 to 0.92) have no
// exception any more.
// - zwangerschap and portret score around 0.90; with one run that is unreliable
// as a target, so their floor stays at 0.85.
// - the homepage (0.85 measured, floor raised from 0.8), gezin and loveshoot
// (0.89 and 0.88, floor 0.85) are recorded at their current score until the
// photos are responsive (NFR-P-01).
const unreliableAtNinety =
'scoort rond 0,90; met 1 run onbetrouwbaar als doel (eigenaar, 2026-11-01)';
const untilResponsivePhotos =
"bij de huidige score vastgelegd, tot responsive foto's (eigenaar, 2026-11-01)";
const mobileExceptions = {
'/': { minScore: 0.85, decision: untilResponsivePhotos },
'/soorten-fotoshoots/zwangerschap': {
minScore: 0.85,
decision: unreliableAtNinety,
},
'/soorten-fotoshoots/gezin': {
minScore: 0.85,
decision: untilResponsivePhotos,
},
'/soorten-fotoshoots/loveshoot': {
minScore: 0.85,
decision: untilResponsivePhotos,
},
'/soorten-fotoshoots/portret': {
minScore: 0.85,
decision: unreliableAtNinety,
},
};
const validateExceptions = (exceptions, paths) => {
for (const [path, { minScore, decision }] of Object.entries(exceptions)) {
if (!paths.includes(path)) {
throw new Error(`Exception for unknown page ${path}`);
}
if (typeof decision !== 'string' || decision.trim() === '') {
throw new Error(`Exception for ${path} has no decision`);
}
if (!(minScore < 0.9)) {
throw new Error(`Exception for ${path} is not below 0.9`);
}
}
};
validateExceptions(mobileExceptions, indexedPaths);
// Performance hangt af van de snelheid van de meetmachine: Lighthouse vertraagt
// de CPU 4x ten opzichte van een snelle desktop. Op de CI-runner (Raspberry Pi)
// valt de score daardoor 30-40 punten lager uit dan op een pc. In CI daarom
// alleen melden (LIGHTHOUSE_PERFORMANCE=warn); lokaal in `pnpm check` blokkerend.
// numberOfRuns is bewust 1: een pagina die precies op 0,90 zit kan `pnpm check`
// toevallig laten falen.
const performanceLevel =
process.env.LIGHTHOUSE_PERFORMANCE === 'warn' ? 'warn' : 'error';
const quality = {
'categories:performance': [performanceLevel, { minScore: 0.9 }],
'categories:accessibility': ['error', { minScore: 1 }],
'categories:best-practices': ['error', { minScore: 1 }],
};
// SEO-audits die ook voor noindex-pagina's moeten slagen. Alleen
// `is-crawlable` mag falen: die pagina's staan bewust op noindex.
const seoBasics = {
'document-title': 'error',
'link-text': 'error',
'crawlable-anchors': 'error',
'image-alt': 'error',
};
const escape = (path) => path.replace(/[.*+?^${}()|[\]\\/]/g, '\\$&');
const exact = (paths) => `^${origin}(${paths.map(escape).join('|')})$`;
// One entry per page with an exception, one for all other indexed pages
const buildIndexedEntries = (exceptions, paths) => {
const others = paths.filter((path) => !(path in exceptions));
return [
...Object.entries(exceptions).map(([path, { minScore }]) => ({
matchingUrlPattern: exact([path]),
assertions: {
...quality,
'categories:performance': [performanceLevel, { minScore }],
'categories:seo': ['error', { minScore: 1 }],
},
})),
// Empty until another indexed page without an exception exists
...(others.length > 0
? [
{
matchingUrlPattern: exact(others),
assertions: {
...quality,
'categories:seo': ['error', { minScore: 1 }],
},
},
]
: []),
];
};
module.exports = {
performanceLevel,
indexedPaths,
noindexPaths,
notFoundPath,
mobileExceptions,
validateExceptions,
buildIndexedEntries,
ci: {
collect: {
startServerCommand:
'pnpm preview --port 4173 --strictPort --audit-route --contact-stub',
startServerReadyPattern: 'Local',
url: [...indexedPaths, ...noindexPaths, notFoundPath].map(
(path) => origin + path,
),
numberOfRuns: 1,
// In de CI-container draait Chrome als root en heeft het
// --no-sandbox nodig (LIGHTHOUSE_CHROME_FLAGS in de workflow).
// Niet via LHCI_COLLECT__SETTINGS__*: dat vervangt alle settings,
// ook de desktop-preset.
settings: {
chromeFlags: process.env.LIGHTHOUSE_CHROME_FLAGS ?? '',
},
},
assert: {
// lighthouserc.desktop.cjs raises every performance target back
// to 0.9. Every URL matching an entry must pass it, so an
// exception may only match the pages it names.
assertMatrix: [
...buildIndexedEntries(mobileExceptions, indexedPaths),
{
matchingUrlPattern: exact(noindexPaths),
assertions: {
...quality,
...seoBasics,
'meta-description': 'error',
canonical: 'error',
},
},
{
matchingUrlPattern: exact([notFoundPath]),
assertions: { ...quality, ...seoBasics },
},
],
},
upload: {
// HTML- en JSON-rapporten per meting; in CI als download bij de run
target: 'filesystem',
outputDir: 'lighthouse-reports/mobile',
},
},
};