Hi! New Cloudflare worker user here. Trying to understand what is better. I have a front end react application and a backend api. Is it recommended to have them both running on the same worker or separate worker?
I’m leaning towards having separate workers and using routes (Routes · Cloudflare Workers docs) so that requests to “/api” will route to the api worker and other requests will serve the react app. If I go this way, how can I nicely run “wrangler dev” so that everything is running as if it were from the same domain?
It’s kind of up to you, either can work, but I think I prefer having them in the same worker. Use the worker site starter then just peel off your /api routes into your api logic.
Keep in mind, that significantly larger worker may take longer to load on the first run. If you are counting microseconds, you may want to separate. In most cases, the difference is insignificant, though.
The new upcoming API (still in Beta) will allow you to use ES2020 modules, with dynamic import (await import), which means dealing with multiple granular files & load code only when needed (fast execution).
As you don’t need anymore to transpile or to pack, you will save size and since you can cut your code into modules, it is perfectly doable to use only one worker for front and back-end (1MB of ES2020 is a lot of code).