Showing
3 changed files
with
69 additions
and
15 deletions
| @@ -254,10 +254,9 @@ gh-social: all | @@ -254,10 +254,9 @@ gh-social: all | ||
| 254 | cd test; $(DART_BIN) --enable-asserts github_social_preview.dart | 254 | cd test; $(DART_BIN) --enable-asserts github_social_preview.dart |
| 255 | 255 | ||
| 256 | gh-pages: all | 256 | gh-pages: all |
| 257 | - cd demo; $(FLUTTER_BIN) build web | 257 | + cd demo; $(FLUTTER_BIN) build web --base-href "/dart_pdf/" |
| 258 | git checkout gh-pages | 258 | git checkout gh-pages |
| 259 | rm -rf assets icons | 259 | rm -rf assets icons |
| 260 | mv -fv demo/build/web/* . | 260 | mv -fv demo/build/web/* . |
| 261 | - sed -e 's|<base href="/">|<base href="/dart_pdf/">|' -i index.html | ||
| 262 | 261 | ||
| 263 | .PHONY: test format format-dart format-clang clean publish-pdf publish-printing analyze ref | 262 | .PHONY: test format format-dart format-clang clean publish-pdf publish-printing analyze ref |
| @@ -8,10 +8,13 @@ | @@ -8,10 +8,13 @@ | ||
| 8 | The path provided below has to start and end with a slash "/" in order for | 8 | The path provided below has to start and end with a slash "/" in order for |
| 9 | it to work correctly. | 9 | it to work correctly. |
| 10 | 10 | ||
| 11 | - Fore more details: | 11 | + For more details: |
| 12 | * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base | 12 | * https://developer.mozilla.org/en-US/docs/Web/HTML/Element/base |
| 13 | + | ||
| 14 | + This is a placeholder for base href that will be replaced by the value of | ||
| 15 | + the `--base-href` argument provided to `flutter build`. | ||
| 13 | --> | 16 | --> |
| 14 | - <base href="/"> | 17 | + <base href="$FLUTTER_BASE_HREF"> |
| 15 | 18 | ||
| 16 | <meta charset="UTF-8"> | 19 | <meta charset="UTF-8"> |
| 17 | <meta content="IE=Edge" http-equiv="X-UA-Compatible"> | 20 | <meta content="IE=Edge" http-equiv="X-UA-Compatible"> |
| @@ -20,7 +23,7 @@ | @@ -20,7 +23,7 @@ | ||
| 20 | <!-- iOS meta tags & icons --> | 23 | <!-- iOS meta tags & icons --> |
| 21 | <meta name="apple-mobile-web-app-capable" content="yes"> | 24 | <meta name="apple-mobile-web-app-capable" content="yes"> |
| 22 | <meta name="apple-mobile-web-app-status-bar-style" content="black"> | 25 | <meta name="apple-mobile-web-app-status-bar-style" content="black"> |
| 23 | - <meta name="apple-mobile-web-app-title" content="printing_demo"> | 26 | + <meta name="apple-mobile-web-app-title" content="Flutter PDF Demo"> |
| 24 | <link rel="apple-touch-icon" href="icon-192.png"> | 27 | <link rel="apple-touch-icon" href="icon-192.png"> |
| 25 | 28 | ||
| 26 | <!-- Favicon --> | 29 | <!-- Favicon --> |
| @@ -34,16 +37,68 @@ | @@ -34,16 +37,68 @@ | ||
| 34 | application. For more information, see: | 37 | application. For more information, see: |
| 35 | https://developers.google.com/web/fundamentals/primers/service-workers --> | 38 | https://developers.google.com/web/fundamentals/primers/service-workers --> |
| 36 | <script> | 39 | <script> |
| 40 | + var serviceWorkerVersion = null; | ||
| 41 | + var scriptLoaded = false; | ||
| 42 | + function loadMainDartJs() { | ||
| 43 | + if (scriptLoaded) { | ||
| 44 | + return; | ||
| 45 | + } | ||
| 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 | + | ||
| 37 | if ('serviceWorker' in navigator) { | 53 | if ('serviceWorker' in navigator) { |
| 38 | - window.addEventListener('flutter-first-frame', function () { | ||
| 39 | - navigator.serviceWorker.register('flutter_service_worker.js'); | 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) => { | ||
| 62 | + function waitForActivation(serviceWorker) { | ||
| 63 | + serviceWorker.addEventListener('statechange', () => { | ||
| 64 | + if (serviceWorker.state == 'activated') { | ||
| 65 | + console.log('Installed new service worker.'); | ||
| 66 | + loadMainDartJs(); | ||
| 67 | + } | ||
| 68 | + }); | ||
| 69 | + } | ||
| 70 | + if (!reg.active && (reg.installing || reg.waiting)) { | ||
| 71 | + // No active web worker and we have installed or are installing | ||
| 72 | + // one for the first time. Simply wait for it to activate. | ||
| 73 | + waitForActivation(reg.installing || reg.waiting); | ||
| 74 | + } else if (!reg.active.scriptURL.endsWith(serviceWorkerVersion)) { | ||
| 75 | + // When the app updates the serviceWorkerVersion changes, so we | ||
| 76 | + // need to ask the service worker to update. | ||
| 77 | + console.log('New service worker available.'); | ||
| 78 | + reg.update(); | ||
| 79 | + waitForActivation(reg.installing); | ||
| 80 | + } else { | ||
| 81 | + // Existing service worker is still good. | ||
| 82 | + console.log('Loading app from service worker.'); | ||
| 83 | + loadMainDartJs(); | ||
| 84 | + } | ||
| 85 | + }); | ||
| 86 | + | ||
| 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); | ||
| 40 | }); | 97 | }); |
| 98 | + } else { | ||
| 99 | + // Service workers not supported. Just drop the <script> tag. | ||
| 100 | + loadMainDartJs(); | ||
| 41 | } | 101 | } |
| 42 | </script> | 102 | </script> |
| 43 | - <script src="//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.min.js"></script> | ||
| 44 | - <script type="text/javascript"> | ||
| 45 | - pdfjsLib.GlobalWorkerOptions.workerSrc = "//cdnjs.cloudflare.com/ajax/libs/pdf.js/2.8.335/pdf.worker.min.js"; | ||
| 46 | - </script> | ||
| 47 | - <script src="main.dart.js" type="application/javascript"></script> | ||
| 48 | </body> | 103 | </body> |
| 49 | </html> | 104 | </html> |
| 1 | { | 1 | { |
| 2 | - "name": "printing_demo", | ||
| 3 | - "short_name": "printing_demo", | 2 | + "name": "Flutter PDF Demo", |
| 3 | + "short_name": "Flutter PDF Demo", | ||
| 4 | "start_url": ".", | 4 | "start_url": ".", |
| 5 | "display": "standalone", | 5 | "display": "standalone", |
| 6 | "background_color": "#0175C2", | 6 | "background_color": "#0175C2", |
| 7 | "theme_color": "#0175C2", | 7 | "theme_color": "#0175C2", |
| 8 | - "description": "A new Flutter project.", | 8 | + "description": "Flutter PDF Demo.", |
| 9 | "orientation": "portrait-primary", | 9 | "orientation": "portrait-primary", |
| 10 | "prefer_related_applications": false, | 10 | "prefer_related_applications": false, |
| 11 | "icons": [ | 11 | "icons": [ |
-
Please register or login to post a comment