w3c / w3c/ServiceWorker

Loading service worker behind authentication

Open
#1,648 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Bikeshed
Stars
3.6k
Forks
324
Avg merge
14d 22h
Merged PRs (30d)
1

Description

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 authentication (after login, the user has a cookie 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

// Service worker file
app.get('/test.js', (req,res) => {
    if(req.cookies.token != null) {
        res.sendFile('./public/test.js')
    }
    else {
        res.sendStatus(401)
    }
})

// Index page
app.get('/', (req, res) => {
  
  // initial login check goes here 

  res.cookie('token',"<user token>").sendFile('./public/index.html')
})

frontend: index.html

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

When the request is made, the browser doesn't attach any cookie information:

Chrome dev tools

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the minimal reproduction in index.js and index.html, then inspect how service-worker registration requests handle authentication credentials. A useful outcome would be a documented explanation of the observed behavior and a clear determination of whether a specification or implementation change is required.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
frontend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.