-
Notifications
You must be signed in to change notification settings - Fork 9
/
service-worker.js.phtml
162 lines (145 loc) · 4.5 KB
/
service-worker.js.phtml
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
<?php
/**
* Copyright © Monsoon Consulting. All rights reserved.
* @license Open Software License ("OSL") v. 3.0
*/
/** @var $block \Magento\Framework\View\Element\Template */
/** @var $viewModel Monsoon\Pwa\ViewModel\ServiceWorker */
$viewModel = $block->getViewModel();
?>
importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.1.2/workbox-sw.js');
const SW_VERSION = <?= $block->escapeJs($viewModel->getSwVersion()) ?>;
const CACHE_LIFETIME = <?= $block->escapeJs($viewModel->getCacheLifetime()) ?>;
const {registerRoute, NavigationRoute, setDefaultHandler, setCatchHandler} = workbox.routing;
const {CacheFirst, NetworkOnly, StaleWhileRevalidate} = workbox.strategies;
const {CacheableResponsePlugin} = workbox.cacheableResponse;
const {ExpirationPlugin} = workbox.expiration;
<?php if ($viewModel->isSkipWaitingEnabled()): ?>
const {skipWaiting} = workbox.core;
skipWaiting();
<?php endif ?>
<?php if ($viewModel->isClientsClaimEnabled()): ?>
const {clientsClaim} = workbox.core;
clientsClaim();
<?php endif ?>
/**
* Pre-cache fallback responses
* https://developers.google.com/web/tools/workbox/guides/advanced-recipes#offline_page_only
*/
const OFFLINE_CACHE_NAME = 'offline-html';
const OFFLINE_FALLBACK_HTML_URL = '<?= $block->escapeUrl($viewModel->getOfflinePageUrl()) ?>';
/**
* Populate the cache with the offline HTML page when the service worker is installed.
*/
self.addEventListener('install', async (event) => {
event.waitUntil(
caches.open(OFFLINE_CACHE_NAME)
.then((cache) => cache.add(OFFLINE_FALLBACK_HTML_URL))
);
});
workbox.navigationPreload.enable();
const networkOnly = new NetworkOnly();
/**
* Attempt a network request. If it fails, return the cached HTML.
*/
const navigationHandler = async (params) => {
try {
return await networkOnly.handle(params);
} catch (error) {
return caches.match(OFFLINE_FALLBACK_HTML_URL, {
cacheName: OFFLINE_CACHE_NAME,
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: CACHE_LIFETIME,
maxEntries: 1
})
]
});
}
};
/**
* Register this strategy to handle all navigations.
*/
registerRoute(new NavigationRoute(navigationHandler));
<?php if ($viewModel->isGaOfflineEnabled()): ?>
workbox.googleAnalytics.initialize();
<?php endif ?>
<?php if ($viewModel->isCacheStaticAssetsEnabled()): ?>
registerRoute(
/^(.*)(static)(.*)(.(png|jpeg|jpg|gif|svg|css|js|woff|woff2|ttf|eot|ico|json))$/,
new StaleWhileRevalidate({
cacheName: 'static-assets',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: CACHE_LIFETIME,
maxEntries: 1000
})
]
})
);
<?php endif ?>
<?php if ($viewModel->isCacheMediaAssetsEnabled()): ?>
registerRoute(
/^(.*)(media)(.*)(.(png|jpeg|jpg|gif|css|js|svg|pdf))$/,
new StaleWhileRevalidate({
cacheName: 'media-assets',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: CACHE_LIFETIME,
maxEntries: 5000
})
]
})
);
<?php endif ?>
<?php if ($viewModel->isCacheGoogleFontsEnabled()): ?>
/**
* Cache fonts
* https://developers.google.com/web/tools/workbox/guides/common-recipes#google_fonts
*/
registerRoute(function (_ref) {
var url = _ref.url;
return url.origin === 'https://fonts.gstatic.com';
}, new CacheFirst({
cacheName: 'google-fonts-webfonts',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: CACHE_LIFETIME,
maxEntries: 30
})
]
}));
registerRoute(
function (_ref) {
var url = _ref.url;
return url.origin === 'https://fonts.googleapis.com';
},
new StaleWhileRevalidate({
cacheName: 'google-fonts-stylesheets',
plugins: [
new CacheableResponsePlugin({
statuses: [0, 200]
}),
new ExpirationPlugin({
maxAgeSeconds: CACHE_LIFETIME,
maxEntries: 30
})
]
})
);
<?php endif ?>
<?php if ($block->getChildHtml()): ?>
<?= $block->getChildHtml() ?>
<?php endif ?>