New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow addEventListener in modules syntax #207
Comments
Hey! 👋 I'm having a hard time getting this to work. 😕 My test worker looks like: addEventListener("fetch", (event) => {
console.log("listener", event.request.url);
event.respondWith(new Response("listener body"));
});
export default {
async fetch(request, env, ctx) {
console.log("export", request.url);
return new Response("export body");
},
}; I'm expecting
|
I bumped into the same thing using a heavily modified version of @mrbbot's template using modules. Interestingly, works fine in miniflare, but not when miniflare has a debugger (vscode) attached. It dies at the cache.match step. /** @format */
export async function handleRequest(
request: Request,
env: Bindings,
ctx: ExecutionContext
) {
// Match route against pattern /:name/*action
const url = new URL(request.url);
url.host = "www.powershellgallery.com";
url.protocol = "https";
const target = url.toString();
const cache = caches.default;
const match = await cache.match(target);
if (match) {
console.log("hit");
return match;
}
const originResponse = await fetch(target);
// Must use Response constructor to inherit all of response's fields
const response = new Response(originResponse.body, originResponse);
// Cache API respects Cache-Control headers. Setting s-max-age to 10
// will limit the response to be in cache for 10 seconds max
// Any changes made to the response here will be reflected in the cached value
response.headers.append("Cache-Control", "s-maxage=3600");
// ctx.waitUntil(cache.put(target, response));
cache.put(target, response);
console.log("miss");
return response;
}
const worker: ExportedHandler<Bindings> = { fetch: handleRequest };
export default worker; Stacktrace
|
@JustinGrote I noticed the same and opened a separate issue: #292 |
@mrbbot Thank you! 💖 |
We now allow
addEventListener
anddispatchEvent
when using modules syntax. Therefore, we can remove the restriction that throws an error.Let's say you invoke
addEventListener("fetch")
. You will not receive Cloudflare's "fetch" event, but you can dispatch the event manually, such asdispatchEvent(new Event("fetch"))
.The text was updated successfully, but these errors were encountered: