Configure workers to run on a path(s) currently served by CF Pages?Any Plans to Merge Pages & Workers Sites?

I understand from Workers Site and Pages - #2 by Judge that CF Pages is essentially a more convenient, less configurable version of CF Workers Sites. My question, however, is whether there’s a way to still use workers, but register a route(s) to be served by a worker, on the same domain as pages. I tried this and wrangler let me register a route on my pages domain, but try as I might, I couldn’t ever get it to not 404.

If this doesn’t work and isn’t a feature that’s going to be implemented, I certainly understand. In that case, however, can that be documented and can it fail when I try and register the route?

More broadly speaking, prior to the post I linked above, I didn’t understand the relationship between the two products, and there is scant documentation that discusses the differences and pros/cons.

Regardless, I’m using pages and workers (on a subdomain) and just would like to say … thank you very much for the great dev experience and overall good products! It’s truly a breath of fresh air.

I have been looking at this too. Like having an /api route under the same domain, and have that do the dynamic bits via a Worker. The main page would be handled by Pages. Because it is static.

As I read the product evolution, Workers came first, which were all dynamic. Then Worker Sites was added (I guess because people used Workers for static sites/SPA). And two years later came Pages which are (currently) just static/SPA. And not at all dynamic. Kind of replacing Worker Sites but not, at least not yet - given the lack of dynamic functionality.

Interestingly I found https://developers.cloudflare.com/pages/how-to/add-custom-http-headers which proves you can run a Worker on the same domain as Pages but (seemingly) not what you and I want which is to use a path/route within it. So it seems to me like it is /* only for now and you have to use a separate domain if you want to run a Worker on a path like /api.

At least for now.

Update: looking through even more docs, actually it does seem to be possible already to use both Pages and Workers together, with the Worker on a route. See: https://developers.cloudflare.com/pages/tutorials/build-an-api-with-workers#custom-domains

Now, when you run wrangler publish , your API will be published and served on myblog.com , but only when you visit a route matching /api* – this means that your basic routes ( / , /posts/:id , etc.) will be sent to your front-end Pages application, but any API routes will be intercepted and handled by your Workers application.

1 Like

@gregsanderson I saw that too, and I think it’s a reasonable approach but it’s unclear that/if this only works with /*.

That said, I couldn’t get even /* to work the other day but I’ll try again tomorrow. It might make more sense for me to just switch to workers sites - doing so seems like it would go more with the grain.

Yeah, I agree it’s not clear.

Using Worker Sites will work though. Just means the Worker will then handle all requests, even requests it does not need to, as they are not dynamic.

1 Like

Good to know. I do like the idea of having control over the “static” server. Can’t do that on any of the other static site providers that I know of. This would also give me an opportunity to merge code for my link shortener and the server that serves my actual static site. Thanks for talking through this with me @gregsanderson

1 Like