1 | 'use strict';
|
2 |
|
3 | var _assign = require('babel-runtime/core-js/object/assign');
|
4 |
|
5 | var _assign2 = _interopRequireDefault(_assign);
|
6 |
|
7 | var _promise = require('babel-runtime/core-js/promise');
|
8 |
|
9 | var _promise2 = _interopRequireDefault(_promise);
|
10 |
|
11 | var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
|
12 |
|
13 | var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | var DEBUG = false;
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | var assets = global.serviceWorkerOption.assets;
|
28 |
|
29 |
|
30 | var CACHE_NAME = new Date().toISOString();
|
31 |
|
32 | var assetsToCache = [].concat((0, _toConsumableArray3.default)(assets));
|
33 |
|
34 | assetsToCache = assetsToCache.map(function (path) {
|
35 | return new URL(path, global.location).toString();
|
36 | });
|
37 |
|
38 |
|
39 | self.addEventListener('install', function (event) {
|
40 |
|
41 | if (DEBUG) {
|
42 | console.log('[SW] Install event');
|
43 | }
|
44 |
|
45 |
|
46 | event.waitUntil(global.caches.open(CACHE_NAME).then(function (cache) {
|
47 | return cache.addAll(assetsToCache);
|
48 | }).then(function () {
|
49 | if (DEBUG) {
|
50 | console.log('Cached assets: main', assetsToCache);
|
51 | }
|
52 | }).catch(function (error) {
|
53 | console.error(error);
|
54 | throw error;
|
55 | }));
|
56 | });
|
57 |
|
58 |
|
59 | self.addEventListener('activate', function (event) {
|
60 | if (DEBUG) {
|
61 | console.log('[SW] Activate event');
|
62 | }
|
63 |
|
64 |
|
65 | event.waitUntil(global.caches.keys().then(function (cacheNames) {
|
66 | return _promise2.default.all(cacheNames.map(function (cacheName) {
|
67 |
|
68 | if (cacheName.indexOf(CACHE_NAME) === 0) {
|
69 | return null;
|
70 | }
|
71 |
|
72 | return global.caches.delete(cacheName);
|
73 | }));
|
74 | }));
|
75 | });
|
76 |
|
77 | self.addEventListener('message', function (event) {
|
78 | switch (event.data.action) {
|
79 | case 'skipWaiting':
|
80 | if (self.skipWaiting) {
|
81 | self.skipWaiting();
|
82 | self.clients.claim();
|
83 | }
|
84 | break;
|
85 | default:
|
86 | break;
|
87 | }
|
88 | });
|
89 |
|
90 | self.addEventListener('fetch', function (event) {
|
91 | var request = event.request;
|
92 |
|
93 |
|
94 | if (request.method !== 'GET') {
|
95 | if (DEBUG) {
|
96 | console.log('[SW] Ignore non GET request ' + request.method);
|
97 | }
|
98 | return;
|
99 | }
|
100 |
|
101 | var requestUrl = new URL(request.url);
|
102 |
|
103 |
|
104 | if (requestUrl.origin !== location.origin) {
|
105 | if (DEBUG) {
|
106 | console.log('[SW] Ignore difference origin ' + requestUrl.origin);
|
107 | }
|
108 | return;
|
109 | }
|
110 |
|
111 | var resource = global.caches.match(request).then(function (response) {
|
112 | if (response) {
|
113 | if (DEBUG) {
|
114 | console.log('[SW] fetch URL ' + requestUrl.href + ' from cache');
|
115 | }
|
116 |
|
117 | return response;
|
118 | }
|
119 |
|
120 |
|
121 | return fetch(request).then(function (responseNetwork) {
|
122 | if (!responseNetwork || !responseNetwork.ok) {
|
123 | if (DEBUG) {
|
124 | console.log('[SW] URL [' + requestUrl.toString() + '] wrong responseNetwork: ' + responseNetwork.status + ' ' + responseNetwork.type);
|
125 | }
|
126 |
|
127 | return responseNetwork;
|
128 | }
|
129 |
|
130 | if (DEBUG) {
|
131 | console.log('[SW] URL ' + requestUrl.href + ' fetched');
|
132 | }
|
133 |
|
134 | var responseCache = responseNetwork.clone();
|
135 |
|
136 | global.caches.open(CACHE_NAME).then(function (cache) {
|
137 | return cache.put(request, responseCache);
|
138 | }).then(function () {
|
139 | if (DEBUG) {
|
140 | console.log('[SW] Cache asset: ' + requestUrl.href);
|
141 | }
|
142 | });
|
143 |
|
144 | return responseNetwork;
|
145 | }).catch(function () {
|
146 |
|
147 | if (event.request.mode === 'navigate') {
|
148 | return global.caches.match('./');
|
149 | }
|
150 |
|
151 | return null;
|
152 | });
|
153 | });
|
154 |
|
155 |
|
156 | if (request.headers.get('accept').includes('webp') && /\.jpe?g$|.png$/i.test(request.url)) {
|
157 | var url = request.url.replace(/\.[^\.]*?$/, '.webp');
|
158 | event.respondWith(fetch(new Request(url, (0, _assign2.default)({}, request))));
|
159 | }
|
160 | event.respondWith(resource);
|
161 | }); |
\ | No newline at end of file |