Reputation: 11317
I have a bunch of angular directives with external templates as well as some code that directly loads some other templates. Is there an easy way to add a cache breaker as a query parameter for all such templates? Of course I could manually extend all template URLs manually for instance by '?' + date
but I would have to remember to do it every time. This is why I am looking for a generic solution. Any ideas?
Upvotes: 0
Views: 435
Reputation: 6066
Another solution would be to put all templates in the $templateCache
. There is a grunt task for this: https://www.npmjs.org/package/grunt-angular-templates. This puts all templates in one JavaScript file. Then you only have to make sure that that file is not cached.
Upvotes: 0
Reputation: 6066
What we do is use the base
tag in index.html
.
<head>
<script type="text/javascript">
document.write("<base href='http://" + document.location.host + "/" + buildNb + "' />");
</script>
...
</head>
This changes the base used when a relative path is resolved to an absolute path, so also when AngularJS fetches its templates. Note that this also solves the problem for your other static assets, such as JS, CSS, images.
index.html
is not cached. Every other file is put in a new sub directory with each build. These files are cached indefinitely.
Upvotes: 1
Reputation: 5313
Firstly you could wrap up the creation of a template uri into an angular service so you only generate the uris in one place. Then when you need to generate a uri for a template you could inject that service in.
Next inside that service you could have a private variable with a version number in that you could increment upon each release. Then the service that builds the uri simply has to append the version number in the query string which will only bust the cache when you change the version number.
Depending on the platform you are using you could automate this using a build server. For example if you are using a .net back end you could use the version number of a dll on the web server as your version number. The build server such as Team City can update that upon deployment to your test environment.
Upvotes: 1