Closed jamesarosen closed 2 years ago
Before modules syntax, that's exactly what I did. Upstream, I would just embed the event (not pretty, but functional):
addEventListener('fetch', event => {
event.request.event = event
event.respondWith(router.handle(event.request))
})
...and with the new(er) CF modules syntax, they natively send request, env
params to handlers, which works perfectly fine with itty (you can pass ANY params after the request to the handle
function, and those will be passed to all handlers.
const router = Router()
router.get('/', (request, env) => {
// now have access to the env (where CF bindings like durables, KV, etc now are)
})
export default {
fetch: router.handle // because the handle signature matches CF, nothing more is needed
}
Hope this helps! Let me know if you have issues with it!
There are some middleware use-cases that rely on the
FetchEvent
. For example, here's a Cloudflare middleware that normalizes theUser-Agent
, caches based on the normalized UA, and instructs downstream caches to vary onUser-Agent
:Is the recommended approach to attach the
FetchEvent
to theRequest
so upstream handlers have access to it?