Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

205
Visualizações
Loading service worker behind authentication

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 devtool image.

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

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda