Skip to content

Commit

Permalink
Fix the problem on the web
Browse files Browse the repository at this point in the history
  • Loading branch information
temeddix committed Feb 17, 2024
1 parent 04d7fa4 commit 1872afb
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 10 deletions.
4 changes: 2 additions & 2 deletions documentation/docs/running-and-building.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ flutter build web

When deploying your web app, ensure that your web server is configured to include cross-origin-related HTTP headers in its responses. These headers enable web browsers using your website to gain access to `SharedArrayBuffer` web API, which is something similar to shared memory on the web.

- [`cross-origin-opener-policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy): `same-origin`
- [`cross-origin-embedder-policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy): `require-corp`.
- [`Cross-Origin-Opener-Policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Opener-Policy): `same-origin`
- [`Cross-Origin-Embedder-Policy`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cross-Origin-Embedder-Policy): `require-corp`.

Additionally, don't forget to specify the MIME type `application/wasm` for `.wasm` files within the server configuration to ensure optimal performance.
12 changes: 10 additions & 2 deletions flutter_ffi_plugin/bin/src/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,16 @@ Future<void> buildWebassembly({bool isReleaseMode = false}) async {
await Process.run("cargo", ["install", "wasm-bindgen-cli"]);

// Verify Flutter SDK web server's response headers.
print("Patching Flutter SDK's web server with CORS HTTP headers.");
await patchServerHeaders();
try {
await patchServerHeaders();
print("Patched Flutter SDK's web server with CORS HTTP headers.");
} catch (error) {
print("Failed patching Flutter's web server with CORS HTTP headers.");
print("Try using the command below.");
print('flutter run' +
' --web-header=Cross-Origin-Opener-Policy=same-origin' +
' --web-header=Cross-Origin-Embedder-Policy=require-corp');
}

// Prepare the webassembly output path.
final flutterProjectPath = Directory.current;
Expand Down
6 changes: 3 additions & 3 deletions flutter_ffi_plugin/bin/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ Future<void> patchServerHeaders() async {
var serverFileContent = await serverFile.readAsString();

// Check if the server already includes cross-origin HTTP headers.
if (serverFileContent.contains('cross-origin-opener-policy')) {
if (serverFileContent.contains('Cross-Origin-Opener-Policy')) {
return;
}

Expand All @@ -475,11 +475,11 @@ Future<void> patchServerHeaders() async {
);
lines.insert(serverDeclaredIndex + 1, """
httpServer.defaultResponseHeaders.add(
'cross-origin-opener-policy',
'Cross-Origin-Opener-Policy',
'same-origin',
);
httpServer.defaultResponseHeaders.add(
'cross-origin-embedder-policy',
'Cross-Origin-Embedder-Policy',
'require-corp',
);""");
serverFileContent = lines.join("\n");
Expand Down
4 changes: 2 additions & 2 deletions flutter_ffi_plugin/example/native/hub/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ crate-type = ["lib", "cdylib", "staticlib"]
[dependencies]
rinf = "6.6.0"
prost = "0.12.3"
wasm-bindgen = "0.2.90"
tokio_with_wasm = "0.4.0"
wasm-bindgen = "0.2.91"
tokio_with_wasm = "0.4.1"
sample_crate = { path = "../sample_crate" }
2 changes: 1 addition & 1 deletion rust_crate/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ allo-isolate = "0.1.24"

[target.'cfg(target_family = "wasm")'.dependencies]
js-sys = "0.3.67"
wasm-bindgen = "0.2.90"
wasm-bindgen = "0.2.91"

0 comments on commit 1872afb

Please sign in to comment.