1

I have a CSS file dynamically generated by a Spring controller. I set the Cache-Control response header in the handler method but for some reason my FireFox keeps requesting the CSS file when requesting an HTML file that has a reference to it instead of using the cached version.

Here's the code.

@Controller
@RequestMapping("/foo.css")
public class FooController {
    @RequestMapping(method = RequestMethod.GET)
    public void show(HttpServletResponse response) {
        try {
            response.setHeader("Cache-Control", "max-age=3600");
            response.getWriter().println("this is a test.");
        }
        catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(new Date());
    }
}

And the HTML file references the CSS file in the usual way.

<link rel="stylesheet" type="text/css" href="/foo.css" />

What am I doing wrong here?

3 Answers 3

2

I'm the OP, but after further research, I've decided that you need to implement this yourself. You need to have the server generate a 304 response code for the client browser to use a cached resource, but neither Spring nor Tomcat support this out-of-box.

Sign up to request clarification or add additional context in comments.

Comments

1

Well, first of all, browsers and proxy servers do not need to comply with HTTP Cache controls putted on the headers. They are only advisory. Maybe the browser is ignoring the cache request and obeying what is configured on his preferences.

Another way to do that is putting a random attribute to the url, generated by javascript. Something like:

<link type="text/css" href="/foo.css?d=328943298432" />

Here's a nice article about this subject http://code.google.com/speed/page-speed/docs/caching.html

Comments

0

This has been supported by Spring for quite a while now, and it has been improved in recent versions. See the reference documentation about this.

@Controller
public class FooController {

  @RequestMapping("/foo.css")
  public ResponseEntity<String> show() {

    String cssContent = generateCssContent();
    String version = hashCssContent(cssContent);

    // automatically writes CacheControl + Etag headers
    // generates HTTP 304 responses for conditional requests
    return ResponseEntity
        .ok()
        .cacheControl(CacheControl.maxAge(30, TimeUnit.DAYS))
        .eTag(version) // lastModified is also available
        .body(cssContent);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.