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

Chore: Optimising Flutter Web App Page Load Time [Smoke and Mirrors] 🪄 #325

Open
1 task done
nelsonic opened this issue Mar 4, 2023 · 2 comments
Open
1 task done
Labels
chore a tedious but necessary task often paying technical debt flutter Flutter related issues T4h Time Estimate 4 Hours tech-debt A feature/requirement implemented in a sub-optimal way & must be re-written technical A technical issue that requires understanding of the code, infrastructure or dependencies

Comments

@nelsonic
Copy link
Member

nelsonic commented Mar 4, 2023

At present there are a handful of instances of <script> in the index.html file:

image

The first instance is just a variable that Flutter updates. Nothing to do here.

app/index.html

Lines 21 to 24 in af4e656

<script>
// Version injected by flutter build, do not touch.
var serviceWorkerVersion = '3511979493';
</script>

flutter.js is already loaded after DOM with the defer keyword:

app/index.html

Line 26 in af4e656

<script src="flutter.js" defer></script>

The only way we can improve this is to dynamically create this <script> tag 🪄
and only load flutter.js after the DOM has finished loading. ⏳
I have an idea for how we can do this but it's going to require some JS wizardry. 🧙

The interesting (complex) part is window.addEventListener('load' ... block:

app/index.html

Lines 39 to 54 in af4e656

<script>
window.addEventListener('load', function(ev) {
// Download main.dart.js
_flutter.loader.loadEntrypoint({
serviceWorker: {
serviceWorkerVersion: serviceWorkerVersion,
},
onEntrypointLoaded: async function(app) {
let appRunner = await app.initializeEngine();
await appRunner.runApp();
// Remove loading screen when App available:
document.querySelector('#loading').remove();
}
});
});
</script>

Todo

  • experiment with loading all of the JS after the DOM has loaded.
@nelsonic nelsonic added chore a tedious but necessary task often paying technical debt technical A technical issue that requires understanding of the code, infrastructure or dependencies T4h Time Estimate 4 Hours flutter Flutter related issues tech-debt A feature/requirement implemented in a sub-optimal way & must be re-written labels Mar 4, 2023
@nelsonic
Copy link
Member Author

nelsonic commented Mar 6, 2023

With the addition of:

<button onclick="human();">I am not a robot</button>

We now have a rudimentary "bot detector":

image

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 🚀

100-perf-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! 🐎

@nelsonic
Copy link
Member Author

nelsonic commented Mar 6, 2023

PR: #327 🙏

SimonLab added a commit that referenced this issue Mar 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore a tedious but necessary task often paying technical debt flutter Flutter related issues T4h Time Estimate 4 Hours tech-debt A feature/requirement implemented in a sub-optimal way & must be re-written technical A technical issue that requires understanding of the code, infrastructure or dependencies
Projects
None yet
Development

No branches or pull requests

1 participant