user2093576
user2093576

Reputation: 3092

How does versioning of JavaScript files work?

As per few of the answers for the question asked, how to force browser to reload the JS on refresh,

Add a ? to the script's src link.

For instance:

 <script type="text/javascript" src="myfile.js?1500"></script>

or

<script type="text/javascript" src="myfile.js?v1"></script>

I just want to know how does it work? Do i need to do any changes in my JS file as well so that it gets compatible with the new version added? if not how does it reload JS everytime with values like src="myfile.js?1500" ?

Upvotes: 0

Views: 4533

Answers (2)

Medet Tleukabiluly
Medet Tleukabiluly

Reputation: 11930

You need a server that would resolve file version, here's a simple implementation

directory structure

 - index.js
 - node_modules/
 - version0.js
 - version1.js
 - version2.js

index.js

var express = require('express');
var app = express();

app.get('/myfile.js', function(req, res){
  var version = req.query.v || 0;
  res.sendFile(__dirname + '/version' + version + '.js');
});

app.listen(3000, function(){
  console.log('listening 3000');
});

Demo at https://glimmer-crow.hyperdev.space/myfile.js?v=1
change the v query param

Hypderdev source https://hyperdev.com/#!/project/glimmer-crow

Upvotes: 0

Mario Santini
Mario Santini

Reputation: 3003

It is just a trick, you pass a query string that tell to the browser he have to reload the resource from the server.

It doesn't matter what is after the question mark ? just has to be different each time the page reloads.

You don't have to change anything on your server side, as the query string can be ignored.

Upvotes: 1

Related Questions