|
33 | 33 | <link rel="manifest" href="manifest.json">
|
34 | 34 | </head>
|
35 | 35 | <body>
|
36 |
| - <!-- This script installs service_worker.js to provide PWA functionality to |
37 |
| - application. For more information, see: |
38 |
| - https://developers.google.com/web/fundamentals/primers/service-workers --> |
39 |
| - <script> |
40 |
| - var serviceWorkerVersion = null; |
41 |
| - var scriptLoaded = false; |
42 |
| - function loadMainDartJs() { |
43 |
| - if (scriptLoaded) { |
44 |
| - return; |
| 36 | +<script> |
| 37 | + var serviceWorkerVersion = null; |
| 38 | + var scriptLoaded = false; |
| 39 | + function loadMainDartJs() { |
| 40 | + if (scriptLoaded) { |
| 41 | + return; |
| 42 | + } |
| 43 | + scriptLoaded = true; |
| 44 | + var scriptTag = document.createElement('script'); |
| 45 | + scriptTag.src = 'main.dart.js'; |
| 46 | + scriptTag.type = 'application/javascript'; |
| 47 | + document.body.append(scriptTag); |
45 | 48 | }
|
46 |
| - scriptLoaded = true; |
47 |
| - var scriptTag = document.createElement('script'); |
48 |
| - scriptTag.src = 'main.dart.js'; |
49 |
| - scriptTag.type = 'application/javascript'; |
50 |
| - document.body.append(scriptTag); |
51 |
| - } |
52 | 49 |
|
53 |
| - if ('serviceWorker' in navigator) { |
54 |
| - // Service workers are supported. Use them. |
55 |
| - window.addEventListener('load', function () { |
56 |
| - // Wait for registration to finish before dropping the <script> tag. |
57 |
| - // Otherwise, the browser will load the script multiple times, |
58 |
| - // potentially different versions. |
59 |
| - var serviceWorkerUrl = 'flutter_service_worker.js?v=' + serviceWorkerVersion; |
60 |
| - navigator.serviceWorker.register(serviceWorkerUrl) |
61 |
| - .then((reg) => { |
| 50 | + if ('serviceWorker' in navigator) { |
| 51 | + // Service workers are supported. Use them. |
| 52 | + window.addEventListener('load', function () { |
| 53 | + // Register Firebase Messaging service worker. |
| 54 | + navigator.serviceWorker.register('firebase-messaging-sw.js'); |
| 55 | + |
| 56 | + // Wait for registration to finish before dropping the <script> tag. |
| 57 | + // Otherwise, the browser will load the script multiple times, |
| 58 | + // potentially different versions. |
| 59 | + var serviceWorkerUrl = |
| 60 | + 'flutter_service_worker.js?v=' + serviceWorkerVersion; |
| 61 | + |
| 62 | + navigator.serviceWorker.register(serviceWorkerUrl).then((reg) => { |
62 | 63 | function waitForActivation(serviceWorker) {
|
63 | 64 | serviceWorker.addEventListener('statechange', () => {
|
64 | 65 | if (serviceWorker.state == 'activated') {
|
|
70 | 71 | if (!reg.active && (reg.installing || reg.waiting)) {
|
71 | 72 | // No active web worker and we have installed or are installing
|
72 | 73 | // one for the first time. Simply wait for it to activate.
|
73 |
| - waitForActivation(reg.installing || reg.waiting); |
| 74 | + waitForActivation(reg.installing ?? reg.waiting); |
74 | 75 | } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) {
|
75 | 76 | // When the app updates the serviceWorkerVersion changes, so we
|
76 | 77 | // need to ask the service worker to update.
|
|
84 | 85 | }
|
85 | 86 | });
|
86 | 87 |
|
87 |
| - // If service worker doesn't succeed in a reasonable amount of time, |
88 |
| - // fallback to plaint <script> tag. |
89 |
| - setTimeout(() => { |
90 |
| - if (!scriptLoaded) { |
91 |
| - console.warn( |
92 |
| - 'Failed to load app from service worker. Falling back to plain <script> tag.', |
93 |
| - ); |
94 |
| - loadMainDartJs(); |
95 |
| - } |
96 |
| - }, 4000); |
97 |
| - }); |
98 |
| - } else { |
99 |
| - // Service workers not supported. Just drop the <script> tag. |
100 |
| - loadMainDartJs(); |
101 |
| - } |
| 88 | + // If service worker doesn't succeed in a reasonable amount of time, |
| 89 | + // fallback to plaint <script> tag. |
| 90 | + setTimeout(() => { |
| 91 | + if (!scriptLoaded) { |
| 92 | + console.warn( |
| 93 | + 'Failed to load app from service worker. Falling back to plain <script> tag.' |
| 94 | + ); |
| 95 | + loadMainDartJs(); |
| 96 | + } |
| 97 | + }, 4000); |
| 98 | + }); |
| 99 | + } else { |
| 100 | + // Service workers not supported. Just drop the <script> tag. |
| 101 | + loadMainDartJs(); |
| 102 | + } |
102 | 103 | </script>
|
103 | 104 | </body>
|
104 | 105 | </html>
|
0 commit comments