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 | 'use strict';
|
39 |
|
40 | var precacheConfig = [["/mirage/index.js","4f6d556e043f9b14847fda071664d591"]];
|
41 | var cacheName = 'sw-precache-v3-sw-precache-webpack-plugin-' + (self.registration ? self.registration.scope : '');
|
42 |
|
43 |
|
44 | var ignoreUrlParametersMatching = [/^utm_/];
|
45 |
|
46 |
|
47 |
|
48 | var addDirectoryIndex = function(originalUrl, index) {
|
49 | var url = new URL(originalUrl);
|
50 | if (url.pathname.slice(-1) === '/') {
|
51 | url.pathname += index;
|
52 | }
|
53 | return url.toString();
|
54 | };
|
55 |
|
56 | var cleanResponse = function(originalResponse) {
|
57 |
|
58 | if (!originalResponse.redirected) {
|
59 | return Promise.resolve(originalResponse);
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | var bodyPromise = 'body' in originalResponse ?
|
65 | Promise.resolve(originalResponse.body) :
|
66 | originalResponse.blob();
|
67 |
|
68 | return bodyPromise.then(function(body) {
|
69 |
|
70 | return new Response(body, {
|
71 | headers: originalResponse.headers,
|
72 | status: originalResponse.status,
|
73 | statusText: originalResponse.statusText
|
74 | });
|
75 | });
|
76 | };
|
77 |
|
78 | var createCacheKey = function(originalUrl, paramName, paramValue,
|
79 | dontCacheBustUrlsMatching) {
|
80 |
|
81 | var url = new URL(originalUrl);
|
82 |
|
83 |
|
84 |
|
85 | if (!dontCacheBustUrlsMatching ||
|
86 | !(url.pathname.match(dontCacheBustUrlsMatching))) {
|
87 | url.search += (url.search ? '&' : '') +
|
88 | encodeURIComponent(paramName) + '=' + encodeURIComponent(paramValue);
|
89 | }
|
90 |
|
91 | return url.toString();
|
92 | };
|
93 |
|
94 | var isPathWhitelisted = function(whitelist, absoluteUrlString) {
|
95 |
|
96 | if (whitelist.length === 0) {
|
97 | return true;
|
98 | }
|
99 |
|
100 |
|
101 | var path = (new URL(absoluteUrlString)).pathname;
|
102 | return whitelist.some(function(whitelistedPathRegex) {
|
103 | return path.match(whitelistedPathRegex);
|
104 | });
|
105 | };
|
106 |
|
107 | var stripIgnoredUrlParameters = function(originalUrl,
|
108 | ignoreUrlParametersMatching) {
|
109 | var url = new URL(originalUrl);
|
110 |
|
111 | url.hash = '';
|
112 |
|
113 | url.search = url.search.slice(1)
|
114 | .split('&')
|
115 | .map(function(kv) {
|
116 | return kv.split('=');
|
117 | })
|
118 | .filter(function(kv) {
|
119 | return ignoreUrlParametersMatching.every(function(ignoredRegex) {
|
120 | return !ignoredRegex.test(kv[0]);
|
121 | });
|
122 | })
|
123 | .map(function(kv) {
|
124 | return kv.join('=');
|
125 | })
|
126 | .join('&');
|
127 |
|
128 | return url.toString();
|
129 | };
|
130 |
|
131 |
|
132 | var hashParamName = '_sw-precache';
|
133 | var urlsToCacheKeys = new Map(
|
134 | precacheConfig.map(function(item) {
|
135 | var relativeUrl = item[0];
|
136 | var hash = item[1];
|
137 | var absoluteUrl = new URL(relativeUrl, self.location);
|
138 | var cacheKey = createCacheKey(absoluteUrl, hashParamName, hash, /\.\w{8}\./);
|
139 | return [absoluteUrl.toString(), cacheKey];
|
140 | })
|
141 | );
|
142 |
|
143 | function setOfCachedUrls(cache) {
|
144 | return cache.keys().then(function(requests) {
|
145 | return requests.map(function(request) {
|
146 | return request.url;
|
147 | });
|
148 | }).then(function(urls) {
|
149 | return new Set(urls);
|
150 | });
|
151 | }
|
152 |
|
153 | self.addEventListener('install', function(event) {
|
154 | event.waitUntil(
|
155 | caches.open(cacheName).then(function(cache) {
|
156 | return setOfCachedUrls(cache).then(function(cachedUrls) {
|
157 | return Promise.all(
|
158 | Array.from(urlsToCacheKeys.values()).map(function(cacheKey) {
|
159 |
|
160 | if (!cachedUrls.has(cacheKey)) {
|
161 | var request = new Request(cacheKey, {credentials: 'same-origin'});
|
162 | return fetch(request).then(function(response) {
|
163 |
|
164 |
|
165 | if (!response.ok) {
|
166 | throw new Error('Request for ' + cacheKey + ' returned a ' +
|
167 | 'response with status ' + response.status);
|
168 | }
|
169 |
|
170 | return cleanResponse(response).then(function(responseToCache) {
|
171 | return cache.put(cacheKey, responseToCache);
|
172 | });
|
173 | });
|
174 | }
|
175 | })
|
176 | );
|
177 | });
|
178 | }).then(function() {
|
179 |
|
180 |
|
181 | return self.skipWaiting();
|
182 |
|
183 | })
|
184 | );
|
185 | });
|
186 |
|
187 | self.addEventListener('activate', function(event) {
|
188 | var setOfExpectedUrls = new Set(urlsToCacheKeys.values());
|
189 |
|
190 | event.waitUntil(
|
191 | caches.open(cacheName).then(function(cache) {
|
192 | return cache.keys().then(function(existingRequests) {
|
193 | return Promise.all(
|
194 | existingRequests.map(function(existingRequest) {
|
195 | if (!setOfExpectedUrls.has(existingRequest.url)) {
|
196 | return cache.delete(existingRequest);
|
197 | }
|
198 | })
|
199 | );
|
200 | });
|
201 | }).then(function() {
|
202 |
|
203 | return self.clients.claim();
|
204 |
|
205 | })
|
206 | );
|
207 | });
|
208 |
|
209 |
|
210 | self.addEventListener('fetch', function(event) {
|
211 | if (event.request.method === 'GET') {
|
212 |
|
213 |
|
214 |
|
215 | var shouldRespond;
|
216 |
|
217 |
|
218 |
|
219 | var url = stripIgnoredUrlParameters(event.request.url, ignoreUrlParametersMatching);
|
220 | shouldRespond = urlsToCacheKeys.has(url);
|
221 |
|
222 |
|
223 |
|
224 | var directoryIndex = 'index.html';
|
225 | if (!shouldRespond && directoryIndex) {
|
226 | url = addDirectoryIndex(url, directoryIndex);
|
227 | shouldRespond = urlsToCacheKeys.has(url);
|
228 | }
|
229 |
|
230 |
|
231 |
|
232 | var navigateFallback = '/mirage/index.html';
|
233 | if (!shouldRespond &&
|
234 | navigateFallback &&
|
235 | (event.request.mode === 'navigate') &&
|
236 | isPathWhitelisted(["^(?!\\/__).*"], event.request.url)) {
|
237 | url = new URL(navigateFallback, self.location).toString();
|
238 | shouldRespond = urlsToCacheKeys.has(url);
|
239 | }
|
240 |
|
241 |
|
242 |
|
243 | if (shouldRespond) {
|
244 | event.respondWith(
|
245 | caches.open(cacheName).then(function(cache) {
|
246 | return cache.match(urlsToCacheKeys.get(url)).then(function(response) {
|
247 | if (response) {
|
248 | return response;
|
249 | }
|
250 | throw Error('The cached response that was expected is missing.');
|
251 | });
|
252 | }).catch(function(e) {
|
253 |
|
254 |
|
255 | console.warn('Couldn\'t serve response for "%s" from cache: %O', event.request.url, e);
|
256 | return fetch(event.request);
|
257 | })
|
258 | );
|
259 | }
|
260 | }
|
261 | });
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|