Don't bundle CanvasKit and Pyodide in CDN-mode web builds; fix --no-cdn loading Pyodide from a CDN (0.86.7) - #6760
Open
FeodorFitsner wants to merge 3 commits into
Open
Don't bundle CanvasKit and Pyodide in CDN-mode web builds; fix --no-cdn loading Pyodide from a CDN (0.86.7)#6760FeodorFitsner wants to merge 3 commits into
FeodorFitsner wants to merge 3 commits into
Conversation
The web template picked between the bundled runtime and jsdelivr with
`{% if cookiecutter.no_cdn == "True" %}`, but `flet build` passes
`no_cdn` through cookiecutter's extra_context as a Python bool. Jinja
compares it as-is, and `True == "True"` is False, so the CDN branch was
taken in both modes: `--no-cdn` downloaded, cached and shipped ~15 MB of
Pyodide that the browser then ignored.
Only pyodideUrl was affected. `flet.noCdn` is derived separately, via
`"{{ cookiecutter.no_cdn }}".toLowerCase() == "true"`, which renders
correctly - so CanvasKit and the fallback fonts loaded locally as
expected while Pyodide alone went to the CDN, which made this look like
a Pyodide quirk rather than a template bug.
The template's other no_cdn test (assets/FontManifest.json) already used
plain truthiness. All conditions now do, and no `== "True"` comparisons
remain under templates/.
Also fixes the bundled URL being origin-absolute: with `--base-url
myapp` the app requested /pyodide/pyodide.mjs while the file sat at
/myapp/pyodide/pyodide.mjs. It now renders relative to the configured
base URL, as canvasKitBaseUrl does; without --base-url the rendered URL
is unchanged.
In CDN mode (the default) Flutter loads CanvasKit from gstatic and Flet
points pyodideUrl at jsdelivr, so neither local copy is ever requested -
yet `flutter build web` always emits canvaskit/ (~37 MB) and
ensure_pyodide() ran unconditionally in both `flet build web` and `flet
publish`, downloading and copying a further ~15 MB.
`flet build web` and `flet publish` now drop both in CDN mode. A minimal
web build goes from 71 MB to 19 MB. `flet build` also clears a pyodide/
left in the reused Flutter project by an earlier --no-cdn build, so
switching modes doesn't silently keep shipping it. --no-cdn (or
[tool.flet.web] cdn = false) still bundles everything, unchanged.
Also decouples where a runtime asset is fetched from from what the build
bundled. flutter_bootstrap.js applied canvasKitBaseUrl and
fontFallbackBaseUrl only inside `if (flet.noCdn)`, so a host serving its
own copy of the runtime had to also set noCdn for the assignment to take
effect - setting the URL alone failed silently and the app booted off
gstatic anyway. Both are now applied whenever set, default to null in
CDN mode, and are pinned by flet build / patch_index.py only when
bundling.
FletJS.canvasKitBaseUrl becomes String? to match. It has no readers in
the Dart tree, so this is an annotation fix only.
Verified end to end on a minimal app: CDN mode builds to 19 MB with no
canvaskit/ or pyodide/, boots in headless Chrome, and its network log
shows chromium/canvaskit.{js,wasm} from gstatic plus the full Pyodide
runtime from jsdelivr, with zero requests to a local canvaskit/ or
pyodide/ path. --no-cdn builds to 71 MB with both directories present
and local URLs, and boots.
Deploying flet-website-v2 with
|
| Latest commit: |
eaa316f
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://c040830e.flet-website-v2.pages.dev |
| Branch Preview URL: | https://fix-web-skip-bundling-cdn-as.flet-website-v2.pages.dev |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Two related web-build defects, one bug fix and one packaging change. Split into separate commits — the first stands alone.
1.
--no-cdnstill loaded Pyodide from jsdelivrThe web template chose between the bundled runtime and the CDN with:
but
flet buildpassesno_cdnthrough cookiecutter'sextra_contextas a Pythonbool, and Jinja compares it as-is —True == "True"isFalse. The CDN branch was taken in both modes, so--no-cdnbuilds downloaded, cached and shipped ~15 MB of Pyodide that the browser then ignored.Only
pyodideUrlwas affected.flet.noCdnitself is derived separately, via"{{ cookiecutter.no_cdn }}".toLowerCase() == "true", which renders correctly — so CanvasKit and the fallback fonts loaded locally as expected while Pyodide alone went to the CDN. That asymmetry made it look like a Pyodide quirk rather than a template bug.The template's other
no_cdntest (assets/FontManifest.json) already used plain truthiness. All conditions now do, and no== "True"comparisons remain undertemplates/.Also fixes the bundled URL being origin-absolute: with
--base-url myappthe app requested/pyodide/pyodide.mjswhile the file sat at/myapp/pyodide/pyodide.mjs. It now renders relative to the configured base URL, ascanvasKitBaseUrldoes. Without--base-urlthe rendered URL is unchanged.2. CDN-mode builds bundled ~52 MB nothing fetches
In CDN mode (the default) Flutter loads CanvasKit from gstatic and Flet points
pyodideUrlat jsdelivr — yetflutter build webalways emitscanvaskit/(~37 MB), andensure_pyodide()ran unconditionally in bothflet build webandflet publish, copying a further ~15 MB. Neither is ever requested.Both are now dropped in CDN mode. A minimal web build goes from 71 MB to 19 MB.
flet buildalso clears apyodide/left in the reused Flutter project by an earlier--no-cdnbuild, so switching modes doesn't silently keep shipping it.--no-cdn(or[tool.flet.web] cdn = false) still bundles everything, unchanged.Decoupling where assets load from, from what got bundled
flutter_bootstrap.jsappliedcanvasKitBaseUrlandfontFallbackBaseUrlonly insideif (flet.noCdn). A host serving its own copy of the runtime — a CDN-restricted network, an air-gapped deployment, a platform mirroring the runtime on its own origin — had to also setnoCdnfor the assignment to take effect; setting the URL alone failed silently and the app booted off gstatic anyway.Both are now applied whenever set, default to
nullin CDN mode, and are pinned byflet build/patch_index.pyonly when bundling.FletJS.canvasKitBaseUrlbecomesString?to match — it has no readers in the Dart tree, so that is an annotation fix only.Verification
End to end on a minimal app, both modes:
--no-cdncanvaskit//pyodide/pyodideUrl/pyodide/pyodide.mjs(was jsdelivr)canvasKitBaseUrlnull→ gstatic/canvaskit/The CDN-mode build's network log shows
chromium/canvaskit.{js,wasm}from gstatic and the full Pyodide runtime from jsdelivr, with zero requests to a localcanvaskit/orpyodide/path — confirming the dropped directories were unreferenced.The bug in §1 is reproduced against
main: rendering the template withno_cdn=Trueemits the jsdelivr URL there and the local path on this branch.Not covered: Chrome only. Safari and Firefox select different CanvasKit variants (Chrome fetched the
chromium/one) and were not exercised.Summary by Sourcery
Adjust web build and runtime configuration to correctly respect --no-cdn, avoid bundling unused CanvasKit/Pyodide assets in CDN mode, and decouple CDN usage from how runtime asset URLs are configured.
Bug Fixes:
--no-cdnweb builds use the bundled Pyodide runtime instead of loading it from jsdelivr.--no-cdnbuilds to honor the configured base URL for sub-path deployments.noCdn, so hosts serving their own copies are respected.Enhancements:
resolve_no_cdnhelper to centralize CDN-mode resolution in the build pipeline.canvasKitBaseUrlandfontFallbackBaseUrldefault to null in CDN mode and apply them only when explicitly set, allowing flexible runtime hosting.FletJS.canvasKitBaseUrltype to nullable to match its runtime usage.Build:
canvaskit/directory from CDN-mode web build output to reduce bundle size.flet publishonly bundles local CanvasKit/Pyodide assets in no-CDN mode and removes them fromdistotherwise.Documentation:
0.86.7release changes in the Python and Dart package changelogs, including CDN and asset-loading behavior.