Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to instruct browsers to ignore GET parameters when caching a resource

Is there a way (an http header) to tell browsers not to distinguish between main.css and main.css?someparam=1 when performing caching.

According to w3c spec:

since some applications have traditionally used GETs and HEADs with query URLs (those containing a "?" in the rel_path part) to perform operations with significant side effects, caches MUST NOT treat responses to such URIs as fresh unless the server provides an explicit expiration time. This specifically means that responses from HTTP/1.0 servers for such URIs SHOULD NOT be taken from a cache

My Firefox 3.5.5 re-fetches the files from the server, if the GET parameter changes. If it doesn't change, it servers the content from the cache. (verified that with FireBug).

So, is there a way to cope with this (without removing the get parameter).

like image 213
Bozho Avatar asked Dec 09 '09 22:12

Bozho


People also ask

How do I force a browser to ignore cache?

In most web browsers you can force a one-time page load from the server by holding down the shift key while clicking on the Reload or Refresh button. Kyrnin, Jennifer. "Force Your Page to Always Load From the Server, Not the Web Cache." ThoughtCo, Sep.

Does browser cache GET request?

Overview. The HTTP cache stores a response associated with a request and reuses the stored response for subsequent requests. There are several advantages to reusability. First, since there is no need to deliver the request to the origin server, then the closer the client and cache are, the faster the response will be.

How does the browser know what resources to cache?

How Does the Browser Know What to Cache? The browser inspects the headers of the HTTP response generated by the web server. There are four headers commonly used for caching: ETag.

Which HTTP caching header is used to avoid making requests to the origin server?

Expiration is the caching mechanism by which a client can entirely avoid making requests to the origin server. When the origin server specifies an explicit expiration time in the resource, a cache can check that expiration time and respond accordingly without having to contact the server first.


1 Answers

It's not quite clear which side requires params in URL: server or client? Anywway, your server can behave so that the client deals with the same URL and won't need any magic headers that don't exist. ;)

If server requires params, you can add that parameter with Apache URL rewrite, if you use Apache:

RewriteRule ^style.css$ /style.css?param=1 [L]

There are other things Rewrite Engine can do, and you can even pass the params via cookies and extract them in RewriteCond lines into query string:

RewriteCond %{HTTP_COOKIE} a:(.*) # not sure of the syntax
RewriteRule ^style.css$ /style.css?a=%1 [L]

This way the URL will be the same on the client side.

If the client sends requests with params, and it can't work in a different way, you can put an external redirect and set a cookie:

RewriteCond %{QUERY_STRING} a=(.*)
RewriteRule ^(.*)$ /$1 [CO=a:%1:mysite.com,R]

This way the browser will consider caching/not caching of the URL without any parameters. Read Rewrite Engine docs, there are other useful features.

like image 148
culebrón Avatar answered Oct 16 '22 13:10

culebrón