What problem are we trying to solve?
Every time a build is created and deployed, the user has to do a hard refresh of the webpage to cle...
For further actions, you may consider blocking this person and/or reporting abuse
I don't understand the point of this article. This is done automatically already. The build system outputs the built files using a hash. The index.html uses those to load it. When you push new code up, the index.html is replaced with updated references. Set the index.html to never be cached and add the new build files alongside the old ones and people going to the site initially get the new version and people already on the page will continue to work and then when they refresh they get the new version as well.
When your project requires to implement strict security policies in some cases it is necessary to disable hashes, for this reason we need another option to clear all caches.
however I agree that this method could be shorter, in my opinion only need attach in the serviceWorker.js a console.log with current date
Hi Arturo
I'm extremely sorry for such a late reply. Can you help me out how I can make this code shorter ?
Of course, as I commented only need attach a console log in your service worker, for example:
Create a file updateBuildTime.js
then update your package file with
With these lines you can add a console.log, I have used the current date to ensure a new value is different to the last version, but you can use another value
The reason of this is very simple, own service worker update all cache our app but need at least one change to download the new version of this file, the console.log with a date is perfect, it will be diferent in each build.
Thanks Arturo, I will check this out.
Hi Eddie
I'm very sorry for such a late reply. The point of this article was, whenever I was generating the build, I was getting the same hash files through create-react-app. It seems to have changed now. I'm getting different has files generation during the build.
Can you share me some example on how to "Set the index.html to never be cached" only on new builds ? It will help us all
Awesome! Finally, I've found this solution. I wonder how every SaaS company could manage to work without this solution. I'm very curious why there has been no simple solution for this important problem in the React eco-system.
Hi Ammar great article, I need you help here, I tried Dinesh approach, but my app is indefinitely reloading, pls guide here, happy to share more info.
Hi Pragz, can you create a small poc with issue share it me via github. I will have a look at it and get back to you.
One thing to check is that my app is not build using create-react-app, so do not have any Service worker at all and hence not being registered, still the below code will be able to work, also my app uses react-router.
Also is it possible to share your email / twitter , for further 1-1 discussion, thanks for the support.
@Pragz : Please email me at ammartinwala52@gmail.com. We can connect over there
I ran the above code and it is working perfectly fine at my end.
Sent the referral mail!
I am also facing the same issue as it goes under the infinite loop please guide me how fix this issue
window.location.reload()
Hi Aman
I'm extremely sorry for such a late reply. If you are still facing the issue, please email to me your code at ammartinwala52@gmail.com
Can you please check once again your implementation? The react web app gets reload only because of withClearCache's useEffect. The code is never going in refreshCacheAndReload. And also, if intentionally, I wanna get the code fall in refreshCacheAndReload function, It lets the UI into never ending rerendering. Please check your implementation and revert me back.
FYI, your buildDate in package.json and meta.json are always going to be same and if they are going to be same, then if (momLatestDateTime.isAfter(momCurrentDateTime)) {
is always going to give you false.
Hi Vishal
I'm very sorry for such a late reply. Can you check the answer to Khorengrig that I just mentioned. It explained what exactly happens.
If you are still not clear then please email me ammartinwala52@gmail.com
Thanks again for this article. I implemented this approach, but the first time my page loads, I can still see the old version. After closing the tab and reopening it I can see the new version. Is this what is suppose to be happening?
Never mind! That happened the first time after the first change. Then every time a change something I can see the most recent version live!!! Thanks a million sir
Hi Alexis
I'm very sorry for such a late reply. I'm glad that I could be of help. Keep writing. Cheers.
Hi Ammar, Thank you so much for the post. I'm trying to get it to work for me but I'm getting the errors in the pic I provided. Can you please help me if you don't mind. Thanks again for the post!!!
Who can explain me what is going in this file. update-build.js. Every time you run build command your package.json buildDate and meta.json buildDate will be the same(You are inserting the same value). Which way this code will work?
Hi Khorengrig
Im very sorry for this very very late reply.
Both dates are generated at the same time, and when you generate your build and deploy, at that point of time the api call from meta.json will fetch the latest build date and it will be compared with package.json date. If package.json date is cached it will be a mismatch and then only the refresh will happen. If your server/deployment configuration is such that it always takes the latest build then your package.json buildDate and meta.json buildDate will always be the same.
I hope you got the point.
Looks like there is a react-cache-buster npm package that is basically the same as this. It can be found here: npmjs.com/package/react-cache-buster.
Hi Brian
I'm very very sorry for such a late reply. Thanks for sharing I will check that out.
It's calling API without login. How I can solve this problem
Hi Jasim
I'm very very sorry for late reply. You would have to place some kind of authentication check first if you want to call the api. If auth is successful then load the ClearCache component which in turns call the API.
How would you integrate the ClearCache component if App.js is a class Based component? Thanks in advance
Hi Alexis, you can refer to the App.js code and CacheBuster code in this article by Dinesh
dev.to/flexdinesh/cache-busting-a-...
Here the implementation is class-based component. Let me know if you have any queries.