I’m trying to perform an asynchronous write to Workers KV using event.waitUntil() to avoid the put operation blocking the content response to the browser and therefore reduce latency
The problem is that I keep getting this error “ReferenceError: event is not defined at u (worker.js:1:4773)” and I don’t know why.
I’m sure there is a really obvious reason why this isn’t working but I can’t figure it out so I would be very grateful if someone could help!
Here are the relevant sections of code, edited for brevity:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request)
// For easier debugging, exception stack traces are returned in body
// This MUST be removed/disabled in production!
.catch(e => new Response(templateHeader(HEADERATTRIBUTES)
+ '<h1>Sorry, that page is temporarily unavailable</h1><p>'
+ e.stack + '</p>'
+ templateFooter(), {
status: 500,
statusText: "Internal Server Error",
headers: GLOBALHEADERS }))
);
});
…
async function handleRequest(request) {
event.waitUntil(WORKERS_KV.put(sessionCookie,
JSON.stringify(sessionValues),
{expirationTtl: 1209600}));
// Redirect to next step in sequence
response = new Response(null, {
status: 303,
statusText: "See Other",
headers: GLOBALHEADERS
});
// Return response to browser
return response;
};
Code results in error: ReferenceError: event is not defined at u (worker.js:1:xxxx)