0

We have a Vue.js application that is deployed in a docker container using nginx. The nginx configuration follows pretty much the Vue.js documentation here https://cli.vuejs.org/guide/deployment.html#docker-nginx

server {
    listen      80;
    server_name ourhost.com;
    charset utf-8;
    root    /var/www/ourhost.com/dist;
    index   index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location = /index.html {
        expires -1;
        add_header Pragma "no-cache";
        add_header Cache-Control "no-store, must-revalidate";
    }
}

Suppose the Vue.js app has a route https://ourhost.com/start-page

When calling the above url, nginx will try the file start-page in root, fails and servers index.html This will load the basic app javascript (ex. app.somehash.js), which then loads additional css or code junks.

That is all good, and the way it is supposed to work.

After a new deployment, the hashes of the code junks change. Suppose a user in on the page, having loaded the old code, then routes to a new part of the application, that needs loading of extra code junks, then he/she might load junks, that no longer exist.

The problem is, with the above nginx config, the content of index.html would be served for a non existing code junk. I would rather like a proper 404 error and the properly react on this error in the application (tell the user that a new version is out, an he/she should reload the page or else reload the page automatically).

I tried to not use try_files in the config. But this does not serve the index.html content when calling a route of the application directly. And I also don't want to have all possible routes of the Vue.js application in the nginx config.

And help or any ideas are very much appreciated.

  • Does this answer your question? [VueJS/browser caching production builds](https://stackoverflow.com/questions/45867357/vuejs-browser-caching-production-builds) – rémy Aug 05 '21 at 10:06

1 Answers1

0

honored to answer your first SO question ;) and also flagging it as a duplicate of VueJS/browser caching production builds rofl.

I would follow the approach in the edit section of the top answer:

We have a version.json that is incremented/defined at build time. Version number is included in manifest.json. The build bundle is automatically uploaded to S3. We then pass the manifest.json to the backend (we do this on an entry page in Admin area). We then set the "active" version on that UI. This allows us to easily change/revert versions.

The backend puts the "currentVersion" as a Response Header on all requests. If currentVersion !== version (as defined in version.json), then we ask the user to click to refresh their browser (rather than force it on them).

Meaning custom code in your app, that checks for the latest version, and if outdated, it reloads everything, so you have full control.

rémy
  • 1,026
  • 13
  • 18
  • Thank you @rémy I like that solution! I came up with my own solution in the meantime, that simply adds another location for image and javascript files in the nginx configuration. This location properly returns 404 if file does not exist. – Nils Samuelsson Aug 05 '21 at 11:12