Skip to content

Commit

Permalink
Add Progressive Web App support to the HTML5 editor
Browse files Browse the repository at this point in the history
  • Loading branch information
Calinou committed Dec 25, 2020
1 parent 545c894 commit 2aad127
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 3 deletions.
24 changes: 21 additions & 3 deletions misc/dist/html/editor.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@
<html xmlns='http://www.w3.org/1999/xhtml' lang='' xml:lang=''>
<head>
<meta charset='utf-8' />
<meta name='viewport' content='width=device-width, user-scalable=no' />
<meta name='viewport' content='width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no' />
<meta name="mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="application-name" content="Godot" />
<meta name="apple-mobile-web-app-title" content="Godot" />
<meta name="theme-color" content="#478cbf" />
<meta name="msapplication-navbutton-color" content="#478cbf" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="msapplication-starturl" content="/latest" />
<link id='-gd-engine-icon' rel='icon' type='image/png' href='favicon.png' />
<title></title>
<link rel="apple-touch-icon" type="image/png" href="favicon.png" />
<link rel="manifest" href="manifest.json" />
<title>Godot Engine Editor</title>

<style type='text/css'>

body {
Expand Down Expand Up @@ -169,7 +180,7 @@
<option value="GLES3">WebGL 2</option>
</select>
<br />
<img src="logo.svg">
<img src="logo.svg" width="1024" height="414" style="width: auto; height: auto; max-width: 85%; max-height: 250px" />
<br />
<label for="zip-file" style="margin-right: 1rem">Preload project ZIP:</label> <input id="zip-file" type="file" id="files" name="files" style="margin-bottom: 1rem"/>
<br />
Expand Down Expand Up @@ -207,6 +218,13 @@
</div>

<script type='text/javascript' src='godot.tools.js'></script>
<script>
window.addEventListener("load", () => {
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js");
}
});
</script>
<script type='text/javascript'>//<![CDATA[

var engine = new Engine;
Expand Down
18 changes: 18 additions & 0 deletions misc/dist/html/manifest.json
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"
}
42 changes: 42 additions & 0 deletions misc/dist/html/offline.html
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>
88 changes: 88 additions & 0 deletions misc/dist/html/service-worker.js
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.
});
6 changes: 6 additions & 0 deletions platform/javascript/SCsub
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ if env["tools"]:
out_files.append(zip_dir.File("logo.svg"))
in_files.append("#icon.png")
out_files.append(zip_dir.File("favicon.png"))
in_files.append("#misc/dist/html/manifest.json")
out_files.append(zip_dir.File("manifest.json"))
in_files.append("#misc/dist/html/service-worker.js")
out_files.append(zip_dir.File("service-worker.js"))
in_files.append("#misc/dist/html/offline.html")
out_files.append(zip_dir.File("offline.html"))

zip_files = env.InstallAs(out_files, in_files)
env.Zip(
Expand Down

0 comments on commit 2aad127

Please sign in to comment.