|
| 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 | +// This file contains a bunch of different index.html "styles" that can be written |
| 6 | +// by Flutter Web users. |
| 7 | +// This should be somewhat kept in sync with the different index.html files present |
| 8 | +// in `flutter/dev/integration_tests/web/web`. |
| 9 | +// @see https://github.com/flutter/flutter/tree/master/dev/integration_tests/web/web |
| 10 | + |
| 11 | +/// index_with_flutterjs_entrypoint_loaded.html |
| 12 | +String indexHtmlFlutterJsCallback = _generateFlutterJsIndexHtml(''' |
| 13 | + window.addEventListener('load', function(ev) { |
| 14 | + // Download main.dart.js |
| 15 | + _flutter.loader.loadEntrypoint({ |
| 16 | + onEntrypointLoaded: onEntrypointLoaded, |
| 17 | + serviceWorker: { |
| 18 | + serviceWorkerVersion: serviceWorkerVersion, |
| 19 | + } |
| 20 | + }); |
| 21 | + // Once the entrypoint is ready, do things! |
| 22 | + async function onEntrypointLoaded(engineInitializer) { |
| 23 | + const appRunner = await engineInitializer.initializeEngine(); |
| 24 | + appRunner.runApp(); |
| 25 | + } |
| 26 | + }); |
| 27 | +'''); |
| 28 | + |
| 29 | +/// index_with_flutterjs_short.html |
| 30 | +String indexHtmlFlutterJsPromisesShort = _generateFlutterJsIndexHtml(''' |
| 31 | + window.addEventListener('load', function(ev) { |
| 32 | + // Download main.dart.js |
| 33 | + _flutter.loader.loadEntrypoint({ |
| 34 | + serviceWorker: { |
| 35 | + serviceWorkerVersion: serviceWorkerVersion, |
| 36 | + } |
| 37 | + }).then(function(engineInitializer) { |
| 38 | + return engineInitializer.autoStart(); |
| 39 | + }); |
| 40 | + }); |
| 41 | +'''); |
| 42 | + |
| 43 | +/// index_with_flutterjs.html |
| 44 | +String indexHtmlFlutterJsPromisesFull = _generateFlutterJsIndexHtml(''' |
| 45 | + window.addEventListener('load', function(ev) { |
| 46 | + // Download main.dart.js |
| 47 | + _flutter.loader.loadEntrypoint({ |
| 48 | + serviceWorker: { |
| 49 | + serviceWorkerVersion: serviceWorkerVersion, |
| 50 | + } |
| 51 | + }).then(function(engineInitializer) { |
| 52 | + return engineInitializer.initializeEngine(); |
| 53 | + }).then(function(appRunner) { |
| 54 | + return appRunner.runApp(); |
| 55 | + }); |
| 56 | + }); |
| 57 | +'''); |
| 58 | + |
| 59 | +/// index_without_flutterjs.html |
| 60 | +String indexHtmlNoFlutterJs = ''' |
| 61 | +<!DOCTYPE HTML> |
| 62 | +<!-- Copyright 2014 The Flutter Authors. All rights reserved. |
| 63 | +Use of this source code is governed by a BSD-style license that can be |
| 64 | +found in the LICENSE file. --> |
| 65 | +<html> |
| 66 | +<head> |
| 67 | + <meta charset="UTF-8"> |
| 68 | + <meta content="IE=Edge" http-equiv="X-UA-Compatible"> |
| 69 | +
|
| 70 | + <title>Web Test</title> |
| 71 | + <!-- iOS meta tags & icons --> |
| 72 | + <meta name="apple-mobile-web-app-capable" content="yes"> |
| 73 | + <meta name="apple-mobile-web-app-status-bar-style" content="black"> |
| 74 | + <meta name="apple-mobile-web-app-title" content="Web Test"> |
| 75 | + <link rel="manifest" href="manifest.json"> |
| 76 | +</head> |
| 77 | +<body> |
| 78 | + <!-- This script installs service_worker.js to provide PWA functionality to |
| 79 | + application. For more information, see: |
| 80 | + https://developers.google.com/web/fundamentals/primers/service-workers --> |
| 81 | + <script> |
| 82 | + var serviceWorkerVersion = null; |
| 83 | + var scriptLoaded = false; |
| 84 | + function loadMainDartJs() { |
| 85 | + if (scriptLoaded) { |
| 86 | + return; |
| 87 | + } |
| 88 | + scriptLoaded = true; |
| 89 | + var scriptTag = document.createElement('script'); |
| 90 | + scriptTag.src = 'main.dart.js'; |
| 91 | + scriptTag.type = 'application/javascript'; |
| 92 | + document.body.append(scriptTag); |
| 93 | + } |
| 94 | +
|
| 95 | + if ('serviceWorker' in navigator) { |
| 96 | + // Service workers are supported. Use them. |
| 97 | + window.addEventListener('load', function () { |
| 98 | + // Wait for registration to finish before dropping the <script> tag. |
| 99 | + // Otherwise, the browser will load the script multiple times, |
| 100 | + // potentially different versions. |
| 101 | + var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion; |
| 102 | + navigator.serviceWorker.register(serviceWorkerUrl) |
| 103 | + .then((reg) => { |
| 104 | + function waitForActivation(serviceWorker) { |
| 105 | + serviceWorker.addEventListener('statechange', () => { |
| 106 | + if (serviceWorker.state == 'activated') { |
| 107 | + console.log('Installed new service worker.'); |
| 108 | + loadMainDartJs(); |
| 109 | + } |
| 110 | + }); |
| 111 | + } |
| 112 | + if (!reg.active && (reg.installing || reg.waiting)) { |
| 113 | + // No active web worker and we have installed or are installing |
| 114 | + // one for the first time. Simply wait for it to activate. |
| 115 | + waitForActivation(reg.installing ?? reg.waiting); |
| 116 | + } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) { |
| 117 | + // When the app updates the serviceWorkerVersion changes, so we |
| 118 | + // need to ask the service worker to update. |
| 119 | + console.log('New service worker available.'); |
| 120 | + reg.update(); |
| 121 | + waitForActivation(reg.installing); |
| 122 | + } else { |
| 123 | + // Existing service worker is still good. |
| 124 | + console.log('Loading app from service worker.'); |
| 125 | + loadMainDartJs(); |
| 126 | + } |
| 127 | + }); |
| 128 | +
|
| 129 | + // If service worker doesn't succeed in a reasonable amount of time, |
| 130 | + // fallback to plaint <script> tag. |
| 131 | + setTimeout(() => { |
| 132 | + if (!scriptLoaded) { |
| 133 | + console.warn( |
| 134 | + 'Failed to load app from service worker. Falling back to plain <script> tag.', |
| 135 | + ); |
| 136 | + loadMainDartJs(); |
| 137 | + } |
| 138 | + }, 4000); |
| 139 | + }); |
| 140 | + } else { |
| 141 | + // Service workers not supported. Just drop the <script> tag. |
| 142 | + loadMainDartJs(); |
| 143 | + } |
| 144 | + </script> |
| 145 | +</body> |
| 146 | +</html> |
| 147 | +'''; |
| 148 | + |
| 149 | +// Generates the scaffolding of an index.html file, with a configurable `initScript`. |
| 150 | +String _generateFlutterJsIndexHtml(String initScript) => ''' |
| 151 | +<!DOCTYPE HTML> |
| 152 | +<!-- Copyright 2014 The Flutter Authors. All rights reserved. |
| 153 | +Use of this source code is governed by a BSD-style license that can be |
| 154 | +found in the LICENSE file. --> |
| 155 | +<html> |
| 156 | +<head> |
| 157 | + <meta charset="UTF-8"> |
| 158 | + <meta content="IE=Edge" http-equiv="X-UA-Compatible"> |
| 159 | +
|
| 160 | + <title>Integration test. App load with flutter.js and onEntrypointLoaded API</title> |
| 161 | + <!-- iOS meta tags & icons --> |
| 162 | + <meta name="apple-mobile-web-app-capable" content="yes"> |
| 163 | + <meta name="apple-mobile-web-app-status-bar-style" content="black"> |
| 164 | + <meta name="apple-mobile-web-app-title" content="Web Test"> |
| 165 | + <link rel="manifest" href="manifest.json"> |
| 166 | + <script> |
| 167 | + // The value below is injected by flutter build, do not touch. |
| 168 | + var serviceWorkerVersion = null; |
| 169 | + </script> |
| 170 | + <!-- This script adds the flutter initialization JS code --> |
| 171 | + <script src="flutter.js" defer></script> |
| 172 | +</head> |
| 173 | +<body> |
| 174 | + <script> |
| 175 | +$initScript |
| 176 | + </script> |
| 177 | +</body> |
| 178 | +</html> |
| 179 | +'''; |
0 commit comments