You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
samples/web/github_dataviz/flutter_service_worker.js

161 lines
6.0 KiB

'use strict';
const MANIFEST = 'flutter-app-manifest';
const TEMP = 'flutter-temp-cache';
const CACHE_NAME = 'flutter-app-cache';
const RESOURCES = {
"index.html": "51839f812a32cce7ac40ca488295da30",
"/": "51839f812a32cce7ac40ca488295da30",
"main.dart.js": "29b3e1963aac9c8ce7dfb5dfe7edc287",
"assets/AssetManifest.json": "7ac1367789abe6eed8ffe5db603b0604",
"assets/NOTICES": "bd3ce89fe7d5ad0e1cb691fa0e9d57e0",
"assets/FontManifest.json": "d751713988987e9331980363e24189ce",
"assets/assets/preview.png": "f41b57a2a2dab8d74ade52f9175b4a59",
"assets/github_data/contributors.json": "4f7b5d7640ca5ab67ee419df55a4833f",
"assets/github_data/comments.tsv": "062efd1b6e583ce9b71fd1c655f101cd",
"assets/github_data/forks.tsv": "307c19f9166eb30ffe34a8de50ac7b57",
"assets/github_data/commits.tsv": "684e345ed4d734f5b4a890098d175b37",
"assets/github_data/pull_requests.tsv": "583f0c3151b9234502a7037bf9735494",
"assets/github_data/stars.tsv": "f949dd28b49c2374880c22b2c8937a76"
};
// The application shell files that are downloaded before a service worker can
// start.
const CORE = [
"/",
"main.dart.js",
"index.html",
"assets/LICENSE",
"assets/AssetManifest.json",
"assets/FontManifest.json"];
// During install, the TEMP cache is populated with the application shell files.
self.addEventListener("install", (event) => {
return event.waitUntil(
caches.open(TEMP).then((cache) => {
// Provide a no-cache param to ensure the latest version is downloaded.
return cache.addAll(CORE.map((value) => new Request(value, {'cache': 'no-cache'})));
})
);
});
// During activate, the cache is populated with the temp files downloaded in
// install. If this service worker is upgrading from one with a saved
// MANIFEST, then use this to retain unchanged resource files.
self.addEventListener("activate", function(event) {
return event.waitUntil(async function() {
try {
var contentCache = await caches.open(CACHE_NAME);
var tempCache = await caches.open(TEMP);
var manifestCache = await caches.open(MANIFEST);
var manifest = await manifestCache.match('manifest');
// When there is no prior manifest, clear the entire cache.
if (!manifest) {
await caches.delete(CACHE_NAME);
contentCache = await caches.open(CACHE_NAME);
for (var request of await tempCache.keys()) {
var response = await tempCache.match(request);
await contentCache.put(request, response);
}
await caches.delete(TEMP);
// Save the manifest to make future upgrades efficient.
await manifestCache.put('manifest', new Response(JSON.stringify(RESOURCES)));
return;
}
var oldManifest = await manifest.json();
var origin = self.location.origin;
for (var request of await contentCache.keys()) {
var key = request.url.substring(origin.length + 1);
if (key == "") {
key = "/";
}
// If a resource from the old manifest is not in the new cache, or if
// the MD5 sum has changed, delete it. Otherwise the resource is left
// in the cache and can be reused by the new service worker.
if (!RESOURCES[key] || RESOURCES[key] != oldManifest[key]) {
await contentCache.delete(request);
}
}
// Populate the cache with the app shell TEMP files, potentially overwriting
// cache files preserved above.
for (var request of await tempCache.keys()) {
var response = await tempCache.match(request);
await contentCache.put(request, response);
}
await caches.delete(TEMP);
// Save the manifest to make future upgrades efficient.
await manifestCache.put('manifest', new Response(JSON.stringify(RESOURCES)));
return;
} catch (err) {
// On an unhandled exception the state of the cache cannot be guaranteed.
console.error('Failed to upgrade service worker: ' + err);
await caches.delete(CACHE_NAME);
await caches.delete(TEMP);
await caches.delete(MANIFEST);
}
}());
});
// The fetch handler redirects requests for RESOURCE files to the service
// worker cache.
self.addEventListener("fetch", (event) => {
var origin = self.location.origin;
var key = event.request.url.substring(origin.length + 1);
// Redirect URLs to the index.html
if (event.request.url == origin || event.request.url.startsWith(origin + '/#')) {
key = '/';
}
// If the URL is not the the RESOURCE list, skip the cache.
if (!RESOURCES[key]) {
return event.respondWith(fetch(event.request));
}
event.respondWith(caches.open(CACHE_NAME)
.then((cache) => {
return cache.match(event.request).then((response) => {
// Either respond with the cached resource, or perform a fetch and
// lazily populate the cache. Ensure the resources are not cached
// by the browser for longer than the service worker expects.
var modifiedRequest = new Request(event.request, {'cache': 'no-cache'});
return response || fetch(modifiedRequest).then((response) => {
cache.put(event.request, response.clone());
return response;
});
})
})
);
});
self.addEventListener('message', (event) => {
// SkipWaiting can be used to immediately activate a waiting service worker.
// This will also require a page refresh triggered by the main worker.
if (event.message == 'skipWaiting') {
return self.skipWaiting();
}
if (event.message = 'downloadOffline') {
downloadOffline();
}
});
// Download offline will check the RESOURCES for all files not in the cache
// and populate them.
async function downloadOffline() {
var resources = [];
var contentCache = await caches.open(CACHE_NAME);
var currentContent = {};
for (var request of await contentCache.keys()) {
var key = request.url.substring(origin.length + 1);
if (key == "") {
key = "/";
}
currentContent[key] = true;
}
for (var resourceKey in Object.keys(RESOURCES)) {
if (!currentContent[resourceKey]) {
resources.add(resourceKey);
}
}
return Cache.addAll(resources);
}