Handling HTTP 304 Not Modified status when loading templates

6,926 views
Skip to first unread message

Unirgy

unread,
Jul 15, 2011, 10:40:52 PM7/15/11
to angular
I understand why AngularJS considers 304 status as an error - there's
no content in response and there's no way to access browser local
cache from JS.

Is there a way in the library not to send any cache related headers so
server side tampering is not required?

Are there any plans to implement persistent local cache for templates
utilizing JS local storage API?

Thanks.

Misko Hevery

unread,
Jul 16, 2011, 12:28:21 AM7/16/11
to ang...@googlegroups.com
Hi Unirgy,

I am not sure I understand your issue. Are you saying that the server responds with 304, but the browser XHR responds as error? i think XHR should handle that automatically.

For 304 Not Modified responses that are a result of a user agent generated conditional request the user agent must act as if the server gave a 200 OKresponse with the appropriate content. The user agent must allow setRequestHeader() to override automatic cache validation by setting request headers (e.g., If-None-MatchIf-Modified-Since), in which case 304 Not Modified responses must be passed through. [RFC2616]

So 304 should be transparent to  angular. What browser are you seeing this on?

-- Misko


--
You received this message because you are subscribed to the Google Groups "angular" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/angular?hl=en.


Igor Minar

unread,
Jul 16, 2011, 12:46:18 AM7/16/11
to ang...@googlegroups.com
+1 it should just work.

Boris Gurvich

unread,
Jul 16, 2011, 2:05:10 AM7/16/11
to ang...@googlegroups.com
Yep, I was getting errors in console for some templates that said
response code 304.
I'll send screenshot the next time it happens.

Anne Lam

unread,
Jul 7, 2017, 1:36:37 AM7/7/17
to Angular and AngularJS discussion
Hey Unirgy,

Seeing how this was quite some time ago, I'm not expecting a response, but I would very much appreciate knowing if you ever figured what was causing the template load error for you? We are experiencing it sporadically but it has been difficult to reproduce. Thanks!

Sander Elias

unread,
Jul 11, 2017, 12:01:13 AM7/11/17
to Angular and AngularJS discussion
Hi Anne,

This is so much outside the reach of angular! A 304 means the server say's to the browser, there is no change in this file, you can use your cached version. They only way to get a 304 is when there is indeed a cached version, otherwise, the file would just get pulled, like every other resource. The only way to get an error is when the cached version of the resource is corrupted somehow.  As this all play's inside the internals of the browser, there is no way you can any influence on this using JS. 
The only other possible scenario is that the server gives a 304  in fault. that means the server tells the browser, nope, you should have this file, while it doesn't. I believe of the 2 this is the most likely thing to happen. However, as this is an error in the server, there is nothing you can do in JS again to fix this. 

However, you might be able to catch the error in JS, and retry the request. This has to be done outside AngularJS. I think the "easy" solution is to use a service-worker. Most browsers support those nowadays.

Regards
Sander

吴国顺

unread,
Dec 20, 2019, 1:29:53 PM12/20/19
to Angular and AngularJS discussion
Hi Sander,

I recently faced the same issue. After some digging, I finally found the root cause is the modification date of template HTML files are too old. I found that if your template files modified earlier than 1997-0726 05:00, the issue would happen.

Note that my testing web server is lighttpd, other webservers may have a similar issue but the date may vary.

For example, you can execute the following command to reproduce this issue or fix the issue, where the *.html file are angular template files.

  1. Reproduced issue. date -s "199707260500" && touch login.html; touch status.html
  2. Fix the issue. date -s "199707260501" && touch login.html; touch status.html

BR,
Guoshun

在 2017年7月11日星期二 UTC+8下午12:01:13,Sander Elias写道:

Sander Elias

unread,
Dec 31, 2019, 4:51:16 AM12/31/19
to Angular and AngularJS discussion
Hi Guoshun,

It seems like a bug in your server, and still not something AngularJs can fix fr you. I'm pretty sure there are settings for the server that can fix this.
Another way to do cache-busting is to add a hash to every request. But that makes things more complex.

Regards
Sander
Reply all
Reply to author
Forward
0 new messages