-
Notifications
You must be signed in to change notification settings - Fork 10
Add service worker for offline compatibility #115
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
0090371
Add first draft of service worker
microbit-robert 538355f
Update service worker, add version control
microbit-robert 4c724eb
Service worker tweaks
microbit-robert 07b703b
Auto reload the page when new service worker is installed
microbit-robert 7075a41
Bump GitHub actions versions
microbit-robert d74a44f
Service worker in TypeScript with versioning
microbit-robert 328948b
Improve sw versioning, try flags with stage
microbit-robert 5dbbe0c
Build fix regarding version
microbit-robert 1524cb8
Fix version code again
microbit-robert 1331423
Nicer version parsing
microbit-robert 2063919
Add firmware.wasm to service worker assets list
microbit-robert 6134ebd
Additional check on cache name before deletion.
microbit-robert e35db80
Add code to unregister service worker if flag is not used
microbit-robert a1c8cff
Move navigator.serviceWorker check
microbit-robert 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
function initSimulatorServiceWorker() { | ||
const simUrls = ["simulator.html", "build/simulator.js", "build/firmware.js"]; | ||
let didInstall = false; | ||
const cacheName = "simulator"; | ||
|
||
self.addEventListener("install", function (ev) { | ||
didInstall = true; | ||
console.log("Installing service worker..."); | ||
ev.waitUntil( | ||
caches | ||
.open(cacheName) | ||
.then(function (cache) { | ||
console.log("Opened cache"); | ||
return cache.addAll(simUrls); | ||
}) | ||
.then(function () { | ||
return self.skipWaiting(); | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener("activate", function (ev) { | ||
console.log("Activating service worker..."); | ||
ev.waitUntil( | ||
caches | ||
.keys() | ||
.then(function (_cacheNames) { | ||
// Delete old versions in cache here. | ||
microbit-robert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
.then(function () { | ||
if (didInstall) { | ||
// Only notify clients for the first activation | ||
didInstall = false; | ||
// Necessary? | ||
return notifyAllClientsAsync(); | ||
microbit-robert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
return Promise.resolve(); | ||
}) | ||
); | ||
}); | ||
|
||
self.addEventListener("fetch", function (ev) { | ||
ev.respondWith( | ||
caches.match(ev.request).then(function (response) { | ||
return response || fetch(ev.request); | ||
}) | ||
); | ||
}); | ||
|
||
function notifyAllClientsAsync() { | ||
var scope = self; | ||
return scope.clients | ||
.claim() | ||
.then(function () { | ||
return scope.clients.matchAll(); | ||
}) | ||
.then(function (clients) { | ||
clients.forEach(function (client) { | ||
return client.postMessage({ | ||
type: "serviceworker", | ||
state: "activated", | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
initSimulatorServiceWorker(); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.