Closed sofiaps closed 4 months ago
Hello @sofiaps!
I don't see anything that jumps out to me based on the code you shared or the description of the problem.
Do you have an instance of your service worker and web app deployed at a public URL that I could try out? I'd like to take a closer look and debug further. If you can't share the URL publicly, you can DM @jeffposnick
on Twitter.
Hello @jeffposnick and thanks for the prompt response!
It seems that the first API response was being received before the service worker registration was through. I am registering the service worker using APP_INITIALIZER, so that I make sure the service worker has been registered before anything else and it seems to be working as expected now.
Hi again @jeffposnick ! I have another question that came up while trying to register PUT/POST calls.... Is there any way to get the body sent in the handlerCallbak function, so that I could manage the failed requests on network errors? Thanks in advance! :)
If you'd like to access the body of a failed POST
/PUT
request in the event of a failure, you need to clone()
the request before it's sent to the network. After it's already been sent to the network, the body is considered "consumed" and can't be accessed again.
The easiest way to do this using Workbox would probably be via a custom fetchDidFail
plugin, which gives you a convenient way to package up reusable logic that runs when the underlying network request failed.
If you use a fetchDidFail
plugin, Workbox will take care of calling clone()
on the requests for you, so you should always be able to access the body of what's passed in to the callback:
Thanks again @jeffposnick for your great support! For now, a simple request.clone().json() has worked for me in order to retrieve the body in my registered routes for the POST/PUT calls, but I am about to re-write my service worker using custom strategies and plugins, so they will for sure come in handy. :+1: :slightly_smiling_face:
Hello @jeffposnick! May I ask another question? I use the Queue class to store and replay tge failed requests, but I would like to send a message from the service worker to the app, once the replay is done, so that I could do some more actions on the client side, but I haven't found a way to do that.. Is it possible to post a message from the service worker (outside the 'message' event-listener) , without having to receive one from the client first?
Does the answer at https://stackoverflow.com/a/67537862/385997 help?
If so, we can get that example moved into the docs so that it's easier to find.
@jeffposnick Unfortunately, I'm not able to use 'self' to get the clients. I've tried it before, since it is mentioned in the docs that we should now use self.skipWaiting() instead of just skipWaiting, but I keep getting an error, since in order for my service worker to properly work, I have it declared as declare const self: ServiceWorkerGlobalScope & ServiceWorker;
. I'm not sure how 'self' should be declared, in order to achieve that with Angular/TS.
Inside of a service worker script, you should use
declare const self: ServiceWorkerGlobalScope;
(and also make sure the 'webworker'
lib
is included in your TypeScript compilation).
You should not use & ServiceWorker
to combine the types, as self
is not an instance of ServiceWorker
.
@jeffposnick yes, sorry! I changed it recently to try something out, but even when declaring it as ServiceWorkerGlobalScope, I cannot access clients (or skipWaiting() ) this way unfortunately.
Also, I cannot use 'webworker' in my lib file, since when I do, I get errors on building
Only when I remove 'webworker' from lib it builds properly
I am not 100% sure how to help you, as we're wading into TypeScript setup issues that are unrelated to Workbox.
But I can tell you that skipWaiting()
is defined as part of the ServiceWorkerGlobalScope
inside of the webworker
TypeScript library, so if you're not using webworker
, that's why you can't find skipWaiting()
.
@jeffposnick I tried by including webworker lib in a different tsconfig.json in my service-worker folder, which extends the main one, and also by triple slash reference... It just doesn't want to work :smile: Anyway, thanks for your help, I really appreciate it!
Forgot to mention that I found a workaround, in case anyone faces the same problem in Angular. I ended up calling the function mentioned at https://stackoverflow.com/a/67537862/385997 from a JS file (created along with the respective typings)
Hi there,
Workbox is moving to a new engineering team within Google. As part of this move, we're declaring a partial bug bankruptcy to allow the new team to start fresh. We realize this isn't optimal, but realistically, this is the only way we see it working. For transparency, here're the criteria we applied:
v7
branch.Thanks, and we hope for your understanding! The Workbox team
Library Affected: workbox-core
Browser & Platform: all browsers
Issue or Feature Request Description: registerRoute does not work after service worker has been activated, although clientsClaim has been called.
My service-worker.ts file:
My main.ts file:
and my package-json:
I also tried calling self.clients.claim() in the activated event listener, but that's also not possible:
Any help would be much appreciated! Thanks!