This repository has been archived by the owner on Jul 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(): add media focused service worker
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
], | ||
}), | ||
); |