An accident happened and a certain HTML page was set to be cached by browsers. We do not want to change the URL of that page because other parties rely on that URL (including SEO).
Is there a way to make as many clients as possible reload that page? Clients are often coming to that page when browsing around on the site so we are often able to send them some fresh JavaScript (or any other resource) before they hit the problematic page. Maybe there is a JavaScript trick to force a reload?!
3
Answers
You can add timestamp as querystring at the end of your url:
You can invoke AJAX request with no cache option to not change the url and not refreshing the page either:
Looking for a JavaScript solution?
Full HTML solution
Furher reading for adding these headers from server-side: https://stackoverflow.com/a/2068407/3958365
Code snippet below will invalidate cache entry of the page with URI equal to
uriOfCachedPage
:You need to inject this code to script or HTML page that your users will probably get.
For more information check XMLHttpRequest page on MDN and Request Cache-Control Directives section in RFC 7232 Hypertext Transfer Protocol (HTTP/1.1): Caching.
You also can store
cachedPageReloaded
flag in cookies or localStorage and invalidate cache entry only if it wasn’t invalidated before. Check Document.cookie and Window.localStorage pages on MDN.Reload Page With TimeStamp Parameter Added to URL to Prevent Cached Versions From Being Loaded
Below is a function I use to reload an html page every time it is visited which appends a timestamp as a “version” parameter to the URL ensuring that it is never a cached copy of the page being retrieved.
This method adds a parameter to the URL, but it doesn’t “change” it. It is the same method often used to carry over data from a form by adding the form data parameters to the end of the url.
Prevent Cached External .js Files
To ensure you are serving up-to-date javascript, that is included via script tags, from external .js files, you can use the jQuery getScript() function, which you can read about HERE. It adds a timestamp parameter to the end of the resource URL (not the actual page’s URL), so that your external .js files being loaded are always current.
Example Usages: