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

[web] support bundling CanvasKit instead of CDN #70101

Closed
kubaprzetakiewicz opened this issue Nov 9, 2020 · 15 comments · Fixed by #92134
Closed

[web] support bundling CanvasKit instead of CDN #70101

kubaprzetakiewicz opened this issue Nov 9, 2020 · 15 comments · Fixed by #92134
Assignees
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter customer: web10 e: web_canvaskit CanvasKit (a.k.a. Skia-on-WebGL) rendering backend for Web P1 High-priority issues at the top of the work list platform-web Web applications specifically
Projects

Comments

@kubaprzetakiewicz
Copy link

Use case

CanvasKit is currently being downloaded from the CDN resource, which can cause problems with very strict content security policies.

Since https://unpkg.com links used by Flutter point to the specific versions of CanvasKit WASM, whitelisting them manually to keep up-to-date is unfeasible (wildcarding is not always possible).

Proposal

Bundle it with the app - this was already mentioned regarding CanvasKit in similar issues, e.g. #53639 (comment)

@sidrao2006
Copy link
Contributor

sidrao2006 commented Nov 9, 2020

This will be useful but it should definitely be made optional, since it will increase the (already big) app size even more

@jonahwilliams
Copy link
Member

@sidrao2006 the binary will be the same size whether you download from a CDN or your own server

@yjbanov
Copy link
Contributor

yjbanov commented Nov 9, 2020

We should support building an app with a bundled version of CanvasKit. However, you can achieve it today with some manual effort. Simply pass --dart-define=FLUTTER_WEB_CANVASKIT_URL=... and host your own copy of CanvasKit at the URL specified in the --dart-define.

@darshankawar darshankawar added platform-web Web applications specifically c: proposal A detailed proposal for a change to Flutter c: new feature Nothing broken; request for a new capability passed first triage labels Nov 10, 2020
@peerwaya
Copy link

peerwaya commented Nov 10, 2020

@yjbanov will. I be able to specify for example --dart-define= FLUTTER_WEB_CANVASKIT_URL=mycanvaskit.wasm where mycanvaskit is adjacent to index.html in the same folder. My use case is to run flutter in electron

@yjbanov yjbanov added the P2 Important issues not at the top of the work list label Nov 12, 2020
@kubaprzetakiewicz
Copy link
Author

We should support building an app with a bundled version of CanvasKit. However, you can achieve it today with some manual effort. Simply pass --dart-define=FLUTTER_WEB_CANVASKIT_URL=... and host your own copy of CanvasKit at the URL specified in the --dart-define.

Unfortunately, that doesn't seem to have any effect.

For example:
➜ flutter run -d chrome --dart-define=FLUTTER_WEB_USE_SKIA=true --dart-define=FLUTTER_WEB_CANVASKIT_URL=https://unpkg.com/canvaskit-wasm@0.20.0/bin/canvaskit.js

above command still loads the default 0.19.0 from unpkg.

I also cannot find any reference to this specific Dart define in code.

@yjbanov yjbanov added the e: web_canvaskit CanvasKit (a.k.a. Skia-on-WebGL) rendering backend for Web label Dec 10, 2020
@yjbanov
Copy link
Contributor

yjbanov commented Dec 10, 2020

Unfortunately, that doesn't seem to have any effect.

For example:
➜ flutter run -d chrome --dart-define=FLUTTER_WEB_USE_SKIA=true --dart-define=FLUTTER_WEB_CANVASKIT_URL=https://unpkg.com/canvaskit-wasm@0.20.0/bin/canvaskit.js

above command still loads the default 0.19.0 from unpkg.

The value should be https://unpkg.com/canvaskit-wasm@0.20.0/bin/ (i.e. no canvaskit.js at the end). Your command-line should have crashed the app. I'm surprised that it still worked 🤔

@yjbanov yjbanov added this to To do in PWA Feb 11, 2021
@yjbanov yjbanov changed the title [Web] CanvasKit binary being bundled instead of fetched from the CDN [web] support bundling CanvasKit instead of CDN Feb 11, 2021
@yjbanov
Copy link
Contributor

yjbanov commented Feb 11, 2021

Added this to the PWA project because this is required in order to support fully offline apps out-of-the-box.

@evenremy
Copy link

We should support building an app with a bundled version of CanvasKit. However, you can achieve it today with some manual effort. Simply pass --dart-define=FLUTTER_WEB_CANVASKIT_URL=... and host your own copy of CanvasKit at the URL specified in the --dart-define.

Unfortunately, that doesn't seem to have any effect.

For example:
➜ flutter run -d chrome --dart-define=FLUTTER_WEB_USE_SKIA=true --dart-define=FLUTTER_WEB_CANVASKIT_URL=https://unpkg.com/canvaskit-wasm@0.20.0/bin/canvaskit.js

above command still loads the default 0.19.0 from unpkg.

I also cannot find any reference to this specific Dart define in code.

Same problem for me.

@jonahwilliams
Copy link
Member

jonahwilliams commented Mar 15, 2021

this setting will only work in release/profile mode. in debug mode the engine is prebuilt so the string cannot be changed.

@philippecade
Copy link

Similar issue loading fonts at #77580

@Wissperwind
Copy link

Wissperwind commented Jul 6, 2021

While we are in the workaround state:
Does someone knows a way to find out programatically, which version of canvas kit do bundle?
So how does my script know if it should bundle vserion 28 or 29 in future?

@philippecade
Copy link

@Wissperwind: we take the version directly from the main.dart.js file and download this one locally. So the version can change and we will always use the correct one.

We have this code in our pipeline:

#!/bin/sh
# Downloads WASM locally and use local fonts
# Temporary solution until https://github.com/flutter/flutter/issues/70101 and 77580 provide a better way
flutter build web
wasmLocation=$(grep canvaskit-wasm build/web/main.dart.js | sed -e 's/.*https/https/' -e 's/\\/bin.*/\\/bin/' | uniq)
echo "Downloading WASM from $wasmLocation"
curl -o build/web/canvaskit.js "$wasmLocation/canvaskit.js"
curl -o build/web/canvaskit.wasm "$wasmLocation/canvaskit.wasm"
sed -i -e "s!$wasmLocation!.!" \\
  -e "s!https://fonts.gstatic.com/s/roboto/v20/KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf!./assets/packages/amos_mobile_widgets/assets/google_fonts/Roboto-Regular.ttf!" \\
  -e "s!https://fonts.googleapis.com/css2?family=Noto+Sans+Symbols!./assets/assets/css/Noto-Sans-Symbols.css!" \\
  -e "s!https://fonts.googleapis.com/css2?family=Noto+Color+Emoji+Compat!./assets/assets/css/Noto-Color-Emoji-Compat.css!" \\
  build/web/main.dart.js

@jhollar
Copy link

jhollar commented Aug 15, 2021

just so I'm clear from all the discussions, a future version of flutter will support the caching of canvaskit so the CDN approach will not be used?

@mariamhas mariamhas added P1 High-priority issues at the top of the work list and removed P2 Important issues not at the top of the work list labels Oct 21, 2021
@Wissperwind
Copy link

Can someone please explain or link how the fix work?
Will we be able to ship flutter applications to intranets that block unpkg.com?

@github-actions
Copy link

github-actions bot commented Nov 4, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
c: new feature Nothing broken; request for a new capability c: proposal A detailed proposal for a change to Flutter customer: web10 e: web_canvaskit CanvasKit (a.k.a. Skia-on-WebGL) rendering backend for Web P1 High-priority issues at the top of the work list platform-web Web applications specifically
Projects
PWA
To do
Development

Successfully merging a pull request may close this issue.