Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vue-cli production build - browser cache problem?

We are building e-commerce, we're using vue on front, and we decided it's the best to follow vue team recommendations, and start new project with vue-cli.

Our problem appears when we are trying to deliver new version to our clients. We are building new application, new files in dist/ folder appears, with new hashes in the name... aaanddd clients still has old version.

That's actually the weirdest part, that browser is somehow caching our code, despite of new hashes O.o

Do anybody had similar problem? And any idea how to solve this?

like image 709
BigKamil5 Avatar asked Oct 29 '19 09:10

BigKamil5


People also ask

What is wrong with Vue JS front end?

This is not about service workers or PWA and the caching headaches that come along with it. The problem that we faced is with more of a vanilla Vue JS front end, not able to refresh itself whenever a new deployment happens. Let me try to explain it in a more lucid way.

Why does my browser cache my files?

This is happening because our browsers are smart enough to cache files that will be potentially useful in the future. Caching is a process of storing commonly-used data in a quick to access memory so it can be used immediately after its requested.

Is it possible to build a PWA with vuejs?

VueJS PWA: Cache Busting. Building a PWA has become a lot easier… | by Gerard Lamusse | Vue.js Developers | Medium Building a PWA has become a lot easier with the help of plugins and libraries such as @vue/cli-plugin-pwa however, though they set up a service-worker and manifest file they don’t handle updates so well.

Where can I find the index file in Vue CLI?

but Cache-Control: no-cache it is in responses to requests. When you do a build, Vue CLI will generate a file called index.html. It’ll be in your dist folder. Open it up and have a look. It should look like the one you posted earlier but with a few modifications, including the hashed asset paths.


Video Answer


1 Answers

Assuming this is nothing to with service worker/PWA, the solution could be implemented by returning the front end version by letting the server return the current version of the Vue App everytime.

axiosConfig.js

axios.interceptors.response.use(
  (resp) => {
    let fe_version = resp.headers['fe-version'] || 'default'
    if(fe_version !== localStorage.getItem('fe-version') && resp.config.method == 'get'){
      localStorage.setItem('fe-version', fe_version)
      window.location.reload() // For new version, simply reload on any get
    }
    return Promise.resolve(resp)
  },
)

Full Article here: https://blog.francium.tech/vue-js-cache-not-getting-cleared-in-production-on-deploy-656fcc5a85fe

like image 163
bragboy Avatar answered Oct 19 '22 22:10

bragboy