On this page
A cache-busting URL lets you deploy a changed asset without waiting for a browser or CDN to discard its previous cached response. The two common approaches are changing a version query string, such as app.js?v=2, and changing the filename to include a content hash, such as app.a1b2c3d4.js.
This comparison explains how each approach interacts with cache keys and long-lived Cache-Control policies, and how to avoid breaking visitors who still have older HTML.
Key Takeaways
- Both approaches can work if every cache treats each versioned URL as a distinct cache key.
- Hashed filenames make the asset version visible in the path and are usually a good fit for build-generated frontend assets.
- A query-string version does not automatically preserve old bytes: the origin must serve the appropriate version on a cache miss.
- Long-lived caching is safe only when a versioned URL continues to represent the same asset bytes.
- Keep old assets available while older HTML, open tabs, and service workers may still reference them.
How cache keys affect cache busting
A cache key determines which stored response can satisfy a request. Browsers normally distinguish resource URLs by path and query string; a fragment such as #section is not sent in the HTTP request and is not a useful asset version. Browser caches may also be partitioned by site context, and response selection can depend on request headers named by Vary.
A CDN may use a configurable key. For example, it might include the whole query string, include selected parameters, or ignore queries. Forwarding a query parameter to the origin and including it in the cache key are separate decisions. If the CDN ignores v, these URLs can map to the same cached object:
/assets/app.js?v=1
/assets/app.js?v=2
In that configuration, changing v does not reliably bust the CDN cache. Check the actual key configuration at every caching layer, including any reverse proxy in front of the origin. Header-based variants, such as those involving Accept-Encoding, must also be handled correctly.
Advertisement
Query string vs. filename hash
| Consideration | Versioned query string | Content-hashed filename |
|---|---|---|
| Example | /assets/app.js?v=2 | /assets/app.b7c8d9e0.js |
| Version changes when | The version parameter is updated | The build emits a new filename for changed content |
| CDN requirement | The relevant query parameter must be in the cache key | The path must be in the cache key |
| Old-version availability | Requires the origin to map old versions to old bytes | Requires old named files to remain deployed |
| Operational fit | Useful when filenames are difficult to change | Well suited to build pipelines that produce asset manifests |
Hash examples in this article are illustrative identifiers, not calculated digests. A real build should calculate its hashes from the asset content and use a sufficiently long hash to make accidental collisions acceptably unlikely.
Versioned query strings
A deployment can update its HTML from /assets/app.js?v=1 to /assets/app.js?v=2. This is straightforward when a template or application controls the version parameter.
The subtle problem is origin behavior. Many static servers ignore the query string when selecting a file. If /assets/app.js is replaced during deployment, a cache miss for ?v=1 may receive the new file instead of the original one. Different caches could then hold different bytes under the same versioned URL.
For reliable long-lived caching and rollback, configure the origin to serve each version's original bytes, or use a deployment layout that retains a separate immutable asset for each release. Ensure the CDN both distinguishes the version in its key and forwards whatever the origin needs to select the right bytes.
Content-hashed filenames
A build can write a new path whenever an asset's content changes, then update the HTML or asset manifest to reference it. Unchanged assets can keep their existing paths. Because the version is in the path, the approach does not depend on the CDN including a query parameter in its key.
The filename alone does not enforce immutability. Your deployment must avoid overwriting an existing hashed path with different content, and must retain older files for as long as they may be requested. Use the asset manifest generated by the same build as the HTML so their references stay consistent.
Cache-Control policies
For a genuinely immutable versioned asset, an origin might send:
Cache-Control: public, max-age=31536000, immutable
max-age=31536000 permits a one-year freshness lifetime. immutable tells supporting caches that a fresh response is not expected to change. The same policy can be used for either URL style, but only if that URL continues to serve the same content. A long lifetime makes accidental reuse of a URL harder to repair; a CDN purge does not generally clear browser caches.
HTML needs a different policy because it points to the current assets. One common choice is:
Cache-Control: no-cache
no-cache allows storage but requires validation before reuse. With correctly maintained validators such as ETag or Last-Modified, a server can respond with 304 Not Modified when the stored HTML is still current. CDN-specific rules can override or supplement origin headers, so confirm the effective policy rather than assuming the origin header is the whole configuration. no-cache is not the same as no-store.
Advertisement
Deployment example: an asset changes
Suppose release A contains:
index.html -> /assets/app.a1b2c3d4.js
A later build changes the JavaScript and produces release B:
index.html -> /assets/app.b7c8d9e0.js
A safe rollout is:
- Upload
app.b7c8d9e0.jsand confirm it is available through the asset-serving path. - Publish release B's HTML, which references the new filename.
- Keep
app.a1b2c3d4.jsavailable for clients still using release A's HTML. - Retire old assets only after a retention period appropriate to your HTML caching, CDN behavior, service-worker lifecycle, open tabs, and rollback needs.
Publishing HTML before its new asset is available can produce failed requests. Some caches may also retain an error response, so check error-caching settings if a temporary missing asset persists after upload.
Revalidating HTML reduces stale references but does not eliminate them: an open page may request a lazy-loaded chunk much later, and a service worker may serve an older application shell. Test upgrades and rollbacks with those cases in mind. If an old asset is removed too soon, a reload may recover the visitor, but it is not a substitute for retaining compatible assets.
The equivalent query-string rollout uses app.js?v=1 and app.js?v=2. It needs the same ordering and retention guarantees, plus an origin mapping that can return the correct bytes for either version on a cache miss.
Verify your deployment
Use an asset you control and inspect both versions through the public CDN endpoint:
curl -sS -D - -o /dev/null 'https://example.com/assets/app.js?v=1'
curl -sS -D - -o /dev/null 'https://example.com/assets/app.js?v=2'
Replace the example URLs with your own. Inspect Cache-Control, Age, ETag, and any provider-specific cache-status headers when present. Then fetch the bodies and compare them with your expected build artifacts; headers alone do not prove that the CDN returns the correct version. Repeat after deployment and after an origin cache miss, and test from a fresh browser session as well as an existing one.
Browser and CDN documentation could not be consulted for this article. Cache-key defaults, cache-status headers, purge behavior, and configuration options vary by provider and version; verify them against your browser and CDN's current documentation before relying on a particular setup.
Common Mistakes
- Changing a query parameter while the CDN ignores it in the cache key.
- Giving mutable content a long freshness lifetime and expecting a CDN purge to fix browsers.
- Overwriting the file behind an old query version, so a cache miss returns new bytes under an old URL.
- Deleting old chunks immediately after publishing new HTML.
- Assuming
no-cacheprevents storage, or that it can update an already open page. - Ignoring service-worker caches, CDN overrides, or cached error responses when debugging stale content.
Best Practices
- Prefer build-generated hashed filenames for frontend assets when your tooling supports them; use query versions when their cache-key and origin behavior are explicitly controlled.
- Deploy new assets before the HTML that references them, and retain old assets for a defined rollback and client-compatibility window.
- Use long-lived caching only for immutable versioned URLs. Make HTML revalidation and CDN overrides deliberate.
- Keep secrets out of URLs: query strings and paths can appear in logs and other request metadata.
- Test the complete path from browser to CDN to origin, including old URLs, cache misses, rollbacks, and lazy-loaded assets.
Conclusion
Hashed filenames and versioned query strings both bust caches when the version changes the effective cache key. Hashed filenames usually make immutable deployments simpler; query strings require particular care with CDN key configuration and origin version retention.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.