-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
50 lines (43 loc) · 1.46 KB
/
index.js
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
var cacheBust = module.exports = function cacheBust (options) {
var defaults = {
version: null,
packageLocation: __dirname + '/../../package.json'
};
options = options ? {
version: options.version || defaults.version,
packageLocation: options.packageLocation || defaults.packageLocation
} : defaults;
if (!options.version) {
try {
options.version = require(options.packageLocation).version;
} catch(e) {
throw(new Error('No version information found. Looked in ' + options.packageLocation));
}
}
var querystring;
if (process.env.NODE_ENV === 'production') {
querystring = options.version;
} else {
querystring = options.version + '-' + cacheBust.getTimestamp();
}
return function (ressource, type) {
type = type || getType(ressource);
if (type === 'js' || type === 'jsx') {
return '<script src="' + ressource + '?v=' + querystring + '"></script>';
} else if (type === 'css') {
return '<link rel="stylesheet" href="' + ressource + '?v=' + querystring + '" />';
} else {
throw new Error('Unknown extension, currently only css, js and jsx are automatically recognized. When using another extension specify either js or css as the second parameter')
}
};
};
cacheBust.handler = function handler (app, options) {
app.locals.cacheBust = cacheBust(options);
};
cacheBust.getTimestamp = function getTimestamp () {
return new Date().valueOf();
};
function getType (ressource) {
var extension = ressource.split('.').pop();
return extension;
}