Why this matters
Versioned URLs allow aggressive caching without risking stale assets.
Enable content versioning and long-lived caching via ResourceChain with VersionResourceResolver and set Cache-Control to public, max-age=31536000 for static locations; keep HTML templates non-immutable.
Versioned URLs allow aggressive caching without risking stale assets.
Side-by-side examples engineers can pattern-match during review.
registry.addResourceHandler("/assets/").addResourceLocations("classpath:/static/");registry.addResourceHandler("/assets/")
.addResourceLocations("classpath:/static/")
.setCacheControl(CacheControl.maxAge(365, java.util.concurrent.TimeUnit.DAYS).cachePublic())
.resourceChain(true)
.addResolver(new VersionResourceResolver().addContentVersionStrategy("/\")));new VersionResourceResolver().addContentVersionStrategy("/")addResourceHandler("/assets/**").addResourceLocations(...); // no cacheFrom the same buckets as this rule.
All static JS/CSS/font/image files MUST use content-hashed filenames (e.g., app.9c1a7b.js) and be served with "Cache-Control: public, max-age=31536000, immutable". HTML and other non-fingerprinted documents MUST be served with "Cache-Control: no-cache" (or equivalent) to enable conditional revalidation.
Serve text-based assets (JS, CSS, JSON, SVG) with Brotli (br) when the client sends "Accept-Encoding: br" and fallback to gzip. Always set "Vary: Accept-Encoding" and do NOT compress already-compressed formats (e.g., .png, .jpg, .woff2).