|
8 | 8 | The path provided below has to start and end with a slash "/" in order for |
9 | 9 | it to work correctly. |
10 | 10 |
|
11 | | - Fore more details: |
| 11 | + For more details: |
12 | 12 | * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base |
13 | 13 | --> |
14 | 14 | <base href="/"> |
|
23 | 23 | <meta name="apple-mobile-web-app-title" content="flutter_evolution"> |
24 | 24 | <link rel="apple-touch-icon" href="icons/Icon-192.png"> |
25 | 25 |
|
26 | | - <!-- Favicon --> |
27 | | - <link rel="icon" type="image/png" href="favicon.png"/> |
28 | | - |
29 | 26 | <title>flutter_evolution</title> |
30 | 27 | <link rel="manifest" href="manifest.json"> |
31 | 28 | </head> |
|
34 | 31 | application. For more information, see: |
35 | 32 | https://developers.google.com/web/fundamentals/primers/service-workers --> |
36 | 33 | <script> |
| 34 | + var serviceWorkerVersion = null; |
| 35 | + var scriptLoaded = false; |
| 36 | + function loadMainDartJs() { |
| 37 | + if (scriptLoaded) { |
| 38 | + return; |
| 39 | + } |
| 40 | + scriptLoaded = true; |
| 41 | + var scriptTag = document.createElement('script'); |
| 42 | + scriptTag.src = 'main.dart.js'; |
| 43 | + scriptTag.type = 'application/javascript'; |
| 44 | + document.body.append(scriptTag); |
| 45 | + } |
| 46 | + |
37 | 47 | if ('serviceWorker' in navigator) { |
38 | | - window.addEventListener('flutter-first-frame', function () { |
39 | | - navigator.serviceWorker.register('flutter_service_worker.js'); |
| 48 | + // Service workers are supported. Use them. |
| 49 | + window.addEventListener('load', function () { |
| 50 | + // Wait for registration to finish before dropping the <script> tag. |
| 51 | + // Otherwise, the browser will load the script multiple times, |
| 52 | + // potentially different versions. |
| 53 | + var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion; |
| 54 | + navigator.serviceWorker.register(serviceWorkerUrl) |
| 55 | + .then((reg) => { |
| 56 | + function waitForActivation(serviceWorker) { |
| 57 | + serviceWorker.addEventListener('statechange', () => { |
| 58 | + if (serviceWorker.state == 'activated') { |
| 59 | + console.log('Installed new service worker.'); |
| 60 | + loadMainDartJs(); |
| 61 | + } |
| 62 | + }); |
| 63 | + } |
| 64 | + if (!reg.active && (reg.installing || reg.waiting)) { |
| 65 | + // No active web worker and we have installed or are installing |
| 66 | + // one for the first time. Simply wait for it to activate. |
| 67 | + waitForActivation(reg.installing ?? reg.waiting); |
| 68 | + } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) { |
| 69 | + // When the app updates the serviceWorkerVersion changes, so we |
| 70 | + // need to ask the service worker to update. |
| 71 | + console.log('New service worker available.'); |
| 72 | + reg.update(); |
| 73 | + waitForActivation(reg.installing); |
| 74 | + } else { |
| 75 | + // Existing service worker is still good. |
| 76 | + console.log('Loading app from service worker.'); |
| 77 | + loadMainDartJs(); |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + // If service worker doesn't succeed in a reasonable amount of time, |
| 82 | + // fallback to plaint <script> tag. |
| 83 | + setTimeout(() => { |
| 84 | + if (!scriptLoaded) { |
| 85 | + console.warn( |
| 86 | + 'Failed to load app from service worker. Falling back to plain <script> tag.', |
| 87 | + ); |
| 88 | + loadMainDartJs(); |
| 89 | + } |
| 90 | + }, 4000); |
40 | 91 | }); |
| 92 | + } else { |
| 93 | + // Service workers not supported. Just drop the <script> tag. |
| 94 | + loadMainDartJs(); |
41 | 95 | } |
42 | 96 | </script> |
43 | | - <script src="main.dart.js" type="application/javascript"></script> |
44 | 97 | </body> |
45 | 98 | </html> |
0 commit comments