Reputation: 2000
I've noticed on many sites the JavaScript file has it's regular name then ?v=something
The something can be alphanumeric, or just random numbers.
For example:
<script src="./bundle.js?v=21knfa"></scrip>
I have a few questions about this:
What is the purpose of adding ?v=something
on the file name. For example, does it allow the developers to have multiple versions of their code?
Is this done by the server or the client?
How would you add the version automatically to your JavaScript filename, does it involve a software like Docker to do this? Or is this done in another way.
Any help would be appreciated.
Upvotes: 1
Views: 2753
Reputation: 1056
It is for client-side caching.
If you have a JS file script.js, the users browser will cache it. When you change script.js, the browser will still have the old cached version.
So, you add a ?v=...
to it so when you make changes, the browser will load the new one.
Since it is in the HTML file, it is the server that sets the ?v=...
To do this yourself, simply add the ?v=...
<script src="javascript.js?v=1.0.1" type="text/javascipt"></script>
Say you make a script for your site and name it "script.js".
In your code you put:
<script src="script.js?v=1" type="text/javascipt"></script>
Now when you change "script.js", you change your code as follows:
<script src="script.js?v=2" type="text/javascipt"></script>
Now your visitor's browsers will get the new "version" of your script file.
You could use ?foo=...
or really any other string.
Upvotes: 4