I have a PHP application / website served by nginx. When I navigate around the website using Google Chrome with the dev tools network panel open I can see just prior to navigating to a new page URL page there is a prefetch request, that the server returns with code 503 (not available).
CloudFlare is being used as a proxy.
I'm concerned about these 503 errors. Is an nginx configuration required to handle prefetch requests properly?
Hello fellow developers!
I had the same issue and upon further investigation I found out that this is a Beta feature in Cloudflare, called "Speed Brain". It instructs the browser to initiate a pre-fetch request on any future navigation.
I haven't figured out how to fulfill the pre-fetch request for it to be successful, so I disabled the feature for now since it isn't important to me at this point. There are probably additional headers to be set on your servers response to instruct Cloudflare to cache the document.
While Cloudflare states the following:
i saw in my logs 2 consecutive requests in the same second, the first one being from Cloudflare which had the header "sec-purpose: prefetch", indicating that the request somehow got through. Maybe I don't understand everything or this is a result of the feature still being Beta (bugs).
The request caused issues on our side because we mistook the pre-fetch for an actual customer request.
In PHP you can catch such requests by looking the globals in $_SERVER for the header like this:
In our code, we also bypassed the problematic sections triggered by the pre-fetch request, since it's enabled by default on free plans. I believe this isn't ideal for a beta feature. However, you can always disable this option, as shown in the picture.
More on this:
https://developers.cloudflare.com/speed/optimization/content/speed-brain/
https://www.cloudflare.com/press-releases/2024/cloudflare-speed-brain-make-millions-of-web-pages-faster/
Regards!