In development it can be very useful to be able to ensure that your server files aren’t cached.
Here is how to achieve this with AngularJS by using a request interceptor to intercept requests to the server and inject a no-cache request parameter where required.
First of all, let’s create our AngularJS interceptor:
This interceptor is very simple and can be easily adapted for your needs:
1 - Use shouldApplyNoCache() to check if this particular request should have caching eliminated
2 - If it does, call applyNoCache()
3 - ….that’s it. But three steps is always nicer, right?
I suppose the third step is to add the interceptor to our app configuration:
We really don’t want to apply this interceptor in real-world usage, because it will dramatically increase the traffic on your server. Caching is there for a reason, we only want to override this on our dev boxes!