Force cache refresh after deployment

Force cache refresh after deployment

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Force Cache Refresh Without Changing the URI

When a plugin deployment updates an HTML file served at example.com/page.html, you often face a stubborn reality: the browser will happily keep serving the stale version unless you change the URI or convince it to ignore the cached copy. If you cannot modify the URL—perhaps due to constraints in your legacy plugin architecture—the standard "append a timestamp" trick is off the table. You are left with the difficult task of clearing the client-side cache without user intervention, relying solely on server-side mechanisms.

The most reliable approach involves manipulating HTTP response headers to invalidate the cached resource at the protocol level. Specifically, you need to set the Cache-Control header to no-store and no-cache, while simultaneously ensuring the Last-Modified or ETag headers change with every deployment. When a browser receives these directives, it is forced to revalidate the content with the server before attempting to serve it from disk.

Here is how you implement this in an Apache environment using .htaccess. You must ensure the file is readable and located in the directory serving page.html:

<FilesMatch "page\.html$">
    Header set Cache-Control "no-cache, no-store, must-revalidate"
    Header set Pragma "no-cache"
    Header set Expires "0"
</FilesMatch>

This configuration instructs the browser to never cache page.html and to always verify freshness with the server. However, simply telling the browser "this is fresh" is not enough if the content itself hasn't changed in the eyes of the server. The Last-Modified header must reflect the actual timestamp of the file on the filesystem. If your build process updates the file but the web server caches its own internal metadata (common with mod_expires or specific caching modules), you may need to restart the service or use a versioning strategy like mtime.

If you are using PHP to serve the HTML, you can force an immediate cache bust by manipulating headers directly within the script. This is often more robust than relying solely on .htaccess rules, as it ensures the logic travels with the file content:

<?php
// At the very top of page.html before any output
header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache");
header("Expires: 0");
?>
<!-- Rest of the HTML content -->

This approach guarantees that every time the user requests page.html, the browser treats it as a new resource. The trade-off here is increased bandwidth usage and slightly higher latency on every load, as the browser cannot retrieve the file from its local disk. For a plugin deployed to 1000 users, this might seem inefficient, but in scenarios where you cannot update the URI, it is the only way to ensure consistency without forcing users to clear their manual cache settings.

Another consideration involves the resources loaded by page.html, such as script.js. Even if you successfully force a reload of page.html, the browser might still serve an old script.js from its cache, leading to JavaScript errors where new HTML DOM elements are manipulated by old logic. To mitigate this, apply similar Cache-Control directives to your assets or utilize query string parameters for those specific files if their URIs can be modified. If you cannot modify the URI of script.js either, you must ensure its content changes in a way that alters its ETag, forcing the browser to re-download it whenever page.html is accessed.

While server-side headers handle the immediate problem, long-term strategy should aim to minimize reliance on aggressive caching directives. A better architectural pattern is to version your assets and update the HTML source to reference those specific versions (e.g., script.v2.js). If you are working within the Laravel ecosystem, leveraging built-in asset management or considering a migration to a service that handles cache invalidation more gracefully might be a worthwhile long-term investment. For instance, checking out resources at https://laravelcompany.com can provide insights into modern patterns for managing deployments and asset pipelines without relying on brittle header hacks.

Ultimately, forcing a cache refresh without URI changes is a stopgap solution. It works by overriding browser heuristics with explicit instructions to always fetch fresh data, but it comes at the cost of performance and requires careful handling of dependent resources like JavaScript files to prevent state desynchronization.

Our network