summaryrefslogtreecommitdiffstats
path: root/platform/web
diff options
context:
space:
mode:
authorRémi Verschelde <rverschelde@gmail.com>2024-02-12 23:29:16 +0100
committerRémi Verschelde <rverschelde@gmail.com>2024-02-12 23:29:16 +0100
commit1b55fa15b08d10b81048f901976fdc1d6fe96fe7 (patch)
tree9e98576a157435a3c62c94cc5a059fa6690d8e6c /platform/web
parentf317cc713aa4dbcee2efa10db764473a56680be7 (diff)
parent62cec03a06f695f80746c9507afbe726945388d1 (diff)
downloadredot-engine-1b55fa15b08d10b81048f901976fdc1d6fe96fe7.tar.gz
Merge pull request #86089 from adamscott/pwa-coop-coep
Add PWA option to ensure cross-origin isolation headers on web export
Diffstat (limited to 'platform/web')
-rw-r--r--platform/web/.eslintrc.sw.js14
-rw-r--r--platform/web/doc_classes/EditorExportPlatformWeb.xml4
-rw-r--r--platform/web/export/export_plugin.cpp4
-rw-r--r--platform/web/js/engine/engine.js16
-rw-r--r--platform/web/package.json8
5 files changed, 40 insertions, 6 deletions
diff --git a/platform/web/.eslintrc.sw.js b/platform/web/.eslintrc.sw.js
new file mode 100644
index 0000000000..cba9ed8001
--- /dev/null
+++ b/platform/web/.eslintrc.sw.js
@@ -0,0 +1,14 @@
+module.exports = {
+ "extends": [
+ "./.eslintrc.js",
+ ],
+ "rules": {
+ "no-restricted-globals": 0,
+ },
+ "globals": {
+ "onClientMessage": true,
+ "___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___": true,
+ "___GODOT_CACHE___": true,
+ "___GODOT_OPT_CACHE___": true,
+ },
+};
diff --git a/platform/web/doc_classes/EditorExportPlatformWeb.xml b/platform/web/doc_classes/EditorExportPlatformWeb.xml
index 929d271676..75125e2708 100644
--- a/platform/web/doc_classes/EditorExportPlatformWeb.xml
+++ b/platform/web/doc_classes/EditorExportPlatformWeb.xml
@@ -55,6 +55,10 @@
<member name="progressive_web_app/enabled" type="bool" setter="" getter="">
If [code]true[/code], turns this web build into a [url=https://en.wikipedia.org/wiki/Progressive_web_app]progressive web application[/url] (PWA).
</member>
+ <member name="progressive_web_app/ensure_cross_origin_isolation_headers" type="bool" setter="" getter="">
+ When enabled, the progressive web app will make sure that each request has cross-origin isolation headers (COEP/COOP).
+ This can simplify the setup to serve the exported game.
+ </member>
<member name="progressive_web_app/icon_144x144" type="String" setter="" getter="">
File path to the smallest icon for this web application. If not defined, defaults to the project icon.
[b]Note:[/b] If the icon is not 144x144, it will be automatically resized for the final build.
diff --git a/platform/web/export/export_plugin.cpp b/platform/web/export/export_plugin.cpp
index 706bfca8ba..41c969b5f4 100644
--- a/platform/web/export/export_plugin.cpp
+++ b/platform/web/export/export_plugin.cpp
@@ -150,6 +150,7 @@ void EditorExportPlatformWeb::_fix_html(Vector<uint8_t> &p_html, const Ref<Edito
config["executable"] = p_name;
config["args"] = args;
config["fileSizes"] = p_file_sizes;
+ config["ensureCrossOriginIsolationHeaders"] = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
String head_include;
if (p_preset->get("html/export_icon")) {
@@ -222,10 +223,12 @@ Error EditorExportPlatformWeb::_build_pwa(const Ref<EditorExportPreset> &p_prese
const String dir = p_path.get_base_dir();
const String name = p_path.get_file().get_basename();
bool extensions = (bool)p_preset->get("variant/extensions_support");
+ bool ensure_crossorigin_isolation_headers = (bool)p_preset->get("progressive_web_app/ensure_cross_origin_isolation_headers");
HashMap<String, String> replaces;
replaces["___GODOT_VERSION___"] = String::num_int64(OS::get_singleton()->get_unix_time()) + "|" + String::num_int64(OS::get_singleton()->get_ticks_usec());
replaces["___GODOT_NAME___"] = proj_name.substr(0, 16);
replaces["___GODOT_OFFLINE_PAGE___"] = name + ".offline.html";
+ replaces["___GODOT_ENSURE_CROSSORIGIN_ISOLATION_HEADERS___"] = ensure_crossorigin_isolation_headers ? "true" : "false";
// Files cached during worker install.
Array cache_files;
@@ -353,6 +356,7 @@ void EditorExportPlatformWeb::get_export_options(List<ExportOption> *r_options)
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/focus_canvas_on_start"), true));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "html/experimental_virtual_keyboard"), false));
r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "progressive_web_app/enabled"), false));
+ r_options->push_back(ExportOption(PropertyInfo(Variant::BOOL, "progressive_web_app/ensure_cross_origin_isolation_headers"), true));
r_options->push_back(ExportOption(PropertyInfo(Variant::STRING, "progressive_web_app/offline_page", PROPERTY_HINT_FILE, "*.html"), ""));
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "progressive_web_app/display", PROPERTY_HINT_ENUM, "Fullscreen,Standalone,Minimal UI,Browser"), 1));
r_options->push_back(ExportOption(PropertyInfo(Variant::INT, "progressive_web_app/orientation", PROPERTY_HINT_ENUM, "Any,Landscape,Portrait"), 0));
diff --git a/platform/web/js/engine/engine.js b/platform/web/js/engine/engine.js
index 3d6720a2fc..7e24ad9ae2 100644
--- a/platform/web/js/engine/engine.js
+++ b/platform/web/js/engine/engine.js
@@ -179,9 +179,7 @@ const Engine = (function () {
preloader.preloadedFiles.length = 0; // Clear memory
me.rtenv['callMain'](me.config.args);
initPromise = null;
- if (me.config.serviceWorker && 'serviceWorker' in navigator) {
- navigator.serviceWorker.register(me.config.serviceWorker);
- }
+ me.installServiceWorker();
resolve();
});
});
@@ -242,6 +240,17 @@ const Engine = (function () {
this.rtenv['request_quit']();
}
},
+
+ /**
+ * Install the progressive-web app service worker.
+ * @returns {Promise} The service worker registration promise.
+ */
+ installServiceWorker: function () {
+ if (this.config.serviceWorker && 'serviceWorker' in navigator) {
+ return navigator.serviceWorker.register(this.config.serviceWorker);
+ }
+ return Promise.resolve();
+ },
};
Engine.prototype = proto;
@@ -252,6 +261,7 @@ const Engine = (function () {
Engine.prototype['startGame'] = Engine.prototype.startGame;
Engine.prototype['copyToFS'] = Engine.prototype.copyToFS;
Engine.prototype['requestQuit'] = Engine.prototype.requestQuit;
+ Engine.prototype['installServiceWorker'] = Engine.prototype.installServiceWorker;
// Also expose static methods as instance methods
Engine.prototype['load'] = Engine.load;
Engine.prototype['unload'] = Engine.unload;
diff --git a/platform/web/package.json b/platform/web/package.json
index 5da0e2a76b..4e17cd530b 100644
--- a/platform/web/package.json
+++ b/platform/web/package.json
@@ -5,18 +5,20 @@
"description": "Development and linting setup for Godot's Web platform code",
"scripts": {
"docs": "jsdoc --template js/jsdoc2rst/ js/engine/engine.js js/engine/config.js js/engine/features.js --destination ''",
- "lint": "npm run lint:engine && npm run lint:libs && npm run lint:modules && npm run lint:tools && npm run lint:html",
+ "lint": "npm run lint:engine && npm run lint:libs && npm run lint:modules && npm run lint:tools && npm run lint:sw && npm run lint:html",
"lint:engine": "eslint \"js/engine/*.js\" --no-eslintrc -c .eslintrc.engine.js",
+ "lint:sw": "eslint \"../../misc/dist/html/service-worker.js\" --no-eslintrc -c .eslintrc.sw.js",
"lint:libs": "eslint \"js/libs/*.js\" --no-eslintrc -c .eslintrc.libs.js",
"lint:modules": "eslint \"../../modules/**/*.js\" --no-eslintrc -c .eslintrc.libs.js",
"lint:tools": "eslint \"js/jsdoc2rst/**/*.js\" --no-eslintrc -c .eslintrc.engine.js",
"lint:html": "eslint \"../../misc/dist/html/*.html\" --no-eslintrc -c .eslintrc.html.js",
- "format": "npm run format:engine && npm run format:libs && npm run format:modules && npm run format:tools && npm run format:html",
+ "format": "npm run format:engine && npm run format:libs && npm run format:modules && npm run format:tools && format:sw && npm run format:html",
"format:engine": "npm run lint:engine -- --fix",
"format:libs": "npm run lint:libs -- --fix",
"format:modules": "npm run lint:modules -- --fix",
"format:tools": "npm run lint:tools -- --fix",
- "format:html": "npm run lint:html -- --fix"
+ "format:html": "npm run lint:html -- --fix",
+ "format:sw": "npm run lint:sw -- --fix"
},
"author": "Godot Engine contributors",
"license": "MIT",