-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add service worker for offline compatibility (#115)
For the moment this is opt in via flag=sw on the URL so existing embedders won't be affected. The main motivation here is to experiment with PWA support in micro:bit Python Editor which is being worked on separately.
- Loading branch information
1 parent
6204a0b
commit 9d81538
Showing
8 changed files
with
159 additions
and
5 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type Stage = "local" | "REVIEW" | "STAGING" | "PRODUCTION"; | ||
|
||
export const stage = (process.env.STAGE || "local") as Stage; |
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 |
---|---|---|
@@ -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 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 |
---|---|---|
@@ -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 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