Loading service worker behind authentication

113 views
Skip to first unread message

Francesco Terrell

unread,
Jun 19, 2022, 6:08:22 PM6/19/22
to Chromium-discuss

Hi everyone!

I have developed a javascript application with a custom service worker and facing a problem fetching the service worker file behind authentication.

My application is deployed behind a cookie based authetication (after login, the user has a cookies which is checked by every request). This is fine for for fetching all resources, except the service-worker file, which the browser doesn't doesn't send any cookies.

Here is miminal example reproducing my issue:

backend: index.js

app.get('/test.js', (req,res) => {
  if(req.cookies.Test != null) {
   res.sendFile('/home/francesco/projects/test-sw/public/test.js')
  }
  else {
   res.sendStatus(401)
  }
})

frontend: index.html

<h1>Test</h1>
// Doesn't work
<script>
const registration = await navigator.serviceWorker.register('test.js')
console.log(registration)
</script>
// Works
<script src="test2.js"></script>

When the request is made, the browser doesn't attach any cookie information chrome devtool image.

Why doesn't the browser treat the service-worker request as everything else? Am i'm missing a flag similiar to fetch credentials option?

Thanks in advanced for any help on the subject

Reply all
Reply to author
Forward
0 new messages