I have created a simple worker based on https://developers.cloudflare.com/workers/examples/alter-headers/ example and added "mydomain/*" route to it. Now when I try to access my domain, I get error 522. I have successfully replaced the whole response, but using await fetch(request) to get the original response always fails.
#Can workers alter requests made to Cloudflare pages?
20 messages · Page 1 of 1 (latest)
Putting a Worker in front of Pages is a bit buggy and not recommended - as a first step can you try retrying the most recent deployment on the Pages project?
Longterm we would recommend looking into Pages Functions for running Workers alongside Pages projects
Note also that if you want to statically alter headers for a path, you can use a _headers file on the Pages project: https://developers.cloudflare.com/pages/platform/headers
Redeployment didn't fix the problem for me.
I would very much like to use _headers, but I am using https://github.com/cloudflare/next-on-pages/ which doesn't support _headers
While building the application, directories static and functions are created automatically, but I don't see any way of placing _headers
I would need to either fork next-on-pages to implement support or I could use Direct Upload in which I can easily add a new file, but I need manually upload files on every deployment.
You would need to copy them into the output directory after the build is complete
I have no experience with next-on-pages however, it may be running a Function that causes _headers to not run
Yeah but next-on-pages is creating the output directory automatically after git push, so I can't just copy anything in there.
According to the repo above, the build command is npx @cloudflare/next-on-pages --experimental-minify and the output dir is .vercel/output/static
So you could theoretically change the build command to
npx @cloudflare/next-on-pages --experimental-minify && cp -v ./_headers ./.vecel/output/static/```
HOWEVER this probably wont actually work if next-on-pages is using Functions on every route, which I suspect it is
In which case you will likely have to modify next-on-pages to suit that need
Running a regular Worker on top of Pages is the kind of unsupported thing where I can't really help a whole lot, one workaround to it causing 522s in the past was retrying the most recent Pages deploy after publishing the Worker but if that doesn't work then there isn't a lot to be said
Copying _headers during build works well 🙂 Thank you very much
Ah nice! Glad to hear!
@finite vessel Could you please help me with one more thing? Headers generally work ok, but they are never applied to the index page. I have tried following paths:
X-Test: 1
https://:project.pages.dev/
X-Test: 2
https://:project.pages.dev/*
X-Test: 3
https://:build.tklm.pages.dev
X-Test: 4
https://::build.tklm.pages.dev/
X-Test: 5
https://:build.tklm.pages.dev/*
X-Test: 6
/
X-Test: 7
*
X-Test: 8
/*
X-Test: 9
/index
X-Test: 10```
Headers 3 & 9 are showing up on other resources, but nothing is applied to index.
As I alluded to earlier, they won't apply on pages that a Pages Function is running on, so likely that's the case?
Ah, I see. In such case, I will apply them to the page function directly. Thank you