forked from GoogleChromeLabs/sw-precache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
69 lines (62 loc) · 2.14 KB
/
Gruntfile.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* eslint-env node */
// This is a basic Gruntfile illustrating how to call the sw-precache library. It doesn't include
// all of the functionality from the sample gulpfile, such as running a web server, or managing
// separate DEV and DIST directories.
'use strict';
var packageJson = require('../package.json');
var path = require('path');
var swPrecache = require('../lib/sw-precache.js');
module.exports = function(grunt) {
grunt.initConfig({
swPrecache: {
dev: {
handleFetch: false,
rootDir: 'app'
}
}
});
function writeServiceWorkerFile(rootDir, handleFetch, callback) {
var config = {
cacheId: packageJson.name,
dynamicUrlToDependencies: {
'dynamic/page1': [
path.join(rootDir, 'views', 'layout.jade'),
path.join(rootDir, 'views', 'page1.jade')
],
'dynamic/page2': [
path.join(rootDir, 'views', 'layout.jade'),
path.join(rootDir, 'views', 'page2.jade')
]
},
// If handleFetch is false (i.e. because this is called from swPrecache:dev), then
// the service worker will precache resources but won't actually serve them.
// This allows you to test precaching behavior without worry about the cache preventing your
// local changes from being picked up during the development cycle.
handleFetch: handleFetch,
logger: grunt.log.writeln,
staticFileGlobs: [
rootDir + '/css/**.css',
rootDir + '/**.html',
rootDir + '/images/**.*',
rootDir + '/js/**.js'
],
stripPrefix: rootDir + '/',
// verbose defaults to false, but for the purposes of this demo, log more.
verbose: true
};
swPrecache.write(path.join(rootDir, 'service-worker.js'), config, callback);
}
grunt.registerMultiTask('swPrecache', function() {
/* eslint-disable no-invalid-this */
var done = this.async();
var rootDir = this.data.rootDir;
var handleFetch = this.data.handleFetch;
/* eslint-enable */
writeServiceWorkerFile(rootDir, handleFetch, function(error) {
if (error) {
grunt.fail.warn(error);
}
done();
});
});
};