Frontend + backend in same worker or separate?

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 ( 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?

Thanks in advance

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.

1 Like

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.

1 Like

I’m keeping them separate due to cost mainly, depends entirely on your use case though.

1 Like

1 MB limit per worker.

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).