-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Initial service worker * Don't cache bootstrap * bootstrap no longer has a hash. * Handing service worker updates and refactoring loading * Missed some bits * Adding constant
- Loading branch information
1 parent
0bf18bd
commit 727f7aa
Showing
15 changed files
with
373 additions
and
92 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Copyright 2019 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
export default function constsPlugin(consts) { | ||
return { | ||
name: "consts-plugin", | ||
async resolveId(id) { | ||
if (id !== "consts:") { | ||
return; | ||
} | ||
return id; | ||
}, | ||
load(id) { | ||
if (id !== "consts:") { | ||
return; | ||
} | ||
return Object.entries(consts) | ||
.map( | ||
([key, value]) => `export const ${key} = ${JSON.stringify(value)};` | ||
) | ||
.join(""); | ||
} | ||
}; | ||
} |
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,41 @@ | ||
/** | ||
* Copyright 2018 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
const resourceListMarker = "___REPLACE_THIS_WITH_RESOURCE_LIST_LATER"; | ||
|
||
export default function resourceList() { | ||
return { | ||
name: "dependencygraph", | ||
resolveId(id) { | ||
if (id !== "resource-list:") { | ||
return null; | ||
} | ||
return id; | ||
}, | ||
load(id) { | ||
if (id !== "resource-list:") { | ||
return null; | ||
} | ||
return `export default ${resourceListMarker};`; | ||
}, | ||
generateBundle(_outputOptions, bundle) { | ||
const resourceListJSON = JSON.stringify(Object.keys(bundle)); | ||
|
||
for (const item of Object.values(bundle)) { | ||
if (!item.code) { | ||
continue; | ||
} | ||
item.code = item.code.replace(resourceListMarker, resourceListJSON); | ||
} | ||
} | ||
}; | ||
} |
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,18 +1,5 @@ | ||
const { parse } = require("path"); | ||
const rescapeRE = require("escape-string-regexp"); | ||
|
||
module.exports = { | ||
repo: "GoogleChromeLabs/graviton", | ||
path: "dist/**/*", | ||
branch: "master", | ||
findRenamed(path, newPaths) { | ||
const parsedPath = parse(path); | ||
if (parsedPath.base.startsWith("bootstrap-")) { | ||
const end = /\..*$/.exec(parsedPath.base)[0]; | ||
const re = new RegExp( | ||
`^${rescapeRE(parsedPath.dir)}/bootstrap-[^\.]+${end}$` | ||
); | ||
return newPaths.find(path => re.test(path)); | ||
} | ||
} | ||
branch: "master" | ||
}; |
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,81 @@ | ||
/** Tell the service worker to skip waiting. Resolves once the controller has changed. */ | ||
export async function skipWaiting() { | ||
const reg = await navigator.serviceWorker.getRegistration(); | ||
if (!reg || !reg.waiting) { | ||
return; | ||
} | ||
reg.waiting.postMessage("skip-waiting"); | ||
|
||
return new Promise(resolve => { | ||
navigator.serviceWorker.addEventListener( | ||
"controllerchange", | ||
() => resolve(), | ||
{ once: true } | ||
); | ||
}); | ||
} | ||
|
||
/** Is there currently a waiting worker? */ | ||
export let updateReady = false; | ||
|
||
/** Wait for an installing worker */ | ||
async function installingWorker( | ||
reg: ServiceWorkerRegistration | ||
): Promise<ServiceWorker> { | ||
if (reg.installing) { | ||
return reg.installing; | ||
} | ||
return new Promise<ServiceWorker>(resolve => { | ||
reg.addEventListener("updatefound", () => resolve(reg.installing!), { | ||
once: true | ||
}); | ||
}); | ||
} | ||
|
||
async function watchForUpdate() { | ||
const hasController = !!navigator.serviceWorker.controller; | ||
|
||
// If we don't have a controller, we don't need to check for updates – we've just loaded from the | ||
// network. | ||
if (!hasController) { | ||
return; | ||
} | ||
|
||
const reg = await navigator.serviceWorker.getRegistration(); | ||
// Service worker not registered yet. | ||
if (!reg) { | ||
return; | ||
} | ||
|
||
// Look for updates | ||
if (reg.waiting) { | ||
return; | ||
} | ||
const installing = await installingWorker(reg); | ||
await new Promise<void>(resolve => { | ||
installing.addEventListener("statechange", () => { | ||
if (installing.state === "installed") { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
|
||
updateReady = true; | ||
} | ||
|
||
/** Set up the service worker and monitor changes */ | ||
export async function init() { | ||
const thisURL = new URL(location.href); | ||
|
||
if (thisURL.searchParams.has("no-sw")) { | ||
const reg = await navigator.serviceWorker.getRegistration(); | ||
if (reg) { | ||
await reg.unregister(); | ||
location.reload(); | ||
} | ||
return; | ||
} | ||
|
||
await navigator.serviceWorker.register("/sw.js"); | ||
watchForUpdate(); | ||
} |
26 changes: 26 additions & 0 deletions
26
src/services/preact-canvas/components/game-loading/index.tsx
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,26 @@ | ||
/** | ||
* Copyright 2019 Google Inc. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
import { Component, h } from "preact"; | ||
import TopBar from "../top-bar/index.js"; | ||
import { loading, loadingInner } from "./style.css"; | ||
|
||
export default class GameLoading extends Component<{}, {}> { | ||
render() { | ||
return ( | ||
<div class={loading}> | ||
<TopBar titleOnly /> | ||
<div class={loadingInner}>…Loading…</div> | ||
</div> | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/services/preact-canvas/components/game-loading/style.css
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,25 @@ | ||
.loading { | ||
display: flex; | ||
flex: 1; | ||
align-items: center; | ||
justify-content: center; | ||
position: relative; | ||
} | ||
|
||
@keyframes fade-in-out { | ||
50% { | ||
opacity: 1; | ||
} | ||
} | ||
|
||
.loadingInner { | ||
display: flex; | ||
flex-flow: column; | ||
padding: var(--bar-avoid) var(--side-margin) 0; | ||
box-sizing: border-box; | ||
width: 100%; | ||
text-align: center; | ||
font-size: 2.3rem; | ||
opacity: 0; | ||
animation: fade-in-out 3s ease-in-out infinite 0.5s; | ||
} |
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
Oops, something went wrong.