Skip to content

Commit

Permalink
Adding service worker support
Browse files Browse the repository at this point in the history
  • Loading branch information
jakearchibald committed Aug 6, 2019
1 parent b2c71b7 commit 76f9e19
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/main/services/preact-canvas/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ export default class Root extends Component<Props, State> {
this._handleCurrentURL();
}

private _resetToStartScreen() {
private async _resetToStartScreen() {
history.replaceState({}, "", "/" + location.search);
this.setState(
{
Expand All @@ -456,7 +456,8 @@ export default class Root extends Component<Props, State> {
}
}
);
this._stateService!.reset();
const stateService = await stateServicePromise;
stateService.reset();
}

private async _handleCurrentURL() {
Expand Down
19 changes: 18 additions & 1 deletion src/sw/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,26 @@ self.addEventListener("fetch", event => {
}
event.respondWith(
(async function() {
const cachedResponse = await caches.match(event.request, {
let cachedResponse: Response | undefined;

// Handle the URLs that just go to the root page
if (event.request.mode === "navigate") {
const url = new URL(event.request.url);
if (
url.pathname === "/" ||
url.pathname === "/about/" ||
url.pathname.startsWith("/game/")
) {
cachedResponse = await caches.match("/");
}
}

if (!cachedResponse) {
cachedResponse = await caches.match(event.request, {
ignoreSearch: true
});
}

return cachedResponse || fetch(event.request);
})()
);
Expand Down

0 comments on commit 76f9e19

Please sign in to comment.