Refreshing imported CSS in Spring with content versioning strategy for cache busting

After following a tutorial on using a content version strategy in Spring for static assets, everything is working correctly except for one corner case that I'm facing:

Within my HTML, there is a <link> referencing a CSS file called a.css. Upon inspecting the HTML returned by the server, I noticed that the link has been transformed to a-(md5).css, which is expected. The issue arises because a.css also imports another CSS file named b.css. Spring properly updates the import from @import '/css/b.css' to @import '/css/b-(md5).css'. However, when I update b.css, the browser caches the request of a-(md5).css since the md5 of a.css remains the same, leading to incorrect styling due to the pointer still pointing to the previous b-(old-md5).css

This seems like a common issue. How can this be resolved?
Is there a way to instruct the version strategy to compute the md5 after resolving links so that if the dependency's md5 changes, it would also affect the dependent's md5?

Below is my current configuration:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    //content-based versioning and max caching
    registry.addResourceHandler("/**")
            .addResourceLocations("classpath:/static/")
            .setCacheControl(MAX_CACHE_DURATION)
            .resourceChain(false)
            .addResolver(new VersionResourceResolver().addContentVersionStrategy("/**"))
            .addTransformer(new CssLinkResourceTransformer());

    //no cache
    registry.addResourceHandler("/*.html").setCacheControl(CacheControl.noCache());
}

Answer №1

Unfortunately, I struggled to find a neat solution for this issue. As a temporary fix, I decided to turn off caching for all CSS files.

        registry.addResourceHandler("/ui/css/**")
            .addResourceLocations("classpath:/static/css/")
            .setCacheControl(CacheControl.noCache())
            .resourceChain(false)
            .addResolver(new VersionResourceResolver());

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can an inline element be made to wrap onto a new line only after it has reached the full width of the page?

Usually, when you have two inline elements and resize the page down, the second element wraps to the next line like this: Is there a way to make it go onto its own line first, and then wrap normally from there? If I use white-space: nowrap on the second ...

The Link Element Does Not Appear Properly When Styled Using nth-of-type Selector

https://codesandbox.io/s/damp-worker-k7fj6y?file=/src/App.js Can anyone help me understand why the fourth .content <Link/> is not displaying when using the given CSS styling? Could it be a bug in the code, or am I overlooking something important? ...

Swap the < symbol with &gt; and & with &amp; within a specific node or location in an XML document

Hello everyone, I have a XML file with a node named Section, where certain characters like & and < are treated as invalid. I need to replace < with &lt; and & with &amp; only within the Section Node. Currently, I am using the following Java ...

Experiencing difficulties with a JavaScript loading issue where images are not loading properly

I recently downloaded a calendar JavaScript file and placed it in the directory /user/js/calendar. In my HTML file located at /user/, I added the following code: <script language="JavaScript" src="js/calendar/calendar_us.js"></script> <link ...

What is the best way to navigate back to the field where the form validation has not passed successfully?

Currently, I am in the process of creating a registration form that includes validation. When a field fails the validation test (e.g. if the first name is left blank), an alert is triggered. However, the issue arises when alerts for all empty fields are di ...

Position the Facebook Like Button in alignment with the counts box

Is it possible to align the Facebook likes count box at the bottom of my Like Button on my website? I'm looking for help with this task. Here is a visual representation of how I want the fb button to be positioned or styled: ...

Can you affix a tag to the picture?

Currently, I have a static page located at . However, I am planning to dynamically generate this page by retrieving data from a database. My challenge lies in assigning a position ID obtained from the database to each of the tyres on the page. Any sugges ...

Iterating through elements within a Div will retrieve the initial element exclusively

Is there a way to loop through all elements within the MainDiv Div in order to retrieve their values? Currently, I am only able to retrieve the value of the first element. <div id="MainDiv"> <input type="text" id="MyText"value="Text1" /> ...

How to Right Align Text in a Bootstrap Table

Exploring Bootstrap, I challenged myself to create a table without the usual table tags, using only the grid system. While I managed to make it look decent, I'm struggling to align the numbers to the right. Here's a JSFiddle link to what I'v ...

Streamlined Navigation Bar Design for Websites

Struggling with aligning all the navbar options in a single row. Here is an example: https://jsfiddle.net/knyx2u8h/ ` Example Domain <meta charset="utf-8" /> <meta http-equiv="Content-type" content="text/html; char ...

Repetitive data in a list with AngularJS

There's probably a simple solution to this: I have some data in a controller, and depending on whether an item is selected or not, it should display different HTML. If the item is selected, the HTML should be: <li> <a href="#"><s ...

What are the steps to implement AJAX pagination in an HTML page?

Here is my code snippet: $(document).ready(function(){ $("#conteudo").click(function( e ){ e.preventDefault(); var href = $( this ).attr('href'); $('#paginacao a').load(href +'#conteudo'); }); ...

Adjust the color of text in an input field as you type (css)

I am trying to customize the appearance of my search box by making the text color white. However, even after changing the placeholder color and text color to white in my CSS code, the text color only changes to white when the user clicks out of the form. W ...

The dropdown menu seems to be missing from the navigation bar

My dropdown menu is not behaving as expected after clicking. It should be a simple dropdown menu with the tab "about," similar to the one on w3schools. https://i.sstatic.net/VZBGZ.png Below is the code snippet: /* Function to toggle between hiding and ...

Trouble arises when attempting to convert Empty Dates in MongoDB queries using Spring Repository

I am attempting to fetch a document from MongoDB using the following repository: public interface MyObjectRepository extends Repository<MyObject, String>{ MyObject findMyObjectByIdObject(String IdObject);} MyObject is a basic custom class with only ...

Issues with Angular ngroute: controllers are not functioning properly

In order to route my application with different themes on the same page, I plan to utilize the ngroute module. Here's an example of how I intend to achieve this: <html> <head> <title>Angular JS Views</title> ...

The progress bar fails to update once it hits 100%

My circle progress bar is not resetting to start over when it reaches 100%. The code seems to work fine in my browser, but for some reason it's not working on JSFiddle. Here is the link: https://jsfiddle.net/BrsJsk/5e9hs69y/5/ window.onload = fu ...

equal child width as parent column container

I'm in the process of developing a custom dropdown search bar that presents results as the user enters text. One issue I'm facing is that the list of results must have a position: absolute. This causes it to disregard the width of the parent col ...

Customizing the color of the thumbs on a Material UI range slider

Hey there, currently working on creating a range slider with 2 values using the material UI slider component. My goal is to customize the two thumbs with different colors. https://i.sstatic.net/BUq12.png Any ideas on how I can achieve this? ...

Having issues with Tailwind colors not dynamically updating in brackets

Recently, I encountered an issue where my custom colors were not working when implemented dynamically. Below, you'll find two sets of codes and screenshots: one with the dynamic code and output, and another with the static code and output. I prefer n ...