16

I know you can guide Chrome to prefetch links you think users are likely to click on your site, but can you also do the inverse? Can you tell Chrome (or really any browser) not to prefetch and prerender your site?

Is there a tag or other way I can tell browsers that pre-fetching links from the currently viewed page shouldn't be done?

5
  • 1
    Curious, why would you want this?
    – Martijn
    Commented Jul 5, 2016 at 9:19
  • @Martijn One case that I can see is when your site contains time-dependent or highly dynamic content where a delay between the page rendering and when the user actually sees it matter. Commented Mar 5, 2017 at 9:50
  • @Martijn Another reason is if your page is a monster that takes forever to load. We have an internal "rebuild search index" page that takes minutes. I could see chrome prefetching it, invalidating my tests, especially while restarting the webserver. Commented Feb 26, 2022 at 2:54
  • I wonder if requiring a POST request (like from a form) would work in my case. No browser should ever prefetch that! Commented Feb 26, 2022 at 2:57
  • 1
    @GlenPeterson Please tell me you have functionality like that behind a logic?! And better, an command instead of a page, some secure background task
    – Martijn
    Commented Feb 27, 2022 at 21:37

3 Answers 3

12

Chrome and Safari send an X-Purpose: preview HTTP header when pre-fetching/rendering web content. [Source]

Firefox sends a similar header called X-moz: prefetch. [Source]

To block pre-fetching, you could return a 404 response when such headers are detected, as suggested by Peter Freitag in this blog post. He recommends adding these lines to .htaccess to block Firefox prefetching:

RewriteEngine On
SetEnvIf X-moz prefetch HAS_X-moz 
RewriteCond %{ENV:HAS_X-moz} prefetch 
RewriteRule .* /prefetch-attempt [L]

You can extend this to block Firefox, Safari, and Chrome prefetching like this (untested, but should work):

RewriteEngine On
SetEnvIf X-moz prefetch HAS_preview 
SetEnvIf X-Purpose preview HAS_preview
RewriteCond %{ENV:HAS_preview} .
RewriteRule .* /prefetch-attempt [L]
7

Google Chrome does not send any special headers to prerender requests anymore. See:

1

Above answer did not work for me. What did work, however, was this:

RewriteEngine On
SetEnvIfNoCase X-Forwarded-For .+ proxy=yes
SetEnvIfNoCase X-moz prefetch no_access=yes

# block pre-fetch requests with X-moz headers
RewriteCond %{ENV:no_access} yes
RewriteRule .* - [F,L]

From: askapache.com

The [F] flag returns a 403 Forbidden status code to the browser, while the [L] indicates that rule should be the last rule to be processed.

Also, chrome does not seem to prefetch links any longer (at least, for the prev/next meta tags).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.