Skip to content
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

Use flutter.js bootstrapping #2960

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ build/
# Or the files created by dart2js.
*.dart.js
*.js.deps
*.js.map

# Python generated files
*.pyc
Expand Down
7 changes: 5 additions & 2 deletions pkgs/dart_services/lib/src/common.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import 'main.dart' as entrypoint;

Future<void> main() async {
registerPlugins(webPluginRegistrar);
await ui_web.bootstrapEngine();
entrypoint.main();
await ui_web.bootstrapEngine(
runApp: () {
return entrypoint.main();
},
);
}
''';

Expand Down
9 changes: 9 additions & 0 deletions pkgs/dartpad_ui/firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@
}
]
},
{
"source": "/canvaskit/chromium/canvaskit.wasm",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This firebase CORS config looks reasonable to me. I wonder - do we expect to need to allowlist additional resources in the future? For this filename to change?

I also don't know if we're holding flutter web uniquely, or if this resource + CORS pattern should be part of general 'hosting a flutter web app' guidance.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we will need to keep evaluating how we're configuring CORS and reevaluate how to simplify our configuration once we loosen things up enough to get them working.

We don't have good docs on how to configure CORS for Flutter web + Wasm, but I'm still not sure if this is a Flutter web specific configuration or if this is due to how we're sandboxing the iframe element.

cc @eyebrowsoffire @ditman @kevmoo

Copy link
Member

@ditman ditman May 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the incoming --wasm mode, there's a whole host of "CORS cousins" that will need similar configuration + documentation, like COEP or CORP. This file is cool (but of course it depends on the hosting/CDN that the developer picks)!

"headers": [
{
"key": "Access-Control-Allow-Origin",
"value": "*"
}
]
},
{
"source": "**",
"headers": [
Expand Down
4 changes: 2 additions & 2 deletions pkgs/dartpad_ui/web/frame.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
margin-top: 0;
}
</style>
<script src="frame.js"></script>
<script src="require.js"></script>
<script id="compiled-script"></script>
<script src="flutter.js"></script>
<script src="frame.js"></script>
</head>

<body>
Expand Down
20 changes: 13 additions & 7 deletions pkgs/dartpad_ui/web/frame.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,22 @@ function messageHandler(e) {
var obj = e.data;

if (obj.command === 'execute') {
// TODO: Switch to using engineInitializer.initializeEngine(config). See
// https://docs.flutter.dev/development/platform-integration/web/initialization.
window.flutterConfiguration = {
canvasKitBaseUrl: obj.canvasKitBaseUrl
};

replaceJavaScript(obj.js);
runFlutterApp(obj.js);
}
};

function runFlutterApp(compiledScript) {
var blob = new Blob([compiledScript], {type: 'text/javascript'});
var url = URL.createObjectURL(blob);
_flutter.loader.loadEntrypoint({
entrypointUrl: url,
onEntrypointLoaded: async function(engineInitializer) {
let appRunner = await engineInitializer.initializeEngine();
appRunner.runApp();
}
});
}

window.addEventListener('load', function () {
window.addEventListener('message', messageHandler, false);
parent.postMessage({ 'sender': 'frame', 'type': 'ready' }, '*');
Expand Down
Loading