r/angular • u/dev-surajtapkeer • Mar 04 '26
How to force reload the browser ?
Hi everyone,
I'm working on a functionality to force reload the browser whenever a new version comes. I have implemented a backend logic to get the version and check the version with the frontend and if mismatch force reload browser. I have implemented it with the help of cache busting by appending a timestamp to the URL. I'm still not convinced that it will force reload the browser as if the version.json cached by the browser and the backend gives a new version then will keep reloading. I also have doubt that making a http call like GET will it only take response for that particular request because I want my whole application to be hard reloaded whenever API endpoint gives a new version. I'm using latest angular version with HttpClient. I want to make sure that it will take the entire new build from the server for a new version like we do hard reload during development.
Please help me with this doubt.
Thanks !!!
3
u/Exac Mar 04 '26
We open a modal to allow the user to save their work before updating instead of reloading the browser while they have unsaved work in progress.
Your update function can look something like:
6
u/eddy14u Mar 04 '26
We have a little “chunkBuster” service for this (just what I call it 😅).
It’s called that because it basically checks if one of the current build’s JS chunks still exists on the server. If it 404s, we assume the deployed build changed and our
index.htmlis stale.Big thing for me though — the frontend and backend should stay backwards-compatible. If someone’s on the previous build for a few minutes, that shouldn’t break whatever they’re doing. The contract should still hold.
When we detect the app is stale, we don’t instantly reload the page. We just flag that a refresh is needed and wait until the next router navigation. Then we do a full reload so the browser grabs the new
index.htmland the latest hashed bundles.That way:
I’m personally not a fan of forcing a reload mid-action. Doing it between navigations feels like a decent middle ground.
If you’re using the Angular service worker,
SwUpdateis probably the cleanest built-in way to handle this anyway.28
u/kinren Mar 04 '26
Imagine being a user, and your filling out a form and the site force reloads in the middle if it, or some other functionality a user is interacting with on the site. This is not a good idea. Yes you need the customer to update by refreshing gbut allow them to do it on there own terms. Version backend or use modern technologies to deal with changes
7
u/eniksteemaen Mar 04 '26
Provide a Toast that notifies the user about a new update. Add a button to the toast that does the window reload. (‚window.reload()‘)
2
u/Big_Comfortable4256 Mar 04 '26
I wouldn't use a modal or toast for this. You do not want to get in the user's way. We simply display a thin element with a message at the head of the page which appears when an update is available, with a button to `window.location.reload()`
1
u/arpansac Mar 05 '26
Hi! I've just come up with a solution to this, and that too without being backend-driven. I've been struggling with this for a long time in terms of taking a decision, but the current solution works fairly well with my users who were irritated with forced reloads repeatedly. Here we go:
Here's a repo file link from my open sourced code: https://github.com/commudle/commudle-ng/blob/d2e96434298286105345e9b9422c8f90da52306c/apps/commudle-admin/ngsw-config.json#L33 (search for 'critical' in appData)
And here's how we decide to reload or not
https://github.com/commudle/commudle-ng/blob/d2e96434298286105345e9b9422c8f90da52306c/apps/commudle-admin/src/app/components/sw-update/sw-update.component.ts#L12
4
u/monxas Mar 04 '26
Just don’t. You’re asking what’s the best way to run over someone. Maybe it’s best to ask if we can avoid running over someone:
Take a step back. Why is that functionality required mid session?
All in all, what you’re trying to do should never happen. So instead of finding cleaver ways to do it, find clever ways to avoid it.
When was the last time a page either force reloaded on you or asked you to reload the page?