-
Notifications
You must be signed in to change notification settings - Fork 0
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
Service worker #75
Service worker #75
Conversation
efb72fd
to
65c434d
Compare
There is currently an issue when updating the service worker: when a new version of the service worker exists, refreshing the page causes all subsequent fetch requests to fail. This doesn't happen in hydrogen, which makes me think that it might be related to the fact that we're running in an iframe. I've invested significant time into debugging this without success. For the sake of time-boxing, I will leave this on hold for now, to focus on other features that might have higher priority than this. Will revisit soon with a fresh mind, to dig into the issue. |
1f6a723
to
77b69fd
Compare
I've seen this happen in https://github.com/WordPress/wordpress-wasm too! Adding this to the service worker helped: /**
* Ensure the client gets claimed by this service worker right after the registration.
*
* Only requests from the "controlled" pages are resolved via the fetch listener below.
* However, simply registering the worker is not enough to make it the "controller" of
* the current page. The user still has to reload the page. If they don't an iframe
* pointing to /index.php will show a 404 message instead of a homepage.
*
* This activation handles saves the user reloading the page after the initial confusion.
* It immediately makes this worker the controller of any client that registers it.
*/
self.addEventListener('activate', (event) => {
// eslint-disable-next-line no-undef
event.waitUntil(self.clients.claim());
}); Also, I register the service worker like this: const registration = await navigator.serviceWorker.register(scriptUrl);
await registration.update();
// Just in case, let's confirm the worker is already running. On Chrome
// this test sometimes fails for me, on other browsers it mostly works.
const workerRunning = await fetch('/service-worker-test');
// /service-worker-test is only served by the service worker
if(workerRunning.status !== 200) {
window.location.reload();
} |
Thanks @adamziel! |
Not too clear what this is, but seems to be required by the service worker.
34ebe13
to
3206238
Compare
I've now managed to make the service worker updates work consistently both in Firefox and Chrome. The issue I was having seems to be related to our However, currently the user is asked through an alert that blocks the whole page whether they want to apply the new version: If I remove this alert and silently update, on Chrome, sometimes the update works (the new service worker boots correctly), sometimes it doesn't. On Firefox it works consistently, so this seems to be an issue with Chrome. My guess is this is due to some timing issue, since there's asynchronous stuff going on. In any case, I think the user should be asked if they want to update, as they might be composing a message, for example, which would be lost. However, the alert should only block the chat "window", not the whole page. So I will proceed to implement an "alert" that only covers the chat "window". |
Thanks for the help debugging this @ashfame ! |
The file path in hydrogen is: src/platform/web/sw.js. File has not been modified yet.
3206238
to
b1410f3
Compare
This is a good point. Do you think we can adapt the text to explain this? Something along the lines of "If you press OK this will reload the chat, any typed messages will be gone".
Is this necessary or could we just make it work with improved wording in the box? |
This is great news! Thanks for continuing the work on this! Now that this is working, could you confirm what the service worker specifically handles?
Thanks! |
For the alert-replacement we can use the |
b1410f3
to
48d7afd
Compare
I've updated the PR description with some more details on this. I've also added a screenshot of the update dialog. Note that there's still an issue on Chrome (#140), but I'd rather merge this PR and address that issue separately. Also note that the service worker does not actually allow having more that one instance of chatrix active (more details in PR description):
So this is unfortunate, but I think the service worker still provides value, as it prevents the user corrupting the store. Addressing this issue to make it possible to have more than one active session would need to be done upstream, in hydrogen. I'm happy to explore this if we think there is a need for it. |
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.
Not allowing a second instance of the chat is not the best but also better than an experience where messages might be missing from the communication, making things appear broken.
Could you provide one more screenshot that shows what happens when you open an second tab? Which one is the chat that will keep working, the first one or the second one?
2 things come to mind:
|
I added a screen recording to the PR description that shows the interaction. But to answer the question: the user of the first tab sees the session selection screen.
No, our service worker has exactly the same functionality as hydrogen's, the only difference being the upgrade process, where hydrogen shows an |
Thanks for the screen recording, I see what happens and its good enough to avoid any confusions for now. Seems a deliberate switch out of the room in the code somewhere. Feel free to move forward. Re: service worker, this would mean we would have to manually catch up with Hydrogen service worker changes to be copied over to ours as they happen, right? Would be nice to not have to deal with that, eventually. |
Fixes #70
This PR introduces the Service Worker from hydrogen, which has two responsibilities:
*.html
,*.css
, etc) are cached by the service worker so it's possible to load the client when there is no internet connection.TODO
Screen captures
Screen.Recording.2022-11-29.at.11.59.15.mov