Worker uploaded via API: fetch vs service worker
I have been upoading workers via the api using this API: https://developers.cloudflare.com/api/operations/worker-script-upload-worker-module
There are 2 types of runtime APIs when using workers, I think they are referred to as "fetch" and "service worker". What I mean is that I can write my worker like this:
Or:
I think the first one is preferred (fetch). However, when I upload to the API, only the the other one works (service worker). I'm therefore assuming that the service worker API is what is actually being run, it is a lower level thing. The fetch functionality is added by wrangler when it builds. So when uploading via the API we have to use the service worker API. Which is totally ok, just want to check if this is the case?
Cloudflare API Documentation
Interact with Cloudflare's products and services via the Cloudflare API
1 Reply
Hi, I share your confusion around this topic, there are about half a dozen different way to upload a worker, and the best part is that the
upload worker module
endpoint only accepts "service worker" syntax (btw the other one with export default
is usually referred to as "module worker" in the docs. The runtime can understand both forms natively, you just need to use a different endpoint for uploading module workers: https://developers.cloudflare.com/api/operations/worker-script-put-content - hint: you do not need to provide either of the headers shown in the api docs, if you include a json part called metadata
in the request body.