/
sw-lib.js
180 lines (164 loc) 路 5.41 KB
/
sw-lib.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Copyright 2016 Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/* eslint-env browser, serviceworker */
import RouterWrapper from './router-wrapper.js';
import ErrorFactory from './error-factory.js';
import {PrecacheManager} from '../../../sw-precaching/src/index.js';
import {
CacheFirst, CacheOnly, NetworkFirst, NetworkOnly, StaleWhileRevalidate,
} from '../../../sw-runtime-caching/src/index.js';
/**
* This is a high level library to help with using service worker
* precaching and run time caching.
*
* @memberof module:sw-lib
*/
class SWLib {
/**
* Initialises an instance of SWLib. An instance of this class is
* accessible when the module is imported into a service worker as
* `self.goog.swlib`.
*/
constructor() {
this._router = new RouterWrapper();
this._precacheManager = new PrecacheManager();
this._strategies = {
cacheFirst: new CacheFirst(),
cacheOnly: new CacheOnly(),
networkFirst: new NetworkFirst(),
networkOnly: new NetworkOnly(),
fastest: new StaleWhileRevalidate(),
};
}
/**
* Revisioned assets can be cached intelligently
* during the install (i.e. old files are cleared from the cache, new files
* are added to the cache and unchanged files are left as is).
*
* @example
* self.goog.swlib.cacheRevisionedAssets([
* '/styles/main.1234.css',
* {
* url: '/index.html',
* revision: '1234'
* }
* ]);
*
* @param {Array<String|Object>} revisionedFiles A set of urls to cache
* when the service worker is installed.
*/
cacheRevisionedAssets(revisionedFiles) {
// Add a more helpful error message than assertion error.
if (!Array.isArray(revisionedFiles)) {
throw ErrorFactory.createError('bad-revisioned-cache-list');
}
this._precacheManager.cacheRevisioned({
revisionedFiles,
});
}
/**
* Any assets you wish to cache which can't be revisioned should be
* cached with this method. All assets are cached on install regardless of
* whether an older version of the request is in the cache.
*
* @example
* self.goog.swlib.warmRuntimeCache([
* '/scripts/main.js',
* new Request('/images/logo.png')
* ]);
*
* @param {Array<String|Request>} unrevisionedFiles A set of urls to cache
* when the service worker is installed.
*/
warmRuntimeCache(unrevisionedFiles) {
// Add a more helpful error message than assertion error.
if (!Array.isArray(unrevisionedFiles)) {
throw ErrorFactory.createError('bad-revisioned-cache-list');
}
this._precacheManager.cacheUnrevisioned({
unrevisionedFiles,
});
}
/**
* You can access the [Router]{@link module:sw-lib.RouterWrapper}
* with `self.goog.swlib.router`.
* @return {RouterWrapper} Returns the Router.
*/
get router() {
return this._router;
}
/**
* This handler will check is there is a cache response and respond with it if
* there is, otherwise it will make a network request and respond with that,
* caching the response for the next time it's requested.
*
* @example
* self.goog.swlib.router.registerRoute('/', self.google.swlib.cacheFirst);
*
* @return {Handler} A CacheFirst response handler.
*/
get cacheFirst() {
return this._strategies.cacheFirst;
}
/**
* This handler will check is there is a cache response and respond with it if
* there is, otherwise it will throw an error.
*
* @example
* self.goog.swlib.router.registerRoute('/', self.google.swlib.cacheOnly);
*
* @return {Handler} A CacheOnly response handler.
*/
get cacheOnly() {
return this._strategies.cacheOnly;
}
/**
* This handler will attempt to get a response from the network and respond
* with it if available, updating the cache as well. If the network request
* fails, it will respond with any cached response available.
*
* @example
* self.goog.swlib.router.registerRoute('/', self.google.swlib.networkFirst);
*
* @return {Handler} A NetworkFirst response handler.
*/
get networkFirst() {
return this._strategies.networkFirst;
}
/**
* This handle will only return with network requests.
*
* @example
* self.goog.swlib.router.registerRoute('/', self.google.swlib.networkOnly);
*
* @return {Handler} A NetworkOnly response handler.
*/
get networkOnly() {
return this._strategies.networkOnly;
}
/**
* This handler will check the cache and make a network request for all
* requests. If the caches has a value it will be returned and when the
* network request has finished, the cache will be updated. If there is no
* cached response, the request will be forfilled by the network request.
*
* @example
* self.goog.swlib.router.registerRoute('/', self.google.swlib.fastest);
*
* @return {Handler} A StaleWhileRevalidate response handler.
*/
get fastest() {
return this._strategies.fastest;
}
}
export default SWLib;