Get Cloudflare Pages to route all requests that do not match a static resource

I understand that I can configure Cloudflare Pages to serve some URLs using static content, but have other URLs routed to Functions. But to configure this, I need to specify which URLs I want to handle with Functions.

Is it possible to forward to an origin server all requests that do not match a static resource? Is there any way to set this up?

If above is not possible, is it possible to call a Function for all requests that do not match a static resource?

There is, but it requires some manual integration.

You can use Advanced Mode with Functions to route any files that don’t match a static asset (env.ASSETS.fetch won’t return an asset).

Just keep in mind that Cloudflare Pages is not a file locker: if you are serving a “disporportionate amount of images or videos” in relation to HTML content (origin server content doesn’t count) you’re violating the ToS.

Thank you @mcfadyeni. This should serve my use case perfectly. I am trying to staticify a WordPress website, but I want to keep comments, forums and admin dashboard to still work, all of which require dynamic functionality.

What scares me a little bit in this setup is that requests to static assets will also go through _worker.js, so that if plan limit is exceeded, the whole website will become inaccessible, not just dynamic functionality. Is there a way to mark _worker.js as “fail open”, like with normal Cloudflare Workers?

I do not expect normal site traffic to come anywhere close to the limit (about 1000 visitors per day, with 2-3 page views per visitor and about 30 HTTP requests per page view), but a simple attempt to mirror the website would quickly put me above the threshold.

1 Like

Unfortunately not really: with Workers they’re technically “on top of” an origin so that’s why you can fail open but with Functions, even with Advanced Mode, it would be you manually forwarding the request inside the Function (they’re not placed “over top of” an origin the same way).

Got it. On the plus side, this setup will let me avoid serving the website from projectname.pages.dev domain and only serve it from my custom domain name. If _worker.js sees a request to pages.dev, it can just return a 404.