ENGINEERING JOURNAL

Cache-Busting CSS and JS on Shared Hosting Without a Build Step

Cache busting CSS and JS on shared hosting without a build step: version assets with filemtime and set far-future cache headers.

THE ARTICLE 01 — 02

You push a CSS fix to your site, reload the page, and nothing changes. You reload again. Still the old layout. Then a visitor emails you about a bug you fixed yesterday. The problem is not your deploy. It is the browser cache, and on shared hosting without a build step, you have to bust it by hand.

Cache busting CSS and JS on shared hosting means giving the browser a reason to fetch the new file. The usual trick is a version query string, but there are a few details that decide whether it actually works. Here is the setup I use on a no-framework PHP site running on LiteSpeed shared hosting.

Why the browser keeps the old file

Browsers cache static assets aggressively, and shared hosts usually send long cache headers for CSS and JS. That is good for performance and bad for deploys. When the URL is identical, the browser serves its cached copy and never asks the server. Your new file can sit on the server for days while visitors keep seeing the old one. The fix is to change the URL every time the file changes, so the browser treats it as a new resource.

The filemtime query string

The simplest reliable method is to append the file's last-modified time as a query string. In PHP, filemtime() returns that timestamp, so the version updates itself whenever you edit the file. No build step, no manual version numbers to remember.

<?php
function asset_url(string $path): string {
    $file = __DIR__ . '/' . ltrim($path, '/');
    $version = is_file($file) ? (string) filemtime($file) : '0';
    return '/' . ltrim($path, '/') . '?v=' . $version;
}
?>
<link rel="stylesheet" href="<?= asset_url('assets/css/main.css') ?>">
<script src="<?= asset_url('assets/js/main.js') ?>" defer></script>

Every edit changes the timestamp, which changes the URL, which forces a fresh download. When the file does not change, the URL stays the same and the browser keeps using its cache. You get the best of both: instant updates on deploy, full caching between deploys.

Set long cache headers so the trick pays off

Cache busting only helps if unchanged files are actually cached. Add far-future expiry headers for your static assets in .htaccess so the browser does not revalidate on every page load:

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType text/css "access plus 1 year"
    ExpiresByType application/javascript "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
    <FilesMatch "\.(css|js)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
</IfModule>

The immutable directive tells the browser the file at that exact URL will never change, so it skips even conditional requests. That is safe here precisely because the version query string changes whenever the content does. I covered the rest of the deployment side in my deployment checklist for PHP sites on shared hosting.

The query string trap: some caches ignore it

There is one caveat worth knowing. A few older proxies and CDNs ignore query strings when deciding whether two URLs are the same resource. In practice, browsers respect the query string, and that covers the case that actually bites you: a returning visitor with your old CSS in their browser cache. If you ever sit behind a CDN that strips query strings, switch to filename versioning instead, for example main.20261114.css, and rewrite the versioned name back to the real file with mod_rewrite. For a plain shared hosting site, the query string approach is enough.

Verify it with curl before you call it done

After deploying, check the headers yourself instead of trusting a browser reload:

curl -sI https://example.com/assets/css/main.css?v=1763097600 | grep -i -E 'cache-control|expires'
curl -sI https://example.com/assets/css/main.css | grep -i -E 'cache-control|expires'

The first request should return the long max-age and immutable headers. Then change the CSS, reload the page, and confirm the ?v= value in the page source changed. If the value is unchanged, your deploy did not actually update the file, and that is a different problem to chase.

When a visitor still sees the old site

Cache busting fixes your assets, not your HTML. If the HTML page itself is cached, the visitor never sees the new asset URLs at all. Keep HTML cache headers short or absent, and never send immutable on HTML. If a specific visitor is stuck, a hard refresh (Ctrl+Shift+R) clears their browser cache for the page, but that is a support answer, not a deployment strategy. The strategy is: versioned asset URLs, long cache headers on assets, short cache headers on HTML.

The takeaway

You do not need a build step to cache-bust CSS and JS on shared hosting. Append filemtime() as a query string, set far-future cache headers on static assets, keep HTML cache short, and verify the headers with curl after each deploy. It is a small amount of code that ends the "I deployed but nothing changed" class of bug reports for good.

Muhammad Aqib ← ALL ARTICLES
KEEP EXPLORING

More ideas.
Better engineering.

Explore the journal ↗