Skip to content

Instantly share code, notes, and snippets.

@Schepp
Last active April 16, 2018 11:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schepp/adbc1579e8d1a38087e087a40b205322 to your computer and use it in GitHub Desktop.
Save Schepp/adbc1579e8d1a38087e087a40b205322 to your computer and use it in GitHub Desktop.
This serviceworker doesn't work for Samsung Internet Browser
importScripts('https://storage.googleapis.com/workbox-cdn/releases/3.0.0/workbox-sw.js');
workbox.setConfig({ debug: false });
workbox.routing.registerRoute(/\/assets\/.*\.(?:js|css|woff|woff2|png|svg|json)(\?v=.*|)$/, workbox.strategies.cacheFirst({
cacheName: 'assets',
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxEntries: 100,
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 604800,
}),
],
}), 'GET');
workbox.routing.registerRoute(/\.(?:png|svg)$/, workbox.strategies.networkFirst({
cacheName: 'images',
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxEntries: 100,
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 604800,
}),
],
}), 'GET');
const documentCacheOptions = {
cacheName: 'documents',
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
new workbox.expiration.Plugin({
maxEntries: 100,
}),
new workbox.expiration.Plugin({
maxAgeSeconds: 3600,
}),
],
};
workbox.routing.registerRoute(/^$/, workbox.strategies.networkFirst(documentCacheOptions), 'GET');
workbox.routing.registerRoute(/^.*\/$/, workbox.strategies.networkFirst(documentCacheOptions), 'GET');
workbox.routing.registerRoute(/\.(?:html)$/, workbox.strategies.networkFirst(documentCacheOptions), 'GET');
workbox.routing.registerRoute(/_(?:aid|bid|vid)$/, workbox.strategies.networkFirst(documentCacheOptions), 'GET');
const thirdPartyCacheOptions = {
plugins: [
new workbox.cacheableResponse.Plugin({
statuses: [0, 200],
}),
],
};
workbox.routing.registerRoute(/^https:\/\/script.ioam.de\/iam.js/, workbox.strategies.staleWhileRevalidate(thirdPartyCacheOptions), 'GET');
workbox.routing.registerRoute(/^https:\/\/www.googletagservices.com\/tag\/js\/gpt.js/, workbox.strategies.staleWhileRevalidate(thirdPartyCacheOptions), 'GET');
workbox.routing.registerRoute(/^https:\/\/www.youtube.com\/iframe_api/, workbox.strategies.staleWhileRevalidate(thirdPartyCacheOptions), 'GET');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment