UNPKG

4.86 kBJavaScriptView Raw
1'use strict';
2
3var _assign = require('babel-runtime/core-js/object/assign');
4
5var _assign2 = _interopRequireDefault(_assign);
6
7var _promise = require('babel-runtime/core-js/promise');
8
9var _promise2 = _interopRequireDefault(_promise);
10
11var _toConsumableArray2 = require('babel-runtime/helpers/toConsumableArray');
12
13var _toConsumableArray3 = _interopRequireDefault(_toConsumableArray2);
14
15function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
16
17// @flow weak
18/* eslint-disable no-console */
19
20var DEBUG = false;
21
22// When the user navigates to your site,
23// the browser tries to redownload the script file that defined the service
24// worker in the background.
25// If there is even a byte's difference in the service worker file compared
26// to what it currently has, it considers it 'new'.
27var assets = global.serviceWorkerOption.assets;
28
29
30var CACHE_NAME = new Date().toISOString();
31
32var assetsToCache = [].concat((0, _toConsumableArray3.default)(assets));
33
34assetsToCache = assetsToCache.map(function (path) {
35 return new URL(path, global.location).toString();
36});
37
38// When the service worker is first added to a computer.
39self.addEventListener('install', function (event) {
40 // Perform install steps.
41 if (DEBUG) {
42 console.log('[SW] Install event');
43 }
44
45 // Add core website files to cache during serviceworker installation.
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// After the install event.
59self.addEventListener('activate', function (event) {
60 if (DEBUG) {
61 console.log('[SW] Activate event');
62 }
63
64 // Clean the caches
65 event.waitUntil(global.caches.keys().then(function (cacheNames) {
66 return _promise2.default.all(cacheNames.map(function (cacheName) {
67 // Delete the caches that are not the current one.
68 if (cacheName.indexOf(CACHE_NAME) === 0) {
69 return null;
70 }
71
72 return global.caches.delete(cacheName);
73 }));
74 }));
75});
76
77self.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
90self.addEventListener('fetch', function (event) {
91 var request = event.request;
92
93 // Ignore not GET request.
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 // Ignore difference origin.
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 // Load and cache known assets.
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 // User is landing on our page.
147 if (event.request.mode === 'navigate') {
148 return global.caches.match('./');
149 }
150
151 return null;
152 });
153 });
154
155 // optimize image of webp
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