Worker is executed twice for each browser reload
Hello!
I'm trying to make a Cloudflare worker in VS Code, but I'm running into a weird behavior with it.
My worker has a "fetch" function in "index.ts" which I understand is the entry point for when the worker is called.
Inside of that function, I'm calling some functions that fetch more resources and then call an API with a POST request (also with fetch) to process that data.
I noticed during my debugging in VS Code (with an attached debugger on the dev server) that my worker is actually being called twice per reload after I reload it in the browser. When debugging, after the first request is completed, the cursor jumps back to my breakpoint in index.ts and executes the entire code again. After that is done, it doesn't happen again until I reload my browser to try again.
The worker being executed twice is a problem for me because it results in unnecessary API calls and traffic.
Has anyone experienced something like this before and can tell me why this happens and how to fix it?
3 Replies
There is a request for /favicon.ico.
So that executes everything again?
Can I disable loading the favicon?
That only happens when you visit in a browser and is due to browser behaviour. Calling it from curl or fetch or similar wont do this
You can add code to your worker to return a dummy response on /favicon.ico, or if you return html you can specify a favicon in html to stop the browser trying to find one elsewhere https://www.w3schools.com/html/html_favicon.asp
Thank you! I'll try to add a dummy response.