Ameer
Ameer

Reputation: 2000

What is ?v=something mean in a JavaScript file name?

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:

The purpose:

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?

Server or Client

Is this done by the server or the client?

How would this be done

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

Answers (1)

Seth B
Seth B

Reputation: 1056

The Purpose

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>

Example

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.

Also

You could use ?foo=... or really any other string.

Upvotes: 4

Related Questions