Files
AndreaSlopFotografie/web/config/serverConfig.ts
T
SluijsensandClaude Opus 5.5 a8903c9641
Continuous Integration / check (pull_request) Failing after 6m13s
Continuous Integration / lighthouse (pull_request) Skipped
Continuous Integration / deploy-test (pull_request) Skipped
Continuous Integration / deploy-production (pull_request) Skipped
Fixes CI test timeouts on the arm64 runner
The first render with axe takes up to 7 s on the Raspberry Pi runner,
above Vitest's 5 s default; the suite now allows 20 s per test. Also
silences the router warning in the test that crashes a page on purpose,
and makes the nginx test config return 404 instead of a redirect loop
(500) while the test folder is still empty.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 00:33:41 +02:00

164 lines
6.2 KiB
TypeScript

// Genereert de webserverconfiguratie voor test (nginx op de Raspberry Pi) en
// productie (IIS bij de webhoster) uit dezelfde securityheaders als de Vite
// dev- en preview-server. Zo bestaat de headerset maar op één plek.
// Schrijven: `pnpm server-config`. Een test controleert dat de bestanden in
// deploy/ gelijk zijn aan wat deze functies opleveren.
import { securityHeaders } from './securityHeaders.ts';
const generatedNotice =
'Gegenereerd door web/config/serverConfig.ts (pnpm server-config). Niet handmatig aanpassen.';
const xmlAttribute = (value: string) =>
value
.replace(/&/g, '&amp;')
.replace(/"/g, '&quot;')
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;');
// IIS (productie): security headers, HTTPS-redirect, SPA-fallback en caching.
// Bestanden in /assets hebben een hash in de naam en mogen lang gecachet
// worden; index.html nooit, zodat een nieuwe release direct zichtbaar is.
export function iisWebConfig(): string {
const headers = Object.entries(securityHeaders('production'))
.map(
([name, value]) =>
` <add name="${name}" value="${xmlAttribute(value)}" />`,
)
.join('\n');
return `<?xml version="1.0" encoding="utf-8"?>
<!-- ${generatedNotice} -->
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="HTTPS" stopProcessing="true">
<match url="(.*)" />
<conditions>
<add input="{HTTPS}" pattern="^OFF$" />
</conditions>
<action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />
</rule>
<rule name="SPA fallback" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/index.html" />
</rule>
</rules>
</rewrite>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
${headers}
</customHeaders>
</httpProtocol>
<staticContent>
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff2" mimeType="font/woff2" />
<clientCache cacheControlMode="DisableCache" />
</staticContent>
</system.webServer>
<location path="assets">
<system.webServer>
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
</system.webServer>
</location>
</configuration>
`;
}
// nginx (test): dezelfde headers, plus noindex zodat de testsite nooit in
// zoekmachines komt. nginx erft add_header niet als een location er zelf een
// zet, daarom staan de headers in elk location-blok.
export function nginxTestConf(): string {
const headers = [
...Object.entries(securityHeaders('production')).map(
([name, value]) => `add_header ${name} "${value}" always;`,
),
'add_header X-Robots-Tag "noindex, nofollow" always;',
];
const block = (indent: string, extra: string[] = []) =>
[...headers, ...extra].map((line) => indent + line).join('\n');
return `# ${generatedNotice}
# Test-omgeving op de Raspberry Pi, achter de reverse-proxy-Pi (die TLS afhandelt
# met certbot). De proxy moet de Host-header doorgeven (proxy_set_header Host
# $host), anders kiest nginx hier het standaardblok (WordPress).
# Eigen server-blok naast de WordPress-site: bewust geen default_server, zodat
# dit blok alleen de testhostname beantwoordt.
# Pas server_name en root aan aan de server; de rest komt uit de generator.
server {
listen 80;
server_name test.andreaslop.nl;
root /var/www/html/andreaslop-test;
index index.html;
# Achter de reverse proxy: doorverwijzingen relatief houden, anders stuurt
# nginx naar zijn eigen http://-adres in plaats van de https-hostname
absolute_redirect off;
${block(' ', ['add_header Cache-Control "no-cache" always;'])}
# Testsite nooit indexeren
location = /robots.txt {
${block(' ')}
default_type text/plain;
return 200 "User-agent: *\\nDisallow: /\\n";
}
# Bestanden met een hash in de naam: lang cachen
location /assets/ {
${block(' ', ['add_header Cache-Control "public, max-age=31536000, immutable" always;'])}
try_files $uri =404;
}
# SPA-fallback: onbekende paden krijgen index.html (de router toont de 404).
# =404 aan het eind: zonder index.html (lege map) een 404 in plaats van een
# doorverwijzingslus die nginx als 500 afbreekt
location / {
${block(' ', ['add_header Cache-Control "no-cache" always;'])}
try_files $uri $uri/ /index.html =404;
}
}
`;
}
// nginx op de reverse-proxy-Pi (test): alleen HTTP. certbot voegt daarna
// HTTPS en de redirect toe (certbot --nginx -d test.andreaslop.nl). Geen
// security headers hier: die zet de hosting-Pi al, dubbel geeft dubbele headers.
// Intern adres van de hosting-Pi (waar het testblok en WordPress draaien)
const hostingPiAddress = '192.168.1.103';
export function nginxProxyConf(): string {
return `# ${generatedNotice}
# Reverse proxy voor de testsite naar de hosting-Pi. certbot past dit bestand
# aan voor HTTPS; kopieer het daarna dus niet opnieuw zonder certbot opnieuw te
# draaien.
server {
listen 80;
server_name test.andreaslop.nl;
location / {
proxy_pass http://${hostingPiAddress};
# Verplicht: de hosting-Pi kiest op basis van de hostname tussen het
# testblok en WordPress
proxy_set_header Host $host;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Real-IP $remote_addr;
}
}
`;
}
export const serverConfigFiles = {
'deploy/iis/web.config': iisWebConfig,
'deploy/nginx/andreaslop-test.conf': nginxTestConf,
'deploy/nginx/test.andreaslop.nl.proxy.conf': nginxProxyConf,
} as const;