-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathangular-cache-buster.js
More file actions
62 lines (50 loc) · 1.66 KB
/
angular-cache-buster.js
File metadata and controls
62 lines (50 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
angular.module('ngCacheBuster', [])
.config(['$httpProvider', function($httpProvider) {
return $httpProvider.interceptors.push('httpRequestInterceptorCacheBuster');
}])
.provider('httpRequestInterceptorCacheBuster', function() {
this.matchlist = [/.*partials.*/, /.*views.*/ ];
this.logRequests = false;
//Default to whitelist (i.e. block all except matches)
this.black=false;
//Select blacklist or whitelist, default to whitelist
this.setMatchlist = function(list,black) {
this.black = typeof black != 'undefined' ? black : false
this.matchlist = list;
};
this.setLogRequests = function(logRequests) {
this.logRequests = logRequests;
};
this.$get = ['$q', '$log', function($q, $log) {
var matchlist = this.matchlist;
var logRequests = this.logRequests;
var black = this.black;
if (logRequests) {
$log.log("Blacklist? ",black);
}
return {
'request': function(config) {
//Blacklist by default, match with whitelist
var busted= !black;
for(var i=0; i< matchlist.length; i++){
if(config.url.match(matchlist[i])) {
busted=black; break;
}
}
//Bust if the URL was on blacklist or not on whitelist
if (busted) {
var d = new Date();
config.url = config.url.replace(/[?|&]cacheBuster=\d+/,'');
//Some url's allready have '?' attached
config.url+=config.url.indexOf('?') === -1 ? '?' : '&'
config.url += 'cacheBuster=' + d.getTime();
}
if (logRequests) {
var log='request.url =' + config.url
busted ? $log.warn(log) : $log.info(log)
}
return config || $q.when(config);
}
}
}];
});