===== worker/index.ts ===== import { PRODUCTION_HOST } from '../src/site'; import { MOTION_ARM_SCRIPT } from '../src/scripts/motion-arm'; interface Env { ASSETS: { fetch(input: Request | URL | string): Promise }; } let motionArmHash: Promise | null = null; async function sha256(text: string): Promise { const digest = await crypto.subtle.digest('SHA-256', new TextEncoder().encode(text)); return btoa(String.fromCharCode(...new Uint8Array(digest))); } async function securityHeaders(): Promise> { motionArmHash ??= sha256(MOTION_ARM_SCRIPT); const csp = [ "default-src 'self'", `script-src 'self' 'sha256-${await motionArmHash}'`, "style-src 'self'", "img-src 'self' data:", "font-src 'self'", "connect-src 'self'", "base-uri 'self'", "form-action 'self'", "frame-ancestors 'none'", "object-src 'none'", ].join('; '); return { 'Content-Security-Policy': csp, 'Strict-Transport-Security': 'max-age=63072000; includeSubDomains', 'X-Content-Type-Options': 'nosniff', 'Referrer-Policy': 'strict-origin-when-cross-origin', 'Permissions-Policy': 'camera=(), microphone=(), geolocation=(), browsing-topics=()', }; } export default { async fetch(request: Request, env: Env): Promise { const url = new URL(request.url); const isProduction = url.hostname === PRODUCTION_HOST; if (url.pathname === '/robots.txt' && !isProduction) { return new Response('User-agent: *\nDisallow: /\n', { headers: { 'Content-Type': 'text/plain; charset=utf-8', 'X-Robots-Tag': 'noindex' }, }); } // A direct request for the not-found page is a miss too, or it would be served as an indexable 200. const isNotFoundPage = url.pathname === '/404' || url.pathname === '/404.html'; let response = isNotFoundPage ? null : await env.ASSETS.fetch(request); if (!response || response.status === 404) { const notFound = await env.ASSETS.fetch(new URL('/404', url)); response = new Response(notFound.body, { status: 404, headers: notFound.headers }); } else { response = new Response(response.body, response); } // A raster image runs nothing, and a CSP on it breaks the browser's own image viewer (inline styles); SVG keeps it. const type = response.headers.get('Content-Type') ?? ''; const isRaster = type.startsWith('image/') && !type.startsWith('image/svg'); for (const [name, value] of Object.entries(await securityHeaders())) { if (!(isRaster && name === 'Content-Security-Policy')) response.headers.set(name, value); } // Static assets serve .txt as bare text/plain, which browsers read as windows-1252: the proof documents are UTF-8. if (type === 'text/plain') response.headers.set('Content-Type', 'text/plain; charset=utf-8'); if (!isProduction) response.headers.set('X-Robots-Tag', 'noindex'); if (url.pathname.startsWith('/_astro/')) { response.headers.set('Cache-Control', 'public, max-age=31536000, immutable'); } return response; }, }; ===== src/site.ts ===== // Production apex. Every other host gets noindex (worker/index.ts). export const PRODUCTION_HOST = 'elio-pallois.fr'; ===== src/scripts/motion-arm.ts ===== // Inlined in by Base.astro; worker/index.ts hashes this exact string for the CSP. // Arms the reveal and loader CSS only if IntersectionObserver exists, and disarms it if app.ts has // not started within 3 s (blocked or failed script): content is never left hidden. export const MOTION_ARM_SCRIPT = "(()=>{const h=document.documentElement;if(!('IntersectionObserver' in window))return;h.classList.add('motion-armed','is-first-load');setTimeout(()=>{if(!h.classList.contains('motion-live'))h.classList.remove('motion-armed','is-first-load')},3000)})()"; ===== wrangler.jsonc ===== { "$schema": "node_modules/wrangler/config-schema.json", "name": "portfolio-bts-2027", "main": "worker/index.ts", "compatibility_date": "2026-10-01", "assets": { "directory": "./dist", "binding": "ASSETS", // "404-page" would bypass the Worker for browser navigations; the Worker serves 404.html itself. "not_found_handling": "none", // The Worker sets CSP, HSTS and X-Robots-Tag on pages, so it must see them first. "run_worker_first": true }, "routes": [{ "pattern": "elio-pallois.fr", "custom_domain": true }], "observability": { "enabled": true } }