Skip to content Skip to sidebar Skip to footer

Why Won't The Client Receive New Versions Of This Script In The Public Folder?

In my project there is a public folder and a script inside it: public/worker.js, which contains a piece of code: alert('foo'); I call this script using a Worker: new Worker('worke

Solution 1:

You should alter the response header for the file. Maybe this gets you going: Explicit HTTP Response Headers for files in Meteor's public directory


Solution 2:

The script is cached and the browser does not pull the new version from the server.

We need to edit the header of the requests for the files in the /workers folder, using the following code server-side (I wrapped it in a package with api.use('webapp')):

WebApp.rawConnectHandlers.use('/workers', function(req, res, next) {
  res.setHeader('cache-control', 'must-revalidate');
  next();
});

Using WebApp.connectHandlers did not work, the callback was never called, so I used rawConnectHandlers instead.

I am not 100% sure it is the best way to go, but it works.


Solution 3:

I've not found exactly why, but browsers (at least Chrome) seem to treat worker scripts differently to other Javascript files with regards to caching on refresh of the page, even if the headers sent from the server are the same. Refreshing the page makes the browser check for new scripts referenced in script tags, but not those used as a worker.

The way I've fixed this is that at build time, I include a version number/build time/md5 of the file contents in the file name, so it will end up something like worker.12333.js. The advantage of this is that if each filename references a file that essentially immutable, you can set far-future expires headers... So instead of telling the browser to never cache the worker script, it can cache it forever. https://github.com/felthy/grunt-cachebuster is one such tool that does this for Javascript included via script tags, but there are probably others.

The issue with this is that there must be some mechanism to tell the Javascript the updated filename, so it knows to call new Worker('worker.12333.js');. I'm not sure if existing available tools handle that, but the way I do it is to just use the project build time in seconds as the unique key for all the files

<html build-time="12333">
  ...

and then access it via Javascript so it can work out the latest worker script filename. It's not perfect, but it's fairly simple. You can probably come up with other mechanisms depending on your requirements.


Post a Comment for "Why Won't The Client Receive New Versions Of This Script In The Public Folder?"