-
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 13 commits
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
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,3 @@ | ||
export type Stage = "local" | "REVIEW" | "STAGING" | "PRODUCTION"; | ||
|
||
export const stage = (process.env.STAGE || "local") as Stage; |
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,56 @@ | ||
import { Stage, stage as stageFromEnvironment } from "./environment"; | ||
|
||
/** | ||
* A union of the flag names (alphabetical order). | ||
*/ | ||
export type Flag = | ||
/** | ||
* Enables service worker registration. | ||
* | ||
* Registers the service worker and enables offline use. | ||
*/ | ||
"sw"; | ||
|
||
interface FlagMetadata { | ||
defaultOnStages: Stage[]; | ||
name: Flag; | ||
} | ||
|
||
const allFlags: FlagMetadata[] = [{ name: "sw", defaultOnStages: [] }]; | ||
|
||
type Flags = Record<Flag, boolean>; | ||
|
||
const flagsForParams = (stage: Stage, params: URLSearchParams) => { | ||
const enableFlags = new Set(params.getAll("flag")); | ||
const allFlagsDefault = enableFlags.has("none") | ||
? false | ||
: enableFlags.has("*") | ||
? true | ||
: undefined; | ||
return Object.fromEntries( | ||
allFlags.map((f) => [ | ||
f.name, | ||
isEnabled(f, stage, allFlagsDefault, enableFlags.has(f.name)), | ||
]) | ||
) as Flags; | ||
}; | ||
|
||
const isEnabled = ( | ||
f: FlagMetadata, | ||
stage: Stage, | ||
allFlagsDefault: boolean | undefined, | ||
thisFlagOn: boolean | ||
): boolean => { | ||
if (thisFlagOn) { | ||
return true; | ||
} | ||
if (allFlagsDefault !== undefined) { | ||
return allFlagsDefault; | ||
} | ||
return f.defaultOnStages.includes(stage); | ||
}; | ||
|
||
export const flags: Flags = (() => { | ||
const params = new URLSearchParams(window.location.search); | ||
return flagsForParams(stageFromEnvironment, params); | ||
})(); |
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,51 @@ | ||
/// <reference lib="WebWorker" /> | ||
// Empty export required due to --isolatedModules flag in tsconfig.json | ||
export type {}; | ||
declare const self: ServiceWorkerGlobalScope; | ||
declare const clients: Clients; | ||
|
||
const assets = ["simulator.html", "build/simulator.js", "build/firmware.js", "build/firmware.wasm"]; | ||
const cacheName = `simulator-${process.env.VERSION}`; | ||
|
||
self.addEventListener("install", (event) => { | ||
console.log("Installing simulator service worker..."); | ||
self.skipWaiting(); | ||
event.waitUntil( | ||
(async () => { | ||
const cache = await caches.open(cacheName); | ||
await cache.addAll(assets); | ||
})() | ||
); | ||
}); | ||
|
||
self.addEventListener("activate", (event) => { | ||
console.log("Activating simulator service worker..."); | ||
event.waitUntil( | ||
(async () => { | ||
const names = await caches.keys(); | ||
await Promise.all( | ||
names.map((name) => { | ||
if (/^simulator-/.test(name) && name !== cacheName) { | ||
return caches.delete(name); | ||
} | ||
}) | ||
); | ||
await clients.claim(); | ||
})() | ||
); | ||
}); | ||
|
||
self.addEventListener("fetch", (event) => { | ||
event.respondWith( | ||
(async () => { | ||
const cachedResponse = await caches.match(event.request); | ||
if (cachedResponse) { | ||
return cachedResponse; | ||
} | ||
const response = await fetch(event.request); | ||
const cache = await caches.open(cacheName); | ||
cache.put(event.request, response.clone()); | ||
return response; | ||
})() | ||
); | ||
}); |
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
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.