-
Notifications
You must be signed in to change notification settings - Fork 96
/
serviceworker.js
142 lines (129 loc) · 3.51 KB
/
serviceworker.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// most of this was taken from @adactio's serviceworker
// https://adactio.com/journal/9775
// https://adactio.com/serviceworker.js
var version = 'v1.0::';
var staticCacheName = version + 'static';
var pagesCacheName = version + 'pages';
var imagesCacheName = version + 'images';
function updateStaticCache () {
return caches
.open( staticCacheName )
.then( function ( cache ) {
// These items won't block the installation of the Service Worker
cache
.addAll( [
'images/icon/alert-circle.svg',
'images/icon/camera.svg',
'images/icon/content-save.svg',
'images/icon/delete.svg',
'images/icon/emoticon.svg',
'images/icon/fullscreen-exit.svg',
'images/icon/fullscreen.svg',
'images/icon/information-outline.svg',
'images/icon/open-in-app.svg',
'images/icon/share-variant.svg',
'images/icon/settings.svg',
'images/icon/download.svg',
'lang/en-us.json'
] );
// These items must be cached for the Service Worker to complete installation
return cache
.addAll( [
'scripts/glitcher.js',
'scripts/workers/glitchworker.js',
'scripts/workers/storageworker.js',
'scripts/workers/settingsworker.js',
'styles/glitcher.css'
] );
} );
}
function putInCache ( cacheName, request, response ) {
caches
.open( cacheName )
.then( function ( cache ) {
cache
.keys()
.then( function ( keys ) {
cache.put( request, response );
} )
} );
}
function clearOldCaches () {
return caches
.keys()
.then( function ( keys ) {
// Remove caches whose name is no longer valid
return Promise
.all(
keys
.filter( filterInvalidKeys )
.map( function ( key ) { return caches.delete( key ); } )
);
} );
}
function filterInvalidKeys ( key ) {
return key.indexOf( version ) !== 0;
}
function installed ( event ) {
event
.waitUntil( updateStaticCache().then( function () {
return self.skipWaiting();
} ) );
}
function activated ( event ) {
event
.waitUntil( clearOldCaches().then( function () {
return self.clients.claim();
} ) );
}
function fetched ( event ) {
var request = event.request;
// For non-GET requests, try the network, do not fall back to cache.
if ( request.method !== 'GET' ) {
event.respondWith( fetch( request ) );
return;
}
// For HTML requests, try the network first, fall back to the cache
if ( request.headers.get( 'Accept' ).indexOf( 'text/html' ) !== -1 ) {
event.respondWith(
fetch( request )
.then( function ( response ) {
// NETWORK
// Stash a copy of this page in the pages cache
putInCache( pagesCacheName, request, response.clone() );
return response;
} )
.catch( function () {
// CACHE
return caches
.match( request )
.then( function ( response ) {
return response;
} );
} )
);
return;
}
// For non-HTML requests, look in the cache first, fall back to the network
event.respondWith(
caches
.match( request )
.then( function ( response ) {
// CACHE
return response || fetch( request )
.then( function ( response ) {
// NETWORK
// If the request is for an image, stash a copy of this image in the images cache
if ( request.headers.get( 'Accept' ).indexOf( 'image' ) !== -1 ) {
putInCache( imagesCacheName, request, response.clone() );
}
return response;
} );
} )
);
}
(function () {
self.addEventListener( 'fetch', fetched );
self.addEventListener( 'install', installed );
self.addEventListener( 'activated', activated );
})();