A 25-line utility for forcing a page past a stale cache. Call cachebuster.clearCache()
and it appends a fresh, one-time query parameter, reloads, then quietly cleans the URL back up.
window.cachebuster
no dependencies
<0.3 KB min
Live demo — simulated asset cache
Console
how it works
No service worker, no cache-control headers to manage, no server changes. It leans entirely on the fact that a new URL is, by definition, not yet in any cache.
?cachebuster= param and strips it via history.replaceState so it never lingers or gets shared.clearCache() sets that param to a fresh crypto.randomUUID() and calls location.replace(), forcing every cache layer to treat it as a brand-new URL.autoclear attribute to any <script> tag on the page and it clears on first load automatically.Include the script, then call the global whenever you need a guaranteed-fresh load — for example after deploying a fix.
<script src="https://cdn.jsdelivr.net/gh/AetherCodez/CacheBuster.js/cachebuster.min.js"></script> <script> // call this whenever a hard refresh is needed document.querySelector("#refresh-btn").addEventListener("click", () => { cachebuster.clearCache(); }); </script> <!-- or, clear automatically on every load: --> <script src="cachebuster.min.js" autoclear></script>
| Member | Detail |
|---|---|
| window.cachebuster.clearCache() | Appends a fresh random ID as ?cachebuster= and reloads via location.replace(). |
| autoclear attribute | Add to the script tag to call clearCache() once, automatically, on first load. |
| URL cleanup | The param is stripped with history.replaceState immediately after the busted load completes. |