forked from godotengine/godot
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Progressive Web App support to the HTML5 editor
- Loading branch information
Showing
5 changed files
with
175 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "Godot Engine", | ||
"short_name": "Godot", | ||
"description": "Multi-platform 2D and 3D game engine with a feature-rich editor", | ||
"lang": "en", | ||
"start_url": "/", | ||
"display": "standalone", | ||
"orientation": "landscape", | ||
"theme_color": "#478cbf", | ||
"icons": [ | ||
{ | ||
"src": "favicon.png", | ||
"sizes": "256x256", | ||
"type": "image/png" | ||
} | ||
], | ||
"background_color": "#333b4f" | ||
} |
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,42 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<meta http-equiv="X-UA-Compatible" content="IE=edge" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
<title>You are offline</title> | ||
<style> | ||
html { | ||
background-color: #333b4f; | ||
color: #e0e0e0; | ||
} | ||
|
||
body { | ||
font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji"; | ||
margin: 2rem; | ||
} | ||
|
||
p { | ||
margin-block: 1rem; | ||
} | ||
|
||
button { | ||
display: block; | ||
padding: 1rem 2rem; | ||
margin: 3rem auto 0; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<h1>You are offline</h1> | ||
<p>This application requires an Internet connection to run.</p> | ||
<p>Press the button below to try reloading:</p> | ||
<button type="button">Reload</button> | ||
|
||
<script> | ||
document.querySelector("button").addEventListener("click", () => { | ||
window.location.reload(); | ||
}); | ||
</script> | ||
</body> | ||
</html> |
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,88 @@ | ||
/* | ||
Copyright 2015, 2019, 2020 Google LLC. 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. | ||
*/ | ||
|
||
// This service worker is required to expose an exported Godot project as a | ||
// Progressive Web App. It provides an offline fallback page telling the user | ||
// that they need an Internet conneciton to run the project (since the | ||
// WebAssembly payload and PCK file are too large to cache). | ||
|
||
// Incrementing OFFLINE_VERSION will kick off the install event and force | ||
// previously cached resources to be updated from the network. | ||
const OFFLINE_VERSION = 1; | ||
const CACHE_NAME = "offline"; | ||
const OFFLINE_URL = "offline.html"; | ||
|
||
self.addEventListener("install", (event) => { | ||
event.waitUntil( | ||
(async () => { | ||
const cache = await caches.open(CACHE_NAME); | ||
// Setting {cache: 'reload'} in the new request will ensure that the | ||
// response isn't fulfilled from the HTTP cache; i.e., it will be from | ||
// the network. | ||
await cache.add(new Request(OFFLINE_URL, { cache: "reload" })); | ||
})() | ||
); | ||
// Force the waiting service worker to become the active service worker. | ||
self.skipWaiting(); | ||
}); | ||
|
||
self.addEventListener("activate", (event) => { | ||
event.waitUntil( | ||
(async () => { | ||
if ("navigationPreload" in self.registration) { | ||
await self.registration.navigationPreload.enable(); | ||
} | ||
})() | ||
); | ||
|
||
// Tell the active service worker to take control of the page immediately. | ||
self.clients.claim(); | ||
}); | ||
|
||
self.addEventListener("fetch", (event) => { | ||
// We only want to call event.respondWith() if this is a navigation request | ||
// for an HTML page. | ||
if (event.request.mode === "navigate") { | ||
event.respondWith( | ||
(async () => { | ||
try { | ||
// First, try to use the navigation preload response if it's supported. | ||
const preloadResponse = await event.preloadResponse; | ||
if (preloadResponse) { | ||
return preloadResponse; | ||
} | ||
|
||
// Always try the network first. | ||
const networkResponse = await fetch(event.request); | ||
return networkResponse; | ||
} catch (error) { | ||
// catch is only triggered if an exception is thrown, which is likely | ||
// due to a network error. | ||
// If fetch() returns a valid HTTP response with a response code in | ||
// the 4xx or 5xx range, the catch() will NOT be called. | ||
console.log("Fetch failed; returning offline page instead.", error); | ||
|
||
const cache = await caches.open(CACHE_NAME); | ||
const cachedResponse = await cache.match(OFFLINE_URL); | ||
return cachedResponse; | ||
} | ||
})() | ||
); | ||
} | ||
|
||
// If our if() condition is false, then this fetch handler won't intercept the | ||
// request. If there are any other fetch handlers registered, they will get a | ||
// chance to call event.respondWith(). If no fetch handlers call | ||
// event.respondWith(), the request will be handled by the browser as if there | ||
// were no service worker involvement. | ||
}); |
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