Jotley / website / cloudflare-worker.mjs
| 1 | import handler from './.open-next/worker.js'; |
| 2 | import config from './vercel.json'; |
| 3 | |
| 4 | const rules = config.headers; |
| 5 | |
| 6 | export default { |
| 7 | async fetch(request, env, ctx) { |
| 8 | const url = new URL(request.url); |
| 9 | const consent = rules.find(rule => |
| 10 | rule.source === url.pathname && |
| 11 | rule.has?.some(condition => condition.key === 'choice' && url.searchParams.get('choice') === condition.value) |
| 12 | ); |
| 13 | // OpenNext serves public assets without running Next's query-dependent headers. |
| 14 | const response = consent ? await env.ASSETS.fetch(request) : await handler.fetch(request, env, ctx); |
| 15 | const headers = new Headers(response.headers); |
| 16 | for (const { key, value } of consent?.headers ?? []) headers.set(key, value); |
| 17 | |
| 18 | if (!consent && response.status === 200 && !url.pathname.startsWith('/_next/')) { |
| 19 | const cacheSource = url.pathname.startsWith('/media/films/') |
| 20 | ? '/media/films/(.*)' |
| 21 | : /^\/media\/(clips|shots)\//.test(url.pathname) |
| 22 | ? '/media/(clips|shots)/(.*)' |
| 23 | : '/(.*)'; |
| 24 | for (const { key, value } of rules.find(rule => rule.source === cacheSource).headers) headers.set(key, value); |
| 25 | } |
| 26 | |
| 27 | return new Response(response.body, { status: response.status, statusText: response.statusText, headers }); |
| 28 | }, |
| 29 | }; |