Flipper as PWA
Summary: ^ Reference: https://docs.google.com/document/d/1flQJUzTe4AuQz3QCpvbloQycenHsu7ZxbKScov7K7ao Reviewed By: passy Differential Revision: D45693382 fbshipit-source-id: 5a2e6c213a7e7e2cf9cd5f3033cff3e5291a2a92
This commit is contained in:
committed by
Facebook GitHub Bot
parent
47a4c10c67
commit
c6d5eb3334
70
desktop/static/service-worker.js
Normal file
70
desktop/static/service-worker.js
Normal file
@@ -0,0 +1,70 @@
|
||||
/**
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
// OFFLINE_VERSION is used as an update marker so that on subsequent installations
|
||||
// the newer version of the file gets updated.
|
||||
// eslint-disable-next-line header/header, no-unused-vars
|
||||
const OFFLINE_VERSION = 1;
|
||||
const CACHE_NAME = 'offline';
|
||||
const OFFLINE_URL = 'offline.html';
|
||||
|
||||
self.addEventListener('install', (event) => {
|
||||
console.log('Flipper service worker installed');
|
||||
|
||||
event.waitUntil((async () => {
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
// Setting {cache: 'reload'} in the new request will ensure that the response
|
||||
// isn't fulfilled from the HTTP cache; i.e., it will be from the network.
|
||||
await cache.add(new Request(OFFLINE_URL, {cache: 'reload'}));
|
||||
})());
|
||||
// Force the waiting service worker to become the active service worker.
|
||||
self.skipWaiting();
|
||||
});
|
||||
|
||||
self.addEventListener('activate', (event) => {
|
||||
console.log('Flipper service worker activate');
|
||||
event.waitUntil((async () => {
|
||||
// Enable navigation preload if it's supported.
|
||||
// See https://developers.google.com/web/updates/2017/02/navigation-preload
|
||||
if ('navigationPreload' in self.registration) {
|
||||
await self.registration.navigationPreload.enable();
|
||||
}
|
||||
})());
|
||||
|
||||
// Tell the active service worker to take control of the page immediately.
|
||||
self.clients.claim();
|
||||
});
|
||||
|
||||
self.addEventListener('fetch', (event) => {
|
||||
// We only want to call event.respondWith() if this is a navigation request
|
||||
// for an HTML page.
|
||||
if (event.request.mode === 'navigate') {
|
||||
event.respondWith((async () => {
|
||||
try {
|
||||
// First, try to use the navigation preload response if it's supported.
|
||||
const preloadResponse = await event.preloadResponse;
|
||||
if (preloadResponse) {
|
||||
return preloadResponse;
|
||||
}
|
||||
|
||||
// Always try the network first (try flipper server)
|
||||
const networkResponse = await fetch(event.request);
|
||||
return networkResponse;
|
||||
} catch (error) {
|
||||
// Catch is only triggered if an exception is thrown, which is likely
|
||||
// due to a network error.
|
||||
// If fetch() returns a valid HTTP response with a response code in
|
||||
// the 4xx or 5xx range, the catch() will NOT be called.
|
||||
console.log('Fetch failed; returning offline page instead.', error);
|
||||
|
||||
const cache = await caches.open(CACHE_NAME);
|
||||
const cachedResponse = await cache.match(OFFLINE_URL);
|
||||
return cachedResponse;
|
||||
}
|
||||
})());
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user