What is it "?" does a sign mean in a request a static JS file?

I have seen this a lot and I just don't know what that means. This is for example:

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

If in fact you can "catch" the value of "y" in a javascript file, how will it be?

Thanks.

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

+4
source share
3 answers

This is often used to facilitate caching of the JS file. You set the Expires header for the future future, which means the browser can cache it for a very long time. If you change something in the file, you will also update the number in the query line, which will force the browser to restore the file. This works because caching is for unique file names, and the request is executed as part of the file name (as far as the browser is concerned).

A similar approach to this is to use rewriting rules on the web server to have some part of the file name that it does not care about. Here's a Nginx rule to show what I mean:

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

I use this rule to have file names such as style.42750cad6.css , which always points to a style.css file. The text in the middle changes when style.css changes. The difference between the first approach is that it does not use the request, so caching will work in more browsers.

+2
source

This forces the browser not to cache the file, believing that it is a dynamic file with a get parameter, not a static one.

+5
source

ok, as I see it in two ways.

  • it can be used to load js without caching
  • for each request to the server, the server can log information (if logging is enabled), if I use it for analytics, so I can use another parameter for locations and from the log, I can analyze and get the required data.
+1
source

Source: https://habr.com/ru/post/1436522/


All Articles