-
Notifications
You must be signed in to change notification settings - Fork 29.1k
[web] new service worker loading mechanism #75535
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
Conversation
Great to see the service worker is getting some love. Quick question, how is the service worker tested on a local http server? I thought they require https to work. |
82f544b
to
2531654
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this doesn't quite work in debug mode, but could be tweaked to always fallback if the revisions are null
|
||
// If service worker doesn't succeed in a reasonable amount of time, | ||
// fallback to plaint <script> tag. | ||
setTimeout(() => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is always going to fail to load when you flutter run
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Done.
<link rel="manifest" href="manifest.json"> | ||
</head> | ||
<body> | ||
<!-- This script installs service_worker.js to provide PWA functionality to | ||
application. For more information, see: | ||
https://developers.google.com/web/fundamentals/primers/service-workers --> | ||
<script> | ||
var serviceWorkerVersion = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably need to check for null since this won't be set during flutter run.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested various flutter run
modes. This seems to be working fine with ?v=null
. I'm open to stylistic improvements, but AFAICT it's not causing trouble.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the serviceWorkerVersion
even required? Isn't the browser automatically checking the service worker file for byte differences?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could experiment with alternative approaches, but right now I'm using the version number to check that the currently active service worker is the one we want. The version is encoded in the scriptURL
that I can read back from reg.active
. If the versions don't match I force update. This happens when you pull down a new index.html
but the worker is still from the old version of the app.
|
chrome (and possibly edge) both support service workers over localhost for local development. |
Thanks. Makes total sense for Anyway, one thing to note is that when the service worker is registered and it activates the first time, it will not automatically |
6640aae
to
2f1d704
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One of my concerns with shipping this change in the template is that users need to regen to get it - meaning we lose the opportunity to fix bugs that we identify in the future.
Unless we very pro-actively communicate, users might get stuck on old versions/
We do have a pattern for an automatic Migrator that can run over projects, but that is really only intended for simple changes to config files. A user may make small changes to the sw loading that prevent it from running.
Hey guys, I would like to add my two cents, hope I am not overstepping. I have a Flutter web app in production for some time now and I do regular updates. It has been a great experience working with the platform but the service worker has always been kind of a pain to maintain. I use a slightly different version than the one generated by Flutter and also use a similar approach as this PR where I modified The main issue users seem to face is that they do not get the latest version when they do an update because As a workaround I was thinking to use a small javascript script above the Would this approach work, or am I missing something? |
@azbuky you're definitely not overstepping, getting feedback from Flutter users is critical for us to improve the SDK |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
From what I have tested this works for me and fixes all major issues I was having with deploying updates. There is one edge case thought that I think will cause assets to not update correctly. I will use galley.flutter.dev as an example. The server seems to be sending a response header |
@azbuky Thanks for kicking the tires of this new approach! Since this improves the situation for CORE resources, I'm going to go ahead and merge it. I will look into other resources as a follow-up task. In addition to resources, another thing that's still not solved is that we're loading CanvasKit from unpkg, which cannot be cached by the service worker. We should have a mode that bundles CanvasKit such that it can be cached. |
@azbuky great tips here, how did you go about using a html splash screen with flutter? I really need this as canvaskit takes quite some time to load. Thanks |
?revision=*
from CORE resources.Fixes #74831.
According to my testing this also fixes the issue with having to double-reload the page to get the new app version.