Open nelsonic opened 1 year ago
At the time of writing, there is no question on StackOverflow
matching the keywords:
flutter web reduce unused javascript
https://stackoverflow.com/search?q=flutter+web+reduce+unused+javascript
Feels like there's an opportunity here for someone
to get some SO
points ... π
With Smoke and Mirrors: π
With the addition of:
https://github.com/dwyl/app/blob/f6a0eff9e47120a37aea30953b34ad46662f0d11/web/index.html#L38
We now have a rudimentary "bot detector":
That means Lighthouse
/ PageSpeed Insights
will not load/execute any JavaScript
.
We definitely haven't made the problem "go away".
We've just created some "interactivity" for loading the Flutter Web App
.
That ... just happens to give us:
100
Performance Score πYes, this is "smoke and mirrors" because we haven't actually addressed the elephant in the room: main.dart.js
... π
But by drastically reducing the Time to Interactive
we let the page load 10x
faster on Mobile
. π±
Which is game-changing for UX
. π
I still very much want to figure out if we can reduce the size of main.dart.js
... π
But not as worried about it now during our MVP
. βΊοΈ
We know it's a "problem" but it's one people only experience once on their mobile device. 1οΈβ£
Because once the Flutter App
is loaded it's fast! π
I tried to get esbuild
to minify the main.dart.js
with:
https://github.com/dwyl/app/blob/f6a0eff9e47120a37aea30953b34ad46662f0d11/web/build.js#L16-L23
Which I was attempting to run in the deploy.yml
script after the flutter build web
...
It works on localhost
and minifies the files! π
But the filesystem is not writeable on GitHub Actions
:
https://github.com/dwyl/app/actions/runs/4340894609/jobs/7579880670#step:7:19
So it doesn't work on CI
We could attempt to get this working on GitHub Actions
and it could very well end up optimising the JS
. π --> ποΈ
But I don't know if Flutter
will behave "unpredictably" if we tree-shake main.dart.js
manually. π€
If anyone else
wants to spend an hour trying to get this working it would be awesome! π
We are using flutter web in our own app too. We patched flutter generated output to download resources in parallel.
See it here. https://github.com/flutter/flutter/issues/76009#issuecomment-1452281127
@harryfei thanks very much for sharing. π @LuchoTurtle do you want to try and replicate (and document) the changes made in: https://github.com/enkra/enkra-send/commit/a2ecf6b8f41e8ca35e8ceb62fb1a63fc3bf25c28 π
Hm I've created the .patch
files by following https://www.cyberciti.biz/faq/appy-patch-file-using-patch-command/ and I was testing this locally and it worked.
But after re-building the project by running flutter build web --web-renderer html
, I realised that the file flutter.js
wasn't created within the build
folder.
However, it is created when I run flutter build web
(which I assume it builds with CanvasKit - which was used in https://github.com/flutter/flutter/issues/76009#issuecomment-1452281127). Which, in turn, makes me believe that this approach might not be suitable/relevant when using the html
renderer? I say this because of https://geekyants.com/blog/web-renderers-which-one-to-choose-html-or-canvaskit/, where it states that by using html
, the download size is lower (I wager flutter.js
is not created).
I might be wrong though, because I just noticed this behaviour when I was trying to tinker with patching the build
files with the patch
command.
Checking https://github.com/flutter/flutter/issues/104830...
Yep, running flutter clean
solves this hurdle...
enkra-send's flutter speedup patch always preload canvaskit.js and cavaskit.wasm https://github.com/enkra/enkra-send/blob/a2ecf6b8f41e8ca35e8ceb62fb1a63fc3bf25c28/build_tools/web/flutter.js.patch#L10-L11
You can delete those lines if you build with --web-render html
Yeah, I figured you were using the canvaskit
web renderer when I looked at those lines. I was just having some trouble understanding why I didn't have the flutter.js
file when building the bundle. Turns out I had to run flutter clean
instead of just deleting the build
folder for it to be generated again. Since y'all patch the flutter.js
file alongside flutter_service_worker.js
, this is why it wasn't working.
Thank you very much β€οΈ @harryfei
@LuchoTurtle has documented the patch suggested by @harryfei in: https://github.com/dwyl/learn-flutter/tree/main/guides/flutter-web-speed-boost π
With the recent deployment to
GitHub Pages
: https://github.com/dwyl/app/issues/322#issuecomment-1454603830 theperformance
score is now70
: https://pagespeed.web.dev/report?url=https%3A%2F%2Fapp.dwyl.com πwhile
much better than the32
we were seeing last week and definitely something to celebrate! π₯³ it's still unacceptably low. π It must be possible to achieve100
even if we need to resort to "smoke and mirrors". π€i.e: we create a "mockup" of the
App
in PureHTML
andCSS
and replace the basicloading
screen with a "Useful tips" page and deliberately delay downloading anyJS
usingSetTimeout
β So the page is fully interactive andPageSpeed Insights
can turn a blind eye to theJS
. π see: https://github.com/dwyl/app/issues/325Reduce unused
JavaScript
The main recommendation
PageSpeed Insights
offers is "Reduce unusedJavaScript
" ... As noted in: https://github.com/dwyl/app/issues/315#issuecomment-1441279519 themain.dart.js
file is massive!!https://github.com/dwyl/app/blob/af4e656aa5d84102f969b9ce3b336affda521ba2/main.dart.js
The
flutter.js
file doesn't appear to be the problem: https://github.com/dwyl/app/blob/af4e656aa5d84102f969b9ce3b336affda521ba2/index.html#L26 It's not a very big file, only376 loc
and13.6kb
: https://github.com/dwyl/app/blob/af4e656aa5d84102f969b9ce3b336affda521ba2/flutter.jsTodo
JS
(main.dart.js
being the biggest file!) e.g: can we split out the engine and our application code / business logic into separate files for parallel downloading?main.dart.js
, we're definitely not using them all! See: https://github.com/flutter/flutter/issues/46589Starting point:
App
is small this might not have a very big impact. But as it grows this might become essential for reducing theAPK
size and ensuring fast load times. Again, this may not impact theWeb App
... it may only be theAndroid
version, but could still be worth exploring. πWait for
Wasm
? β³ π€·ββοΈWe could just focus on building features and wait patiently for
Flutter
to be compiled toWebAssembly
which will eliminate theJS
(or at least drastically reduce it ...) π This might be awhile
... see: https://github.com/flutter/flutter/issues/41062 and https://github.com/flutter/flutter/issues/53041#issuecomment-1405501969I'm not holding my breath that
Flutter Web
will be compiled toWasm
in 2023 ... β³ π€·ββοΈ And we need to ship ourFlutter Web App
and get to 1kpeople
ASAP.Sponsor/Bounty a
Flutter
Expert? π°Given that we hypothesize that initial
Web App
loading time will be one of the biggest reasons forpeople
to "bounce" from thedwyl web app
, we could figure out how to setup a "bounty" to get aFlutter
expert to help us with this.priority-2
? π₯ π€·ββοΈwhile
we would love to dedicate all of our resources to fixing this immediately. π We have done a decent amount of googling and nothing obvious jumps out. π π€·ββοΈ We cannot allocate anundefined
amount of time into this at the expense of building features. πWe're assigning
priority-2
to this because: 2οΈβ£ a) this not a feature thatpeople
trying theApp
are going to say "wow" I need thisApp
in my life! π b) it's an "unknown unknown" i.e. we have no idea (up-front) how long this will take to fix. π€·ββοΈ c) there could be a "rug pull" in the form of: πDart v3
compilation toWasm
: https://medium.com/dartlang/dart-3-alpha-f1458fb9d232 π€Flutter
eventually releases a new version that dramatically improvesWeb
compilation. πSo ... this is something we need to keep chipping away at π§βπ» and leaving comments on this issue with everything that we try along the way. π¬