- Clear the cache or force reload in the browser.
- Hit CTRL-F5 or CTRL-SHIFT-R in most modern browsers to force reload.
- Use the developer’s console to clear the cache.
- Do it programmatically.
- Change the file name of the script.
- Append a dummy timestamp to the source URL.
- Set an HTTP expires header to encourage browsers to reload the script.
But just how do these methods work exactly? Read on for more!
REAL QUICK TUTORIAL
TABLE OF CONTENTS
|Browser Reload||Programmatic Reload||Useful Bits & Links|
BROWSER FORCE RELOAD
All right, let us now get started with the ways to do a force reload within the browser itself first.
BROWSER FULL RELOAD PAGE
- In most modern browsers – Hold the
COMMANDon a Mac), then click on the reload button. That’s it, this will force a full page reload from the server, not use anything from the local cache.
- The “common shortcut key” to do force-reload is either
CLEAR CACHE IN DEVELOPER’S CONSOLE
Now, the following is only for the Google Chrome browser. But most other Chromium-based browsers should also have a similar feature. If the above hard reload somehow didn’t work, press
F12 to bring up the developer’s console. Right-click on the reload button, and choose “empty cache and hard reload”.
Alternatively, open the network tab (might need to reload the page first). There are 2 things you can do here.
- Right-click on any file, clear the browser cache.
- Or disable the cache entirely, then reload the page.
PROGRAMMATIC FORCE RELOAD
Of course, the above will only work for the developer. If the system is already live, here are a few ways to remedy the situation.
CHANGE THE SCRIPT NAME
<!-- OLD <link rel="stylesheet" type="text/css" href="STYLE.css"> <script src="SCRIPT.js"></script> --> <!-- NEW --> <link rel="stylesheet" type="text/css" href="STYLE-VERSION-2.css"> <script src="SCRIPT-VERSION-2.js"></script>
This is one of the easiest and most reliable ways to ensure that everyone is getting the latest version of the scripts – Simply change the file name, add a trailing version number behind the file name. It is also a good practice to adopt versioning anyway. We can quickly fall back to the older versions should something goes wrong, and check back on what has been changed over time.
APPEND A DUMMY TIMESTAMP
<link rel="stylesheet" type="text/css" href="STYLE.css?t=5566"> <script src="SCRIPT.js?t=123456"></script>
Yep, this is an old trick that we use – Just append a dummy timestamp behind the URL
SCRIPT.JS?t=123456. It really doesn’t do anything but fool the browsers into loading the script from the server every time.
But please take note that this is not a recommended practice in the long run, as browsers will not cache files with parameters. It slows down the performance of your websites, just stick with changing the file name if possible.
HTTP HEADERS – CACHE CONTROL & EXPIRES
QUICK NOTES ON THIS METHOD
Lastly, we can send some HTTP “no-cache” headers to encourage browsers to reload from the server. These examples are done in PHP and Apache, but there should be similar mechanisms in the other programming languages/webservers.
OUTPUT CACHE CONTROL HEADERS WITH PHP
On the page that you want to send the updated scripts, output the following HTTP headers:
<?php header("Cache-Control: no-cache, must-revalidate"); // HTTP 1.1 header("Pragma: no-cache"); // HTTP 1.0 header("Expires: Wed, 1 Jan 2020 00:00:00 GMT"); // Anytime in the past // REST OF THE PAGE AS USUAL ?> <!DOCTYPE html> ...
Very important note – This page is now set to “no-cache, always reload”. This will most likely cause the browser to reload everything on the page itself. For the sake of performance, you might want to turn off these headers after some time, after most of the users have gotten the updated page.
APACHE MOD EXPIRE
If the above is too funky for you, or there are too many pages to push at once – The next alternative is to enable the mod_expire extension in Apache. Just add a small snippet in the
USEFUL BITS & LINKS
That’s all for this tutorial, and here is a small section on some extras and links that may be useful to you.
INFOGRAPHIC CHEAT SHEET
LINKS & REFERENCES
- Tech tip: How to do hard refresh in Chrome, Firefox and IE? – FileCloud
- How to setup expire headers on Apache? – Digital Ocean
- Cache-Control – How to Properly Configure It – KeyCDN
- HTTP Caching – Google Developers
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!