bertramdev / grails-asset-pipeline

Grails Asset Pipeline

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to change response headers

Luxor opened this issue · comments

Hi,

I am using html files as templates for angular js. Angular js does not care about ETag which asset pipeline adds to html files. I guess it need "must-revalidate" in responce header.
That means Cache Controls set 1 year expition in AssetPipelineResponceBuilder.

Could you please advice, how to add no-cache, must-revalidate headers to avoid caching of html files.

Thanks in advance

This issue comes up a few times in questions. And yes the common recommendation is to turn off caching headers altogether for html but this is a very very bad solution. The fundamental issue is not all browsers properly adhere to cache headers when it comes to AJAX calls for loading templates. And as such this is a design flaw in angularjs 1.x . If the server is following the html spec the solution should not be to adjust the server response to handle a bug on the client. The solution should be to adjust the client to handle the bug. The other gotya is this solution does not even work on older versions of IE (9) . The real solution is actually built into the jquery $.ajax() method with the attribute cache:false. What this behavior does is add a parameter to every Ajax request with a time stamp so as to make the request unique. You can override this behavior in angularjs for template loading. They commonly add a ?_=3456578 where the number is the EPOCH time stamp in milliseconds. This gets around any of the cache header issues you are suffering from and has the added benefit of working with IE for greater compatibility. It also preserves the freedom of your server side application to be able to be put behind a CDN service with proper proxy caching and expiration.

Thanks,
David Estes

Sent from my iPad

On Mar 23, 2016, at 5:02 AM, Anton Tuzov notifications@github.com wrote:

Hi,

I am using html files as templates for angular js. Angular js does not care about ETag which asset pipeline adds to html files. I guess it need "must-revalidate" in responce header.
That means Cache Controls set 1 year expition in AssetPipelineResponceBuilder.

Could you please advice, how to add no-cache, must-revalidate headers to avoid caching of html files.

Thanks in advance


You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub

Hey @Luxor

I actually asked David about this very same issue recently, and given this same information, my solution was simply to use @craigburke's excellent Template Cache plugin.

It will load up all of your html templates as Javascript, and then not require separate requests for them later on. I find this up-front cost very minimal in the project I'm working on, but if that's too much of a 1-time payload hit, you can still use that plugin to load one JS file with just the bare minimum for your home page, and split the other templates out into a second JS fetch.

Otherwise, perhaps we should look together at submitting a PR to Angular to address the issue!

@Luxor It is also possible plugins / patches like this or something that does what @davydotcom suggested like this code snippit may work as well, but since I had already been thinking template caching would be a good fit for my current project (and I know Craig did good work on the plugin), that's the direction I thought best.

Thank you!!!