-
Notifications
You must be signed in to change notification settings - Fork 2
/
sw.js
86 lines (77 loc) · 2.47 KB
/
sw.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
/* Unfortunately, `eslint-config-airbnb` disallows the use of `self` (global variable referring to
`ServiceWorkerGlobalScope`) with the `no-restricted-globals` rule. It feels more like a flaw of
ESlint itself so we turn off the rule in this file. For more information read
https://github.com/airbnb/javascript/issues/1632. */
/* eslint no-restricted-globals: 0 */
const APP_NAME = 'frontend-garden';
const CACHE_VERSION = 1;
const ASSETS_PATH_BUILT = '/assets/built/';
const OFFLINE_URL = '/offline/';
const PRECACHE_ASSETS = [
OFFLINE_URL,
'/content/images/2019/05/icon.svg',
`${ASSETS_PATH_BUILT}css/main.min.css`,
];
const PRECACHE_NAME = `${APP_NAME}-precache-v${CACHE_VERSION}`;
const isHtmlPage = (event) => event.request.method === 'GET' && event.request.headers.get('accept').includes('text/html');
const isAssetToCache = (asset) => {
const isAssetWithVersionNumber = asset.url.includes('/assets/') && asset.url.includes('?v');
return (
!(asset.url.includes('www.google-analytics.com') || isAssetWithVersionNumber)
);
};
const updateCache = (cacheName) => {
const isPreCacheAsset = cacheName.includes('precache') && cacheName !== PRECACHE_NAME;
return isPreCacheAsset && caches.delete(cacheName);
};
/**
* Cache data from PRECACHE_ASSETS.
*/
self.addEventListener('install', (event) => {
self.skipWaiting();
event.waitUntil(
caches.open(PRECACHE_NAME)
.then((cache) => {
cache.addAll(PRECACHE_ASSETS);
}),
);
});
/**
* Update service worker and remove old cache, if it's necessary.
*/
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys()
.then((cacheNames) => Promise.all(cacheNames.map(
(currentCacheName) => updateCache(currentCacheName),
))),
);
});
/**
* Return cached asset if exists.
* Cache asset if isn't in cache.
*/
self.addEventListener('fetch', (event) => {
const asset = event.request;
event.respondWith(
caches.match(asset)
.then((cachedResponse) => cachedResponse || fetch(asset).then(
(response) => {
if (isAssetToCache(asset)) {
const responseToCache = response.clone();
caches.open(PRECACHE_NAME)
.then((cache) => {
cache.put(asset, responseToCache);
});
}
return response;
},
)
.catch((error) => {
if (isHtmlPage(event)) {
return caches.match(OFFLINE_URL);
}
return error;
})),
);
});