import handler from './.open-next/worker.js'; import config from './vercel.json'; const rules = config.headers; export default { async fetch(request, env, ctx) { const url = new URL(request.url); const consent = rules.find(rule => rule.source === url.pathname && rule.has?.some(condition => condition.key === 'choice' && url.searchParams.get('choice') === condition.value) ); // OpenNext serves public assets without running Next's query-dependent headers. const response = consent ? await env.ASSETS.fetch(request) : await handler.fetch(request, env, ctx); const headers = new Headers(response.headers); for (const { key, value } of consent?.headers ?? []) headers.set(key, value); if (!consent && response.status === 200 && !url.pathname.startsWith('/_next/')) { const cacheSource = url.pathname.startsWith('/media/films/') ? '/media/films/(.*)' : /^\/media\/(clips|shots)\//.test(url.pathname) ? '/media/(clips|shots)/(.*)' : '/(.*)'; for (const { key, value } of rules.find(rule => rule.source === cacheSource).headers) headers.set(key, value); } return new Response(response.body, { status: response.status, statusText: response.statusText, headers }); }, };