Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React app : old version kept in cache until I inspect it

I used create-react-app to start working on a React app. By default, it uses service workers. I also happen to serve my production app with https.

It turns out that with this config, my browser just doesn't get new versions I deploy. It keeps on reloading old versions, even if I force a refresh. The only way I can get it to load the latest build is by inspecting the page (with Chrome). Then, when I force-refresh, the latest build is loaded.

I have disabled the service worker as it seemed to be the culprit, but I keep on bumping into this problem. I can't ask my users to each open the inspector in their browsers in order to get the latest build.

How can I find out how to fix this problem? Is there a create-react-app config I could change in order to make sure this does not happen anymore?

like image 690
Gabriel Theron Avatar asked Dec 12 '17 14:12

Gabriel Theron


People also ask

How do I uninstall old version React?

1 Check version of create-react-app, using npx create-react-app -V . 2 Uninstall any global version of create-react-app, using either npm uninstall -g create-react-app or yarn global remove create-react-app . 3 View the contents of your machine _npx folder, each folder inside represents a version of node installed.

Do I have to install React app again every time I start a new project?

You will have to install it separately for each project.


1 Answers

I've actually found what the problem was: the index.html was cached, and so it was pointing to old js files which were also cached.

The way I solved it was to add this config to an .htaccess file at the root of the app (although configuring apache through other means might also work):

<FilesMatch "\.(html)$">
  FileETag None
  <IfModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </IfModule>
</FilesMatch>

In the end, it was unrelated to the service worker. It only had to do with the way index.html was cached.

like image 118
Gabriel Theron Avatar answered Oct 18 '22 14:10

Gabriel Theron