Quick revision
The short answer
We turn on Spring's content version strategy so every static file is served under a URL that contains a hash of its content, let Thymeleaf rewrite asset links to those URLs, and give assets a one-year public Cache-Control. HTML is served with Cache-Control: no-cache and an ETag, so browsers always fetch the current page, which points at the current asset URLs. Old and new scripts can then never share a URL, so a browser can no longer combine new HTML with an old cached script.
What to remember
In this answer, we saw that the broken page came from old and new scripts sharing one URL. We enabled spring.web.resources.chain.strategy.content so each asset is served under a hash of its content, let Thymeleaf's @{...} links be rewritten through ResourceUrlEncodingFilter, or used ResourceUrlProvider elsewhere, and gave assets max-age=365d and public. HTML is rendered through controllers with Cache-Control: no-cache and an ETag, so browsers always get the current page and therefore the current asset URLs.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We turn on Spring's content version strategy so every static file is served under a URL that contains a hash of its content, let Thymeleaf rewrite asset links to those URLs, and give assets a one-year public Cache-Control. HTML is served with Cache-Control: no-cache and an ETag, so browsers always fetch the current page, which points at the current asset URLs. Old and new scripts can then never share a URL, so a browser can no longer combine new HTML with an old cached script.
Why this question is asked
The broken-page-after-release bug is one of the most common front-end problems, and it is entirely a caching problem. Users who visited yesterday have app.js in their browser cache; after the release, the HTML expects functions that only the new app.js has, and the page breaks until the cache expires or the user forces a reload. Interviewers like this question because the fix is a well-known pattern, cache busting with content-hashed file names, and Spring Boot supports it with a few properties if we know where they are. In this answer, we will reproduce the problem, enable content versioning, make templates emit the versioned URLs, set the caching headers for assets and for HTML, and then explain why the combination makes the bug impossible.
1Reproducing the Problem
Suppose the application serves /js/app.js from src/main/resources/static with a Cache-Control lifetime of one day, a common default someone set long ago. On Monday a user loads the page and the browser stores app.js for 24 hours. On Tuesday morning we release: the HTML now calls a new function, initCheckout(), that exists only in the new app.js. The user comes back, the browser fetches the new HTML (or revalidates it), sees a script tag for /js/app.js, finds a fresh copy in its cache and uses Monday's file.
The root of the problem is that two different versions of the file share one URL, and the browser has no way to know the URL now means something else. Shortening the cache lifetime only narrows the window and slows every page. The real fix is to give each version its own URL.
2Enabling Content Versioning in the Resource Chain
Spring MVC has a resource chain, a sequence of resolvers that sits in front of static files. One of them, the version resolver, can add a version to each file's URL. With the content strategy, the version is an MD5 hash of the file's bytes, so app.js is served as app-5d41402abc4b2a76b9719d911017c592.js, and the hash changes only when the file changes. In Spring Boot we turn it on with two properties.
The resolver works in both directions. When a template asks for the URL of /js/app.js, it returns the hashed name. When a browser requests /js/app-5d41...592.js, the resolver strips the hash, finds app.js, checks that the hash matches the current content and serves it. So we do not rename any files in our build; Spring computes the names at runtime.
3Making Templates Emit the Versioned URLs
Hashed URLs only help if the HTML uses them. With Thymeleaf, we write links with the @{...} link expression, and Spring Boot registers a ResourceUrlEncodingFilter when the resource chain is enabled. That filter hooks into URL encoding so every @{/js/app.js} is rewritten to the hashed name as the page is rendered.
For other template engines or for building URLs in Java code, we inject ResourceUrlProvider and call getForLookupPath("/js/app.js"), which returns the same hashed path. A plain src="/js/app.js" written by hand bypasses all of this and brings the bug back, so it is worth a quick search of the templates for unversioned asset links.
4Serving HTML So It Is Always Revalidated
The assets can now be cached for a year, but the HTML must not be, because the HTML is what tells the browser which asset URLs are current. We serve pages with Cache-Control: no-cache, which lets the browser keep a copy but makes it check with the server before every use, and an ETag, so an unchanged page costs a 304 without a body. A ShallowEtagHeaderFilter computes the ETag from the response body.
But there is a catch. The cachecontrol properties apply to everything served from the static locations. If an index.html sits in src/main/resources/static, it will also get the one-year lifetime and the bug returns in a worse form. So HTML should be rendered from templates through controllers, as above, and never left as a static file under the long-lived cache settings.
5Why This Fixes the Bug
After the change, Tuesday's HTML refers to /js/app-<new hash>.js. The browser revalidates the HTML because of no-cache, gets the new page, sees a URL it has never stored and downloads the new script. Monday's script is still in the cache under its old URL, but nothing asks for it any more. So the browser can no longer combine new HTML with an old script, because the versions have different names.
Notice the extra benefit of hashing by content. A file that did not change between releases keeps the same hash and URL, so users keep their cached copy across releases and only download what actually changed.
8 messages in order. Play them, or step through them one at a time.
Changed files get new URLs; unchanged files keep theirs and stay cached.
6Follow-up questions
Why is versioning by file content better than by application version?
With an application version, such as /v2.4.1/js/app.js, every release changes every asset URL, so users download all assets again even if only one file changed. Content hashing changes only the URLs of files whose bytes changed. It is also safer: a hash cannot be forgotten, while a manually bumped version can be, and two instances that serve the same file always produce the same name.
What happens during a rolling deployment?
For a short time, an old instance may receive a request for a new hashed URL it cannot serve, or the other way round, and return 404. Common remedies are sticky sessions during the rollout, uploading assets to a CDN or object storage before switching traffic so all versions stay available, or keeping the previous release's assets alongside the new ones for a while.
Should the hashed assets also be served from a CDN?
It is a natural next step. Because the URLs never change meaning, a CDN can cache them for a year without any purging, which takes almost all asset traffic off the application servers.
7Conclusion
In this answer, we saw that the broken page came from old and new scripts sharing one URL. We enabled spring.web.resources.chain.strategy.content so each asset is served under a hash of its content, let Thymeleaf's @{...} links be rewritten through ResourceUrlEncodingFilter, or used ResourceUrlProvider elsewhere, and gave assets max-age=365d and public. HTML is rendered through controllers with Cache-Control: no-cache and an ETag, so browsers always get the current page and therefore the current asset URLs.
Open with
The bug happens because the new HTML and the old app.js share one URL, so the browser has no idea its cached script is out of date. The fix is to give every version of an asset its own URL and then cache assets forever while always revalidating HTML.
Then, if they want more
In Spring Boot I would set spring.web.resources.chain.strategy.content.enabled=true with paths=/**, which makes Spring serve each static file under a name containing an MD5 hash of its content, like app-5d41...js.
Thymeleaf links written as @{/js/app.js} are rewritten to the hashed name by ResourceUrlEncodingFilter, which Spring Boot registers when the resource chain is on; in other templates or Java code I would use ResourceUrlProvider. Then I set spring.web.resources.cache.cachecontrol.max-age=365d and cache-public=true for assets, which is safe because a changed file gets a new URL.
For HTML I render through controllers with Cache-Control: no-cache and an ETag from ShallowEtagHeaderFilter, so the browser checks every time and gets a cheap 304 if nothing changed, and I make sure no index.html sits in the static folder with the one-year setting.
After a release, the browser gets the new HTML, sees new asset URLs and downloads only the files that changed.
About this answer
- Last updated
- 13 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Configure Spring Boot so static assets get content-hashed URLs and long-lived caching...": https://scrivio-interview-library.netlify.app/architecture/caching-strategies/c3f462a262/