Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring MVC disable caching for resource .js files

Tags:

spring-mvc

We have a bunch of .js files in a web application which are not located under a single directory. UI was developed separately and it is pretty time consuming to redesign it to have all the *.js files in one place.

The problem is that those files are cached by browsers pretty heavily and this creates a lot of issues with every application update. And we decided to turn off caching for those files.

So, *.js files were included into servlet mapping:

<servlet-mapping>
    <servlet-name>app</servlet-name>
    <url-pattern>*.js</url-pattern>
</servlet-mapping>

I have tried using mvc:resources but it does not handle url's masks like this:

<mvc:resources mapping="*.js" location="*.js" cache-period="0"/>

This doesn't work and I have 404 response back when I am trying to access a js file.

I have also tried mvc:interceptor:

    <mvc:interceptor>
        <mvc:mapping path="*.js"/>
        <bean id="webJSContentInterceptor"
              class="org.springframework.web.servlet.mvc.WebContentInterceptor">
            <property name="cacheSeconds" value="0"/>
            <property name="useExpiresHeader" value="true"/>
            <property name="useCacheControlHeader" value="true"/>
            <property name="useCacheControlNoStore" value="true"/>
        </bean>
    </mvc:interceptor>

This results in 404 error as well.

Is this type of thing possible?

like image 649
afansky Avatar asked Sep 21 '12 09:09

afansky


People also ask

How do I stop JavaScript caching in Chrome?

When you're in Google Chrome, click on View, then select Developer, then Developer Tools. Alternatively, you can right click on a page in Chrome, then click Inspect. Click on the Network tab, then check the box to Disable cache.

How does spring boot cache static data?

We can enable caching in the Spring Boot application by using the annotation @EnableCaching. It is defined in org. springframework. cache.

Can JavaScript be cached?

In general, most modern browsers will cache JavaScript files. This is standard practice for modern browsers and ensures an optimized loading experience. Cached assets such as JavaScript will typically be served from the browser's cache instead of making another request for a resource that has already been retrieved.


1 Answers

I realize this answer comes rather late, but here's for future reference.

Serving static resources with ResourceHttpRequestHandlers (Spring 3.2+)

First, you were on the right track with <mvc:resources>, there was just a typo in your path pattern declaration:

  • the location must be a folder location where you resources live (this can be on your file system, in your webapp resources, on the classpath, in webJARs...)
  • the pattern must take into account your project layout

Let's say you've got the following structure:

src/main/webapp/static/
  |- js/
  |- js/lib/jquery.js
  |- js/main.js
  |- css/style.css

You can serve all your static resources like this:

<mvc:resources mapping="/**" location="/static/"/>

Note: cache-period and other attributes should work just fine.

Resource handling features (Spring 4.1+)

As of Spring 4.1, there are new, more flexible ways to work with this.

For example, you can achieve cache busting with your JS and CSS files like this:

<mvc:resources mapping="/**" location="/static/"/>
  <mvc:resource-chain resource-cache="true">
    <mvc:resolvers>
      <mvc:version-resolver>
        <mvc:content-version-strategy patterns="/**"/>
      </mvc:version-resolver>
    </mvc:resolvers>
  </mvc:resource-chain>
</mvc:resources>

Of course you can achieve the same thing (more elegantly) with javaconfig:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

  VersionResourceResolver versionResolver = new VersionResourceResolver()
    .addContentVersionStrategy("/**");

  registry.addResourceHandler("/**")
    .addResourceLocations(/static/)
    .resourceChain(true).addResolver(versionResolver);
}

See this blog post and this example app for more on this.

like image 195
Brian Clozel Avatar answered Oct 04 '22 20:10

Brian Clozel