Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the "?" sign mean in a request for a static JS file?

Tags:

javascript

url

I've seen that a lot and I just don't know what it means. This, for example:

<script src="http://server.com/file.js?y=2345678" type="text/javascript"></script>

If it is in deed possible to 'catch' the value of 'y' in the javascript file, how would that be?

Thank you.

PS. I know what mod_rewrite is and that is not the answer, just in case :)

like image 990
Aerozeek Avatar asked Feb 20 '23 01:02

Aerozeek


2 Answers

This is to force the browser not to cache the file, by making it believe that it is a dynamic file with get parameter rather than a static one.

like image 125
3on Avatar answered Feb 21 '23 16:02

3on


This is often used to facilitate caching of the JS file. You set a far-future Expires header which means the browser may cache it for a very long time. If you change something in the file you also update the number in the querystring, which will make the browser refetch the file. This works because caching is for unique filenames and the querystring is part of the filename (as far as the browser is concerned).

A similar approach to this is to use rewrite rules in the web server to have some part of the file name which it doesnät care about. Here's a Nginx rule to show what I mean:

rewrite ^/style\..*\.css$ /style.css;

I use this rule to have filenames like style.42750cad6.css, which always points to the file style.css. The text in the middle is changed whenever I change style.css. The difference between the first approach is that this does not use the querystring so the caching will work in more browsers.

like image 43
Emil Vikström Avatar answered Feb 21 '23 17:02

Emil Vikström