October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PCOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Set Cache Headers for Versioned JavaScript Files

Set a long cache lifetime on JavaScript URLs that change with file contents, and use no-cache for the HTML entry document that references them.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

For JavaScript files whose URL changes every time their contents change, use a long cache lifetime such as Cache-Control: public, max-age=31536000, immutable. Keep the HTML document that points to those files revalidatable—typically Cache-Control: no-cache—so browsers can discover new filenames after a deployment.

Set a long lifetime only for truly versioned files

A cache identifies a response by its URL. If you publish changed JavaScript under the same URL, a browser or intermediary may keep serving the old cached response until it becomes stale. Instead, make each content change produce a new URL, commonly with a hash in the filename, such as app.8f31c2.js. See MDN’s HTTP caching guide for how versioned URLs separate cached versions.

For a static asset whose URL will never serve different contents, a typical response header is:

Cache-Control: public, max-age=31536000, immutable

max-age=31536000 is a one-year freshness interval, not a measured performance result. MDN uses it as an example in its Cache-Control reference. The immutable directive indicates that the fresh response need not be revalidated. Do not use this long-lived policy if a deployment can overwrite the contents at that same URL. For a stable URL, use a shorter freshness lifetime or require revalidation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

What each directive does

  • max-age=31536000 allows a stored response to be reused while fresh for one year after it is received.
  • immutable signals that the representation will not change while fresh; it is appropriate only when the URL-to-content mapping is enforced by deployment.
  • public permits shared caches to store a response in cases where other rules might otherwise restrict it. It is commonly used for public static assets, but avoid it for personalized or authorization-sensitive responses unless shared caching is intentional.

Keep the HTML entry document revalidatable

The HTML document is different from the hashed JavaScript file: its stable URL must eventually point clients to the latest asset filename. A common policy is:

Cache-Control: no-cache

no-cache does not mean “do not store.” It allows storage but requires a cache to validate the response before reusing it. By contrast, no-store tells caches not to store the response at all. The distinction is documented in the MDN Cache-Control reference.

Use validators for revalidation, not as a substitute for versioning

For a stable URL such as the HTML entry document, an ETag or Last-Modified validator can make revalidation efficient. When the cached copy is stale, the client can ask whether it has changed; if it has not, the server may respond with 304 Not Modified rather than retransmitting the body. Validators do not replace changing the JavaScript URL when its contents change. MDN explains this process in its HTTP caching guide.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Deployment checks

  1. Configure the build or release process so every JavaScript content change produces a new filename or versioned URL.
  2. Set the long-lived asset policy on those versioned files. Include public only when shared caching is safe for the response.
  3. Set the HTML entry document to Cache-Control: no-cache; add ETag or Last-Modified where practical.
  4. Check the actual response headers delivered to clients, including behavior at any CDN or managed cache. These layers can have product-specific controls and cache keys.
  5. If a file must be removed or corrected urgently, do not assume changing origin headers clears copies already stored in intermediate caches; purge the relevant managed cache as needed.

The underlying HTTP caching rules are specified in RFC 9111.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. Shenzhen desk3 min
    HONOR Expands Beyond Smartphones With Humanoid Robot RevealHONOR said it unveiled its first humanoid robot at MWC 2026 and named shopping assistance, workplace inspections, and supportive companionship as intended uses. Later Robotics D1 claims and a reported…
  2. Cupertino desk5 min
    Apple Unveils AirPods Max 2: The Upgrade That Should Have Happened Years AgoAirPods Max 2 adds H2-powered audio features and Apple claims up to 1.5× more effective ANC, but its design, Smart Case, and 20-hour battery rating are unchanged. Wired lossless audio…
  3. Cupertino desk4 min
    Apple’s OLED Touch MacBooks Are Coming—but the Dynamic Island Is the Real GambleApple has not announced an OLED touchscreen MacBook, but reports point to high-end models arriving in late 2026 or early 2027. The reported Mac Dynamic Island could be useful, but…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.