[web] Fix JS crash when FF blocks service workers. (#106072)

This commit is contained in:
David Iglesias
2022-06-16 19:22:07 -07:00
committed by GitHub
parent 2c15e3cae4
commit b1b1ee9ca6
5 changed files with 160 additions and 3 deletions

View File

@@ -76,6 +76,7 @@ _flutter.loader = null;
_loadEntrypoint(entrypointUrl) {
if (!this._scriptLoaded) {
console.debug("Injecting <script> tag.");
this._scriptLoaded = new Promise((resolve, reject) => {
let scriptTag = document.createElement("script");
scriptTag.src = entrypointUrl;
@@ -96,7 +97,7 @@ _flutter.loader = null;
_waitForServiceWorkerActivation(serviceWorker, entrypointUrl) {
if (!serviceWorker || serviceWorker.state == "activated") {
if (!serviceWorker) {
console.warn("Cannot activate a null service worker. Falling back to plain <script> tag.");
console.warn("Cannot activate a null service worker.");
} else {
console.debug("Service worker already active.");
}
@@ -114,7 +115,7 @@ _flutter.loader = null;
_loadWithServiceWorker(entrypointUrl, serviceWorkerOptions) {
if (!("serviceWorker" in navigator) || serviceWorkerOptions == null) {
console.warn("Service worker not supported (or configured). Falling back to plain <script> tag.", serviceWorkerOptions);
console.warn("Service worker not supported (or configured).", serviceWorkerOptions);
return this._loadEntrypoint(entrypointUrl);
}
@@ -145,6 +146,11 @@ _flutter.loader = null;
console.debug("Loading app from service worker.");
return this._loadEntrypoint(entrypointUrl);
}
})
.catch((error) => {
// Some exception happened while registering/activating the service worker.
console.warn("Failed to register or activate service worker:", error);
return this._loadEntrypoint(entrypointUrl);
});
// Timeout race promise
@@ -153,7 +159,7 @@ _flutter.loader = null;
timeout = new Promise((resolve, _) => {
setTimeout(() => {
if (!this._scriptLoaded) {
console.warn("Failed to load app from service worker. Falling back to plain <script> tag.");
console.warn("Loading from service worker timed out after", timeoutMillis, "milliseconds.");
resolve(this._loadEntrypoint(entrypointUrl));
}
}, timeoutMillis);