|
| 1 | +// Copyright 2014 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +/** |
| 6 | + * This script installs service_worker.js to provide PWA functionality to |
| 7 | + * application. For more information, see: |
| 8 | + * https://developers.google.com/web/fundamentals/primers/service-workers |
| 9 | + */ |
| 10 | + |
| 11 | +if (!_flutter) { |
| 12 | + var _flutter = {}; |
| 13 | +} |
| 14 | +_flutter.loader = null; |
| 15 | + |
| 16 | +(function() { |
| 17 | + "use strict"; |
| 18 | + class FlutterLoader { |
| 19 | + // TODO: Move the below methods to "#private" once supported by all the browsers |
| 20 | + // we support. In the meantime, we use the "revealing module" pattern. |
| 21 | + |
| 22 | + // Watchdog to prevent injecting the main entrypoint multiple times. |
| 23 | + _scriptLoaded = null; |
| 24 | + |
| 25 | + // Resolver for the pending promise returned by loadEntrypoint. |
| 26 | + _didCreateEngineInitializerResolve = null; |
| 27 | + |
| 28 | + /** |
| 29 | + * Initializes the main.dart.js with/without serviceWorker. |
| 30 | + * @param {*} options |
| 31 | + * @returns a Promise that will eventually resolve with an EngineInitializer, |
| 32 | + * or will be rejected with the error caused by the loader. |
| 33 | + */ |
| 34 | + loadEntrypoint(options) { |
| 35 | + const { |
| 36 | + entrypointUrl = "main.dart.js", |
| 37 | + serviceWorker, |
| 38 | + } = (options || {}); |
| 39 | + return this._loadWithServiceWorker(entrypointUrl, serviceWorker); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Resolves the promise created by loadEntrypoint. Called by Flutter. |
| 44 | + * Needs to be weirdly bound like it is, so "this" is preserved across |
| 45 | + * the JS <-> Flutter jumps. |
| 46 | + * @param {*} engineInitializer |
| 47 | + */ |
| 48 | + didCreateEngineInitializer = (function(engineInitializer) { |
| 49 | + if (typeof this._didCreateEngineInitializerResolve != "function") { |
| 50 | + console.warn("Do not call didCreateEngineInitializer by hand. Start with loadEntrypoint instead."); |
| 51 | + } |
| 52 | + this._didCreateEngineInitializerResolve(engineInitializer); |
| 53 | + // Remove this method after it's done, so Flutter Web can hot restart. |
| 54 | + delete this.didCreateEngineInitializer; |
| 55 | + }).bind(this); |
| 56 | + |
| 57 | + _loadEntrypoint(entrypointUrl) { |
| 58 | + if (!this._scriptLoaded) { |
| 59 | + this._scriptLoaded = new Promise((resolve, reject) => { |
| 60 | + let scriptTag = document.createElement("script"); |
| 61 | + scriptTag.src = entrypointUrl; |
| 62 | + scriptTag.type = "application/javascript"; |
| 63 | + this._didCreateEngineInitializerResolve = resolve; // Cache the resolve, so it can be called from Flutter. |
| 64 | + scriptTag.addEventListener("error", reject); |
| 65 | + document.body.append(scriptTag); |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + return this._scriptLoaded; |
| 70 | + } |
| 71 | + |
| 72 | + _waitForServiceWorkerActivation(serviceWorker, entrypointUrl) { |
| 73 | + if (!serviceWorker || serviceWorker.state == "activated") { |
| 74 | + if (!serviceWorker) { |
| 75 | + console.warn("Cannot activate a null service worker. Falling back to plain <script> tag."); |
| 76 | + } else { |
| 77 | + console.debug("Service worker already active."); |
| 78 | + } |
| 79 | + return this._loadEntrypoint(entrypointUrl); |
| 80 | + } |
| 81 | + return new Promise((resolve, _) => { |
| 82 | + serviceWorker.addEventListener("statechange", () => { |
| 83 | + if (serviceWorker.state == "activated") { |
| 84 | + console.debug("Installed new service worker."); |
| 85 | + resolve(this._loadEntrypoint(entrypointUrl)); |
| 86 | + } |
| 87 | + }); |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + _loadWithServiceWorker(entrypointUrl, serviceWorkerOptions) { |
| 92 | + if (!("serviceWorker" in navigator) || serviceWorkerOptions == null) { |
| 93 | + console.warn("Service worker not supported (or configured). Falling back to plain <script> tag.", serviceWorkerOptions); |
| 94 | + return this._loadEntrypoint(entrypointUrl); |
| 95 | + } |
| 96 | + |
| 97 | + const { |
| 98 | + serviceWorkerVersion, |
| 99 | + timeoutMillis = 4000, |
| 100 | + } = serviceWorkerOptions; |
| 101 | + |
| 102 | + let serviceWorkerUrl = "flutter_service_worker.js?v=" + serviceWorkerVersion; |
| 103 | + let loader = navigator.serviceWorker.register(serviceWorkerUrl) |
| 104 | + .then((reg) => { |
| 105 | + if (!reg.active && (reg.installing || reg.waiting)) { |
| 106 | + // No active web worker and we have installed or are installing |
| 107 | + // one for the first time. Simply wait for it to activate. |
| 108 | + let sw = reg.installing || reg.waiting; |
| 109 | + return this._waitForServiceWorkerActivation(sw, entrypointUrl); |
| 110 | + } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) { |
| 111 | + // When the app updates the serviceWorkerVersion changes, so we |
| 112 | + // need to ask the service worker to update. |
| 113 | + console.debug("New service worker available."); |
| 114 | + return reg.update().then((reg) => { |
| 115 | + console.debug("Service worker updated."); |
| 116 | + let sw = reg.installing || reg.waiting || reg.active; |
| 117 | + return this._waitForServiceWorkerActivation(sw, entrypointUrl); |
| 118 | + }); |
| 119 | + } else { |
| 120 | + // Existing service worker is still good. |
| 121 | + console.debug("Loading app from service worker."); |
| 122 | + return this._loadEntrypoint(entrypointUrl); |
| 123 | + } |
| 124 | + }); |
| 125 | + |
| 126 | + // Timeout race promise |
| 127 | + let timeout; |
| 128 | + if (timeoutMillis > 0) { |
| 129 | + timeout = new Promise((resolve, _) => { |
| 130 | + setTimeout(() => { |
| 131 | + if (!this._scriptLoaded) { |
| 132 | + console.warn("Failed to load app from service worker. Falling back to plain <script> tag."); |
| 133 | + resolve(this._loadEntrypoint(entrypointUrl)); |
| 134 | + } |
| 135 | + }, timeoutMillis); |
| 136 | + }); |
| 137 | + } |
| 138 | + |
| 139 | + return Promise.race([loader, timeout]); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + _flutter.loader = new FlutterLoader(); |
| 144 | +}()); |
0 commit comments