Angular Service worker incorrectly intercepting http redirects (302)
See original GitHub issueHello,
i work on with angular pwa project with spring-boot back end.
In my project, i am using Angular and Spring boot. My service worker is generating by Angular when i build my project.
I use external service (auth2) and my back-end redirecting to login page when session times out. This works perfectly fine without the service worker enabled, however with the service worker this fails.
To my observation, the mode of the Request is changed from navigate to cors. i see that in request headers
Without SW:
Sec-Fetch-Dest: document Sec-Fetch-Mode: navigate
With SW:
Sec-Fetch-Dest: empty Sec-Fetch-Mode: cors
When active service worker, i have this exception :
Access to fetch at 'https://authentication-url.com/...' (redirected from 'https://my-w ebsite.com/file-request.json') from origin 'https://my-website.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
I tried adding ngsw-bypass=true query param to the redirect, but that did not help.
Can you help me ? I search on web but i found nothing on this problem with Angular service worker.
πππππππππππππππππππππππππππ
Issue Analytics
- State:
- Created 3 years ago
- Comments:28 (23 by maintainers)
Top GitHub Comments
So, it sounds you like all three options π
What I like about the 3rd oprion is that it is more generic. I would like to try that first (unless we find out it has shortcomings while working on it). (This is also likely the most straight forward to implement, because the association between the requested resources and their cache is clearly defined.)
SW issues are generally not good first issues, because SW is quite different than other front-end code people are used working on. (Itβs a little like a server and is very powerful (i.e. even a small change in behavior can significantly impact apps).
But you already seems to have a good grasp of SWs (and
@angular/service-worker
in particular), so you might want to give it a go π(Truth be told, this is probably going to be somewhat more complicated than expected.)
This is the exact problem I was facing as well before and opened two issues:
Hope this can help us moving this issue forward.
I think the reproduction should be possible with the minimal setup just creating a dummy redirect with any server while having Angular Service Worker enabled.
@gkalpak your initial thoughts on the matter were the following:
If your stance on this issue has changed that would be really great. If not we do have a client redirect as a workaround. I am slightly surprised that only a handful of people hit this problem, considering http redirect is an valid solution for a wide range of problems.