-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Add "Customizing web app initialization" page #7069
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
e625206
move web FAQ into directory
johnpryan da2bd1f
Move index.md to faq.md
johnpryan b398580
Add web index page
johnpryan 5e6000d
Move web renderers page to platform-integration/web directory
johnpryan dca524b
Add web initialization page
johnpryan 0f94a36
Update sidenav
johnpryan cd491fe
update web/initialization page
johnpryan 548af09
Update src/development/platform-integration/web/initialization.md
sfshaza2 987c467
Update src/development/platform-integration/web/initialization.md
sfshaza2 d3a8f51
Update src/development/platform-integration/web/initialization.md
sfshaza2 c54c2a3
Update src/development/platform-integration/web/initialization.md
sfshaza2 788a20a
Update src/development/platform-integration/web/initialization.md
sfshaza2 11cf971
Update src/development/platform-integration/web/initialization.md
sfshaza2 838637d
Update src/_data/sidenav.yml
sfshaza2 c26fd29
Update src/development/platform-integration/web/initialization.md
sfshaza2 99e22b2
Update src/development/platform-integration/web/initialization.md
sfshaza2 9f863a0
Update src/development/platform-integration/web/initialization.md
sfshaza2 aa2337e
Update src/development/platform-integration/web/initialization.md
sfshaza2 452ba75
Update src/development/platform-integration/web/initialization.md
sfshaza2 ffa7912
Update src/development/platform-integration/web/initialization.md
sfshaza2 d297654
Update src/development/platform-integration/web/index.md
sfshaza2 f7147eb
Update src/development/platform-integration/web/initialization.md
sfshaza2 b1f9508
Update src/development/platform-integration/web/renderers.md
sfshaza2 7ca6610
Update src/development/platform-integration/web/renderers.md
sfshaza2 48bc754
Update src/development/platform-integration/web/renderers.md
sfshaza2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| --- | ||
| title: Web | ||
| layout: toc | ||
| --- |
179 changes: 179 additions & 0 deletions
179
src/development/platform-integration/web/initialization.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| --- | ||
| title: Customizing web app initialization | ||
| description: Customize how Flutter apps are initialized on the web | ||
| --- | ||
|
|
||
| You can customize how a Flutter app is initialized on the web | ||
| using the `_flutter.loader` JavaScript API. | ||
| This API can be used to display a loading indicator in CSS, | ||
| prevent the app from loading based on a condition, | ||
| or wait until the user presses a button before showing the app. | ||
|
|
||
| The initialization process is split into the following stages: | ||
|
|
||
| **Loading the entrypoint script** | ||
| : Fetches the `main.dart.js` script and initializes the service worker. | ||
| **Initializing the Flutter engine** | ||
| : Initializes Flutter's web engine by downloading required resources | ||
| such as assets, fonts, and CanvasKit. | ||
| **Running the app** | ||
| : Prepares the DOM for your Flutter app and runs it. | ||
|
|
||
| This page shows how to customize the behavior | ||
| at each stage of the initialization process. | ||
|
|
||
| ## Getting started | ||
|
|
||
| By default, the `index.html` file | ||
| generated by the `flutter create` command | ||
| contains a script tag | ||
| that calls `loadEntrypoint` from the `flutter.js` file: | ||
|
|
||
| ```html | ||
| <html> | ||
| <head> | ||
| <!-- ... --> | ||
| <script src="flutter.js" defer></script> | ||
| </head> | ||
| <body> | ||
| <script> | ||
| window.addEventListener('load', function (ev) { | ||
| // Download main.dart.js | ||
| _flutter.loader.loadEntrypoint({ | ||
| serviceWorker: { | ||
| serviceWorkerVersion: serviceWorkerVersion, | ||
| } | ||
| }).then(function (engineInitializer) { | ||
| // Initialize the Flutter engine | ||
| return engineInitializer.initializeEngine(); | ||
| }).then(function (appRunner) { | ||
| // Run the app | ||
| return appRunner.runApp(); | ||
| }); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
|
|
||
|
|
||
| {{site.alert.note}} | ||
| In Flutter 2.10 or earlier, | ||
| this script doesn't support customization. | ||
| To upgrade your `index.html` file to the latest version, | ||
| see [Upgrading an older project](#upgrading-an-older-project). | ||
| {{site.alert.end}} | ||
|
|
||
|
|
||
|
|
||
| The `loadEntrypoint` function returns a JavaScript [`Promise`][js-promise] | ||
| that resolves when the Service Worker is initialized | ||
| and the `main.dart.js` entrypoint has been downloaded by the browser. | ||
| It resolves with an **engine initializer** object | ||
| that initializes the Flutter Web engine. | ||
|
|
||
| The `initializeEngine()` function returns a promise | ||
| that resolves with an **app runner** object | ||
| that has a single method `runApp()` that runs the Flutter app. | ||
|
|
||
| [js-promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise | ||
|
|
||
| ## Customizing web app initialization | ||
|
|
||
| In this section, | ||
| learn how to customize each stage of your app’s initialization. | ||
|
|
||
| ### Loading the entrypoint | ||
|
|
||
| The `loadEntrypoint` method accepts these parameters: | ||
|
|
||
| `entrypointUrl` | ||
| : The URL of your Flutter app's entrypoint. Defaults to `main.dart.js`. | ||
|
|
||
| `serviceWorker` | ||
| : The configuration of the `flutter_service_worker.js` file. | ||
| If this isn’t set, the service worker won’t be used. | ||
|
|
||
| `serviceWorkerVersion` | ||
| : Pass *the `serviceWorkerVersion` var* set by | ||
| the build process in your <strong><code>index.html</code></strong> file. | ||
|
|
||
| `timeoutMillis` | ||
| : The timeout value for the service worker load. | ||
| Defaults to <strong><code>4000ms</code></strong>. | ||
|
|
||
|
|
||
| ### Initializing the engine | ||
|
|
||
| Instead of calling `initializeEngine()` on the engine initializer, | ||
| you can call `autoStart()` to immediately start the app | ||
| with the default configuration | ||
| instead of using the app runner to call `runApp()`: | ||
|
|
||
|
|
||
| ```js | ||
| _flutter.loader.loadEntrypoint({ | ||
| serviceWorker: { | ||
| serviceWorkerVersion: serviceWorkerVersion, | ||
| } | ||
| }).then(function(engineInitializer) { | ||
| return engineInitializer.autoStart(); | ||
| }); | ||
| ``` | ||
|
|
||
| ## Example: Display a progress indicator | ||
|
|
||
| To give the user of your application feedback | ||
| during the initialization process, | ||
| use the hooks provided for each stage to update the DOM: | ||
|
|
||
|
|
||
| ```html | ||
| <html> | ||
| <head> | ||
| <!-- ... --> | ||
| <script src="flutter.js" defer></script> | ||
| </head> | ||
| <body> | ||
| <div id="loading"></div> | ||
| <script> | ||
| window.addEventListener('load', function(ev) { | ||
| var loading = document.querySelector('#loading'); | ||
| loading.textContent = "Loading entrypoint..."; | ||
| _flutter.loader.loadEntrypoint({ | ||
| serviceWorker: { | ||
| serviceWorkerVersion: serviceWorkerVersion, | ||
| } | ||
| }).then(function(engineInitializer) { | ||
| loading.textContent = "Initializing engine..."; | ||
| return engineInitializer.initializeEngine(); | ||
| }).then(function(appRunner) { | ||
| loading.textContent = "Running app..."; | ||
| return appRunner.runApp(); | ||
| }); | ||
| }); | ||
| </script> | ||
| </body> | ||
| </html> | ||
| ``` | ||
|
|
||
|
|
||
| For a more practical example using CSS animations, | ||
| see the [initialization code][gallery-init] for the Flutter Gallery. | ||
|
|
||
| [gallery-init]: {{site.github}}/flutter/gallery/blob/master/web/index.html | ||
|
|
||
| ## Upgrading an older project | ||
|
|
||
| If your project was created in Flutter 2.10 or earlier, | ||
| you can create a new `index.html` file | ||
| with the latest initialization template by running `flutter create`. | ||
|
|
||
| From your project directory, run the following: | ||
|
|
||
| ``` | ||
| $ flutter create . | ||
| ``` | ||
|
|
||
| You can now customize the startup process as described above. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we have redirects added for the old page?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was going to ask John for that. :D