summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js')
-rw-r--r--testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js55
1 files changed, 55 insertions, 0 deletions
diff --git a/testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js b/testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js
new file mode 100644
index 0000000000..5720e3cbc3
--- /dev/null
+++ b/testing/web-platform/tests/appmanifest/display-member/display-member-media-feature-service-worker.js
@@ -0,0 +1,55 @@
+// Some user agents only offer app installation if there is a SW and it handles
+// offline requests.
+
+const cacheVersion = "1.2";
+const CACHE_NAME = `cache-v${cacheVersion}`;
+
+// The resources cached by this service worker.
+const resources = [
+ "display-member-media-feature-browser-manual.html",
+ "display-member-media-feature-fullscreen-manual.html",
+ "display-member-media-feature-minimal-ui-manual.html",
+ "display-member-media-feature-standalone-manual.html",
+ "display-member-media-feature-service-worker.js",
+ "resources/display-member-media-feature-manual.js",
+ "resources/icon.png",
+];
+
+// Load all resources for this service worker.
+const precache = async () => {
+ const cache = await caches.open(CACHE_NAME);
+ await cache.addAll(resources);
+};
+
+// Get a resource from the cache.
+const fromCache = async request => {
+ const cache = await caches.open(CACHE_NAME);
+ return await cache.match(request.url);
+};
+
+// Attempt to get resources from the network first, fallback to the cache if we're
+// offline.
+const networkFallbackToCache = async request => {
+ try {
+ const response = await fetch(request);
+ if (response.ok) return response;
+ } catch (err) {}
+ return await fromCache(request);
+};
+
+// When we have a new service worker, update the caches and swap immediately.
+self.addEventListener("install", e => {
+ e.waitUntil(precache().then(() => self.skipWaiting()));
+});
+
+// Claim existing clients.
+self.addEventListener("activate", e => {
+ e.waitUntil(self.clients.claim());
+});
+
+// When a resource need to be fetched, check whether it is
+// contained in the cache and return the cached version, otherwise
+// get it from the network.
+self.addEventListener("fetch", e => {
+ e.respondWith(networkFallbackToCache(e.request));
+});