35

ES6 modules allows us to create a single point of entry like so:

// main.js

import foo from 'foo';

foo()
<script src="scripts/main.js" type="module"></script>

foo.js will be stored in the browser cache. This is desirable until I push a new version of foo.js to production.

It is common practice to add a query string param with a unique id to force the browser to fetch a new version of a js file (foo.js?cb=1234)

How can this be achieved using the es6 module pattern?

spinners
  • 2,021
  • 1
  • 17
  • 31
  • 1
    Awesome question! Finally I've found a word of reason. Just thank you for asking! My main cocern against all those fancy ES Modules was cache busting. It'll fail on race conditions every time the site will get updated. And I love CommonJS for dynamic imports: `const a = require(b ? 'm1' : 'm2')`. `import a from (b ? 'm1' :'m2')` woun't work. – Brian Cannard Dec 23 '17 at 03:02
  • Did you manage to find a solution for this @spinners? I have the exact same problem! – distante Feb 07 '18 at 10:32
  • 1
    Have you figured out a solution to this? This issue is currently a significant problem for me and I am surprised that this currently seems to be the only question on the web about it. I have resorted to adding a "?v=x.x.x" to the end of every import statement in my Javascript files. So I have 'import foo from 'foo?v=1.x.x'; for all my modules. This is a really annoying solution because it means I modify hundreds of files in source control in every release, just because I have to modify the version number, even if no actual code in the file changed. Any info is greatly appreciated. – John Cleveland May 04 '18 at 04:05
  • 1
    ^^ that's what I ended up doing – spinners Sep 05 '18 at 13:18
  • 3
    Is there still no better way in 2020? I read the answer suggesting Etag, which is elegant, but it's not always possible to configure the server. – Christopher Martin Apr 09 '20 at 15:59
  • What's the problem in using `import foo from './foo.js?cb=1234';`? You should write it this way for native js anyway. – Miguel Pynto Mar 02 '21 at 11:35

8 Answers8

10

HTTP headers to the rescue. Serve your files with an ETag that is the checksum of the file. S3 does that by default at example. When you try to import the file again, the browser will request the file, this time attaching the ETag to a "if-none-match" header: the server will verify if the ETag matches the current file and send back either a 304 Not Modified, saving bandwith and time, or the new content of the file (with its new ETag).

This way if you change a single file in your project the user will not have to download the full content of every other module. It would be wise to add a short max-age header too, so that if the same module is requested twice in a short time there won't be additional requests.

If you add cache busting (e.g. appending ?x={randomNumber} through a bundler, or adding the checksum to every file name) you will force the user to download the full content of every necessary file at every new project version.

In both scenario you are going to do a request for each file anyway (the imported files on cascade will produce new requests, which at least may end in small 304 if you use etags). To avoid that you can use dynamic imports e.g if (userClickedOnSomethingAndINeedToLoadSomeMoreStuff) { import('./someModule').then('...') }

Riccardo Galli
  • 10,604
  • 5
  • 59
  • 58
  • 2
    Using `ETag` of course still leads to 1 HTTP request per module, so potentially a lot of overhead - adding `max-age` causes more problems, as modules expire individually, meaning someone might get incompatible module versions when you deploy updates. Cache busting with a content checksum in the filename only forces updates to changed modules, so that is the best strategy I know of - and probably the only strategy that lets you cache perpetually, with no HTTP overhead, and precise updates. Too bad we still have to use bundlers just to get proper caching. – mindplay.dk Nov 20 '20 at 09:31
7

There is one solution for all of this that doesn't involve query string. let's say your module files are in /modules/. Use relative module resolution ./ or ../ when importing modules and then rewrite your paths in server side to include version number. Use something like /modules/x.x.x/ then rewrite path to /modules/. Now you can just have global version number for modules by including your first module with <script type="module" src="/modules/1.1.2/foo.mjs"></script>

Or if you can't rewrite paths, then just put files into folder /modules/version/ during development and rename version folder to version number and update path in script tag when you publish.

Wanton
  • 650
  • 5
  • 8
  • 1
    IMHO this is the best option. New version just goes in a new directory, this way both old and new will work and be supported. Then when you decide it's time delete old versions. – php_nub_qq Sep 28 '20 at 05:35
2

From my point of view dynamic imports could be a solution here.

Step 1) Create a manifest file with gulp or webpack. There you have an mapping like this:

export default {
    "/vendor/lib-a.mjs": "/vendor/lib-a-1234.mjs",
    "/vendor/lib-b.mjs": "/vendor/lib-b-1234.mjs"
};

Step 2) Create a file function to resolve your paths

import manifest from './manifest.js';

const busted (file) => {
 return manifest[file];
};

export default busted;

Step 3) Use dynamic import

import busted from '../busted.js';

import(busted('/vendor/lib-b.mjs'))
  .then((module) => {
    module.default();
});

I give it a short try in Chrome and it works. Handling relative paths is tricky part here.

d-bro82
  • 410
  • 1
  • 6
  • 19
1

Just a thought at the moment but you should be able to get Webpack to put a content hash in all the split bundles and write that hash into your import statements for you. I believe it does the second by default.

Dávid Veszelovszki
  • 1,751
  • 18
  • 16
0

You can use ETags, as pointed out by a previous answer, or alternatively use Last-Modified in relation with If-Modified-Since.

Here is a possible scenario:

  1. The browser first loads the resource. The server responds with Last-Modified: Sat, 28 Mar 2020 18:12:45 GMT and Cache-Control: max-age=60.
  2. If the second time the request is initiated earlier than 60 seconds after the first one, the browser serves the file from cache and doesn't make an actual request to the server.
  3. If a request is initiated after 60 seconds, the browser will consider cached file stale and send the request with If-Modified-Since: Sat, 28 Mar 2020 18:12:45 GMT header. The server will check this value and:
    • If the file was modified after said date, it will issue a 200 response with the new file in the body.
    • If the file was not modified after the date, the server will issue a304 "not modified" status with empty body.

I ended up with this set up for Apache server:

<IfModule headers_module>
  <FilesMatch "\.(js|mjs)$">
    Header set Cache-Control "public, must-revalidate, max-age=3600"
    Header unset ETag
  </FilesMatch>
</IfModule>

You can set max-age to your liking.

We have to unset ETag. Otherwise Apache keeps responding with 200 OK every time (it's a bug). Besides, you won't need it if you use caching based on modification date.

Tigran
  • 1,945
  • 1
  • 15
  • 15
0

I've created a Babel plugin which adds a content hash to each module name (static and dynamic imports).

import foo from './js/foo.js';

import('./bar.js').then(bar => bar());

becomes

import foo from './js/foo.abcd1234.js';

import('./bar.1234abcd.js').then(bar => bar());

You can then use Cache-control: immutable to let UAs (browsers, proxies, etc) cache these versioned URLs indefinitely. Some max-age is probably more reasonable, depending on your setup.

You can use the raw source files during development (and testing), and then transform and minify the files for production.

Jeroen Versteeg
  • 313
  • 2
  • 12
-1

A solution that crossed my mind but I wont use because I don't like it LOL is

window.version = `1.0.0`;

let { default: fu } = await import( `./bar.js?v=${ window.version }` );

Using the import "method" allows you to pass in a template literal string. I also added it to window so that it can be easily accessible no matter how deep I'm importing js files. The reason I don't like it though is I have to use "await" which means it has to be wrapped in an async method.

Banning
  • 2,183
  • 2
  • 15
  • 19
-5

Use of relative path works for me:

import foo from './foo';

or

import foo from './../modules/foo';

instead of

import foo from '/js/modules/foo';

EDIT

Since this answer is down voted, I update it. The module is not always reloaded. The first time, you have to reload the module manually and then the browser (at least Chrome) will "understand" the file is modified and then reload the file every time it is updated.

Fifi
  • 2,450
  • 19
  • 37