4
votes

I'm running into an issue with my SpringMVC/Thymeleaf application where users experience errors when I release an update that includes changes to static resources (images, CSS files, JavaScript libraries). The problem occurs because they have an old version of the resource in their browser cache, so if they do a force refresh the problem goes away, but most users don't know to do that.

In the past, I've solved this in other frameworks by having my build inject the build number into a Spring bean and customizing the URL building mechanism to include the build number in the URL. Combining this with setting the Cache-Control response header to a far-future date on these resources ensures that as long as the user has the resource in their browser cache, it is never re-fetched unless I release a new build.

Does Spring MVC/Thymeleaf have a built-in way of solving this problem? If not, how do I override the URL writing mechanism in Thymeleaf so that I can add the behavior I described above?

1

1 Answers

2
votes

One solution could be to write your own dialect and define an own helper sr. Then you can write @{#sr(yoururl)}. You find information about writing your own dialect.

"As for creating your own utility object, that can be done by creating a simple dialect that implements the IExpressionEnhancingDialect interface. That will make Thymeleaf add any objects returned by your implementation of the getAdditionalExpressionObjects() method to the processing context."

In the dialect you could define the buildnr hard coded.

Another approach is to add the buildnr always to the model, then you can write @{yoururl(buildnr=${buildnr})}.

Or better you put it into the message source and can use @{yoururl(buildnr=#{buildnr})}.