Added back offline mode

This commit is contained in:
2018-04-18 01:08:04 +02:00
parent f0007f9438
commit f522caede0
6 changed files with 377 additions and 251 deletions

View File

@ -0,0 +1,99 @@
// pack:ignore
// onDOMContentLoaded(url: string) {
// let refresh = serviceWorker.reloads.get(url)
// let servedETag = ETAGS.get(url)
// // If the user requests a sub-page we should prefetch the full page, too.
// if(url.includes("/_/") && !url.includes("/_/search/")) {
// var prefetch = true
// for(let pattern of EXCLUDECACHE.keys()) {
// if(url.includes(pattern)) {
// prefetch = false
// break
// }
// }
// if(prefetch) {
// this.prefetchFullPage(url)
// }
// }
// if(!refresh || !servedETag) {
// return Promise.resolve()
// }
// return refresh.then(async (response: Response) => {
// // When the actual network request was used by the client, response.bodyUsed is set.
// // In that case the client is already up to date and we don"t need to tell the client to do a refresh.
// if(response.bodyUsed) {
// return
// }
// // Get the ETag of the cached response we sent to the client earlier.
// let eTag = response.headers.get("ETag")
// // Update ETag
// ETAGS.set(url, eTag)
// // If the ETag changed, we need to do a reload.
// if(eTag !== servedETag) {
// return this.reloadContent(url)
// }
// // Do nothing
// return Promise.resolve()
// })
// }
// prefetchFullPage(url: string) {
// let fullPage = new Request(url.replace("/_/", "/"))
// let fullPageRefresh = fetch(fullPage, {
// credentials: "same-origin"
// }).then(response => {
// // Save the new version of the resource in the cache
// let cacheRefresh = caches.open(serviceWorker.cache.version).then(cache => {
// return cache.put(fullPage, response)
// })
// CACHEREFRESH.set(fullPage.url, cacheRefresh)
// return response
// })
// // Save in map
// serviceWorker.reloads.set(fullPage.url, fullPageRefresh)
// }
// async reloadContent(url: string) {
// let cacheRefresh = CACHEREFRESH.get(url)
// if(cacheRefresh) {
// await cacheRefresh
// }
// return this.postMessage({
// type: "new content",
// url
// })
// }
// async reloadPage(url: string) {
// let networkFetch = serviceWorker.reloads.get(url.replace("/_/", "/"))
// if(networkFetch) {
// await networkFetch
// }
// return this.postMessage({
// type: "reload page",
// url
// })
// }
// reloadStyles() {
// return this.postMessage({
// type: "reload styles"
// })
// }

View File

@ -0,0 +1,69 @@
// pack:ignore
// // Clear cache on authentication and fetch it normally
// if(request.url.includes("/auth/") || request.url.includes("/logout")) {
// return evt.respondWith(caches.delete(this.cache.version).then(() => fetch(request)))
// }
// // Exclude certain URLs from being cached
// for(let pattern of EXCLUDECACHE.keys()) {
// if(request.url.includes(pattern)) {
// return evt.respondWith(this.fromNetwork(request))
// }
// }
// // If the request included the header "X-CacheOnly", return a cache-only response.
// // This is used in reloads to avoid generating a 2nd request after a cache refresh.
// if(request.headers.get("X-CacheOnly") === "true") {
// return evt.respondWith(this.fromCache(request))
// }
// // Save the served E-Tag when onResponse is called
// let servedETag = undefined
// let onResponse = (response: Response | null) => {
// if(response) {
// servedETag = response.headers.get("ETag")
// ETAGS.set(request.url, servedETag)
// }
// return response
// }
// let saveResponseInCache = response => {
// let clone = response.clone()
// // Save the new version of the resource in the cache
// let cacheRefresh = this.cache.store(request, clone).catch(err => {
// console.error(err)
// // TODO: Tell client that the quota is exceeded (disk full).
// })
// CACHEREFRESH.set(request.url, cacheRefresh)
// return response
// }
// // Start fetching the request
// let network =
// fetch(request)
// .then(saveResponseInCache)
// .catch(error => {
// console.log("Fetch error:", error)
// throw error
// })
// // Save in map
// this.reloads.set(request.url, network)
// if(request.headers.get("X-Reload") === "true") {
// return evt.respondWith(network)
// }
// // Scripts and styles are server pushed on the initial response
// // so we can use a network-first response without an additional round-trip.
// // This causes the browser to always load the most recent scripts and styles.
// if(request.url.endsWith("/styles") || request.url.endsWith("/scripts")) {
// return evt.respondWith(this.networkFirst(request, network, onResponse))
// }
// return evt.respondWith(this.cacheFirst(request, network, onResponse))