Skip to content
This repository has been archived by the owner on Jul 23, 2024. It is now read-only.

Commit

Permalink
feat(): add media focused service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
jgw96 committed Mar 7, 2020
1 parent 17a38ec commit 220133e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
19 changes: 19 additions & 0 deletions serviceWorker7/pwabuilder-sw-register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// This is the "serving cached media" service worker

// Add this below content to your HTML page, or add the js file to your page at the very top to register service worker

// Check compatibility for the browser we're running this in
if ("serviceWorker" in navigator) {
if (navigator.serviceWorker.controller) {
console.log("[PWA Builder] active service worker found, no need to register");
} else {
// Register the service worker
navigator.serviceWorker
.register("pwabuilder-sw.js", {
scope: "./"
})
.then(function (reg) {
console.log("[PWA Builder] Service worker has been registered for scope: " + reg.scope);
});
}
}
25 changes: 25 additions & 0 deletions serviceWorker7/pwabuilder-sw.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// This is the "serving cached media" service worker

const CACHE = "pwabuilder-offline";

importScripts('https://storage.googleapis.com/workbox-cdn/releases/5.0.0/workbox-sw.js');

self.addEventListener("message", (event) => {
if (event.data && event.data.type === "SKIP_WAITING") {
self.skipWaiting();
}
});

workbox.loadModule('workbox-cacheable-response');
workbox.loadModule('workbox-range-requests');

workbox.routing.registerRoute(
/.*\.mp4/,
new workbox.strategies.CacheFirst({
cacheName: CACHE,
plugins: [
new workbox.cacheableResponse.CacheableResponsePlugin({statuses: [200]}),
new workbox.rangeRequests.RangeRequestsPlugin(),
],
}),
);

0 comments on commit 220133e

Please sign in to comment.