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

Credentials missing in navigate requests #3336

Open
jarkt opened this issue Jun 29, 2024 · 1 comment
Open

Credentials missing in navigate requests #3336

jarkt opened this issue Jun 29, 2024 · 1 comment

Comments

@jarkt
Copy link

jarkt commented Jun 29, 2024

Library Affected:
workbox-strategies

Browser & Platform:
Google Chrome 126 for Android and surely other versions

Issue or Feature Request Description:
I've noticed a behavior of Chrome that seems to be inconsistent and leads to a problem that I can't solve using the workbox way.
When a service worker is installed and I open a tab with the app, credentials (cookies in this case) are "inside" the request.
But this is not the case when the PWA is installed as an Android app. In this case the initial call is without credentials.
I'm not sure if this is correct from CORS rules or a bug in Chrome, but it doesn't matter.
That's why I've added this code to my service worker (I haven't configured any other runtime caching yet):

registerRoute(
  () => true,
  new NetworkOnly({
    fetchOptions: {
      credentials: 'include',
    }
  })
)

But it has no effect, because options of "navigate" requests are not passed to the fetch function: https://github.com/GoogleChrome/workbox/blob/v7/packages/workbox-strategies/src/StrategyHandler.ts#L201

This was my test code to see that credentials=include would solve my problem, which it does:

self.addEventListener('fetch', (event) => {
  const request = event.request.clone()

  event.respondWith(
    fetch(request, {
      credentials: 'include'
    })
  )
})

I don't know why the options are not passed. Can I solve the problem in another way?

@jarkt
Copy link
Author

jarkt commented Jun 29, 2024

Perhaps this code at the top of the service worker is a workable solution without causing conflicts with some workbox functions like pre- or runtime caching:

self.addEventListener('fetch', (event) => {
  if (event.request.mode === "navigate") {
    event.respondWith(
      fetch(event.request, {
        credentials: 'include'
      })
    )
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant