-
Notifications
You must be signed in to change notification settings - Fork 914
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
used async/syntax for better readability & performance #4144
Open
samar-703
wants to merge
1
commit into
sugarlabs:master
Choose a base branch
from
samar-703:update-scripts
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -1,130 +1,96 @@ | ||
/* | ||
global | ||
|
||
offlineFallbackPage, divInstall | ||
global offlineFallbackPage, divInstall | ||
*/ | ||
|
||
// This is the "Offline page" service worker | ||
|
||
const CACHE = "pwabuilder-precache"; | ||
const precacheFiles = [ | ||
/* Add an array of files to precache for your app */ | ||
"./index.html" | ||
]; | ||
const precacheFiles = ["./index.html"]; | ||
|
||
self.addEventListener("install", function (event) { | ||
// eslint-disable-next-line no-console | ||
// Install event | ||
self.addEventListener("install", event => { | ||
console.log("[PWA Builder] Install Event processing"); | ||
|
||
// eslint-disable-next-line no-console | ||
console.log("[PWA Builder] Skip waiting on install"); | ||
self.skipWaiting(); | ||
|
||
event.waitUntil( | ||
caches.open(CACHE).then(function (cache) { | ||
// eslint-disable-next-line no-console | ||
caches.open(CACHE).then(cache => { | ||
console.log("[PWA Builder] Caching pages during install"); | ||
return cache.addAll(precacheFiles); | ||
}) | ||
); | ||
}); | ||
|
||
// Allow sw to control of current page | ||
self.addEventListener("activate", function (event) { | ||
// eslint-disable-next-line no-console | ||
// Activate event | ||
self.addEventListener("activate", event => { | ||
console.log("[PWA Builder] Claiming clients for current page"); | ||
event.waitUntil(self.clients.claim()); | ||
}); | ||
|
||
function updateCache(request, response) { | ||
// Helper functions | ||
const updateCache = async (request, response) => { | ||
if (response.status === 206) { | ||
console.log("Partial response is unsupported for caching."); | ||
return Promise.resolve(); | ||
return; | ||
} | ||
return caches.open(CACHE).then(function (cache) { | ||
return cache.put(request, response); | ||
}); | ||
} | ||
|
||
function fromCache(request) { | ||
// Check to see if you have it in the cache | ||
// Return response | ||
// If not in the cache, then return | ||
return caches.open(CACHE).then(function (cache) { | ||
return cache.match(request).then(function (matching) { | ||
if (!matching || matching.status === 404) { | ||
return Promise.reject("no-match"); | ||
} | ||
|
||
return matching; | ||
}); | ||
}); | ||
} | ||
const cache = await caches.open(CACHE); | ||
return cache.put(request, response); | ||
}; | ||
|
||
const fromCache = async request => { | ||
const cache = await caches.open(CACHE); | ||
const matching = await cache.match(request); | ||
if (!matching || matching.status === 404) { | ||
throw "no-match"; | ||
} | ||
return matching; | ||
}; | ||
|
||
// If any fetch fails, it will look for the request in the cache and | ||
// serve it from there first | ||
self.addEventListener("fetch", function (event) { | ||
// Fetch event | ||
self.addEventListener("fetch", event => { | ||
if (event.request.method !== "GET") return; | ||
|
||
event.respondWith( | ||
fromCache(event.request).then( | ||
function (response) { | ||
// The response was found in the cache so we responde | ||
// with it and update the entry | ||
|
||
// This is where we call the server to get the newest | ||
// version of the file to use the next time we show view | ||
event.waitUntil( | ||
fetch(event.request).then(function (response) { | ||
if (response.ok) { | ||
return updateCache(event.request, response); | ||
} | ||
}) | ||
); | ||
|
||
return response; | ||
}, | ||
async function () { | ||
// The response was not found in the cache so we look | ||
// for it on the server | ||
try { | ||
const response = await fetch(event.request); | ||
// If request was success, add or update it in the cache | ||
event.respondWith((async () => { | ||
try { | ||
// Try to get from cache first | ||
const cachedResponse = await fromCache(event.request); | ||
|
||
// Update cache in background | ||
event.waitUntil( | ||
fetch(event.request).then(response => { | ||
if (response.ok) { | ||
event.waitUntil(updateCache(event.request, response.clone())); | ||
return updateCache(event.request, response); | ||
} | ||
return response; | ||
} catch (error) { | ||
// eslint-disable-next-line no-console | ||
console.log("[PWA Builder] Network request failed and no cache." + error); | ||
}) | ||
); | ||
|
||
return cachedResponse; | ||
} catch { | ||
// Network fetch if not in cache | ||
try { | ||
const response = await fetch(event.request); | ||
if (response.ok) { | ||
event.waitUntil(updateCache(event.request, response.clone())); | ||
} | ||
return response; | ||
} catch (error) { | ||
console.log("[PWA Builder] Network request failed and no cache." + error); | ||
} | ||
) | ||
); | ||
} | ||
})()); | ||
}); | ||
|
||
self.addEventListener("beforeinstallprompt", (event) => { | ||
// eslint-disable-next-line no-console | ||
// Install prompt | ||
self.addEventListener("beforeinstallprompt", event => { | ||
console.log("done", "beforeinstallprompt", event); | ||
// Stash the event so it can be triggered later. | ||
window.deferredPrompt = event; | ||
// Remove the "hidden" class from the install button container | ||
divInstall.classList.toggle("hidden", false); | ||
}); | ||
|
||
// This is an event that can be fired from your page to tell the SW to | ||
// update the offline page | ||
self.addEventListener("refreshOffline", function () { | ||
// Offline page update | ||
self.addEventListener("refreshOffline", () => { | ||
const offlinePageRequest = new Request(offlineFallbackPage); | ||
|
||
return fetch(offlineFallbackPage).then(function (response) { | ||
return caches.open(CACHE).then(function (cache) { | ||
// eslint-disable-next-line no-console | ||
return fetch(offlineFallbackPage).then(response => { | ||
return caches.open(CACHE).then(cache => { | ||
console.log("[PWA Builder] Offline page updated from refreshOffline event: " + response.url); | ||
return cache.put(offlinePageRequest, response); | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you remove this line?