* @param {Request} req * @param {{ ASSETS: { fetch: typeof fetch } }} env * @param {ExecutionContext} ctx * @returns {Promise }
(req, env, ctx)
| 39 | * @returns {Promise<Response>} |
| 40 | */ |
| 41 | async fetch(req, env, ctx) { |
| 42 | if (!origin) { |
| 43 | origin = new URL(req.url).origin; |
| 44 | await initialized; |
| 45 | } |
| 46 | |
| 47 | // skip cache if "cache-control: no-cache" in request |
| 48 | let pragma = req.headers.get('cache-control') || ''; |
| 49 | let res = !pragma.includes('no-cache') && (await Cache.lookup(req)); |
| 50 | if (res) return res; |
| 51 | |
| 52 | let { pathname, search } = new URL(req.url); |
| 53 | try { |
| 54 | pathname = decodeURIComponent(pathname); |
| 55 | } catch { |
| 56 | // ignore invalid URI |
| 57 | } |
| 58 | |
| 59 | const stripped_pathname = pathname.replace(/\/$/, ''); |
| 60 | |
| 61 | // files in /static, the service worker, and Vite imported server assets |
| 62 | let is_static_asset = false; |
| 63 | const filename = stripped_pathname.slice(base_path.length + 1); |
| 64 | if (filename) { |
| 65 | is_static_asset = |
| 66 | manifest.assets.has(filename) || |
| 67 | manifest.assets.has(filename + '/index.html') || |
| 68 | filename in manifest._.server_assets || |
| 69 | filename + '/index.html' in manifest._.server_assets; |
| 70 | } |
| 71 | |
| 72 | let location = pathname.at(-1) === '/' ? stripped_pathname : pathname + '/'; |
| 73 | |
| 74 | if ( |
| 75 | is_static_asset || |
| 76 | prerendered.has(pathname) || |
| 77 | pathname === version_file || |
| 78 | pathname.startsWith(immutable) |
| 79 | ) { |
| 80 | res = await env.ASSETS.fetch(req); |
| 81 | } else if (location && prerendered.has(location)) { |
| 82 | // trailing slash redirect for prerendered pages |
| 83 | if (search) location += search; |
| 84 | res = new Response('', { |
| 85 | status: 308, |
| 86 | headers: { |
| 87 | location |
| 88 | } |
| 89 | }); |
| 90 | } else { |
| 91 | // dynamically-generated pages |
| 92 | res = await server.respond(req, { |
| 93 | platform: { |
| 94 | env, |
| 95 | ctx, |
| 96 | context: ctx, // deprecated in favor of ctx |
| 97 | // @ts-expect-error webworker types from worktop are not compatible with Cloudflare Workers types |
| 98 | caches, |
no test coverage detected