question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cache busting not working

See original GitHub issue

Following https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content it should be enough to add

spring.resources.chain.enabled=true
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

to my application.properties in order to enable the cache busting / fingerprinting of my static assets.

Here’s an example thymeleaf doc head:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title th:text="#{page.title}"></title>
    <link th:href="@{/css/main.css}" rel="stylesheet" media="screen"/>
    <meta name="viewport" content="width=device-width, initial-scale=1">
</head>

However, the output is still /css/main.css without any version.

Spring Boot v2.2.6.RELEASE
id ‘org.springframework.boot’ version ‘2.2.6.RELEASE’ id ‘io.spring.dependency-management’ version ‘1.0.9.RELEASE’
implementation ‘org.springframework.boot:spring-boot-starter-thymeleaf’

Also running in the IDEA / running the build jar makes no difference.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
black-snowcommented, May 6, 2020

@ultraq kudos to you for you efforts - I was finally able to nail the issue down to lazy-initialization.

Once I spring.main.lazy-initialization=true cache-busting is gone. This seems like a bug to me for the docs say that bean stuff is delayed until an actual HTTP request hits the bean. But it has nothing to do with thymeleaf. I’ll open an issue in spring.

Thanks!

1reaction
ultraqcommented, Apr 29, 2020

I was able to get it working by taking the example app downloadable from https://spring.io/guides/gs/serving-web-content/, matching the versions of the dependencies you listed, then adding an application.properties file with the config values in your post, adding <link rel="stylesheet" th:href="@{/css/test.css}"/> to the greeting.html file, and adding an empty CSS file in the src/main/resources/static/css directory. Result:

<!DOCTYPE HTML>
<html>
<head> 
    <title>Getting Started: Serving Web Content</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="/css/test-d41d8cd98f00b204e9800998ecf8427e.css"/>
</head>
<body>
    <p >Hello, World!</p>
</body>
</html>

One thing I noticed: if there was no file at the target path (like after I got it working I deleted test.css), then Spring Boot couldn’t fingerprint my file and I’d get the plain /css/test.css in the HTML from the server. So it might be good to double check paths to resources are correct as that seems to be a requirement for getting it working too.

But the fingerprinting is definitely working with Thymeleaf, so if there are more configuration issues then it might be worth asking over on the Spring Boot GitHub or on StackOverflow with the spring-boot tag.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cache Busting Not Working? Google Chrome? - Stack Overflow
Chrome seems to be pretty enthusiastic about caching. I've pushed out a few updated js files that required multiple hard refreshes (Ctrl + ......
Read more >
Cache busting stopped working - WordPress.org
In the latest update cache busting is broken. Function *_build_single_hash_url* says: // $qs_hash is from src, same as $filename, redundant.
Read more >
Cache busting in Angular. The problem - Medium
The problem: The cache does not automatically clear on every angular update. Let's first define what the cache does.
Read more >
Cache Busting Explained - StackPath Help
Cache busting is the process of uploading a new file to replace an existing file that is already cached. This is useful because...
Read more >
Cache Busting - Advanced Ads
If you send out a feed or promote your content on external sites, Cache busting is not working, and will fall back not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found