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

379
Visualizações
Service worker in NX/React workspace can"t be register because of "import"

I have a workspace in react where I am trying to setup service workers.

in main.tsx I do :

import * as serviceWorkerRegistration from './serviceWorkerRegistration';

serviceWorkerRegistration.register({
  onSuccess: () => pwaStore.setState((state) => ({ ...state, isReady: true })),
  onUpdate: () => pwaStore.setState((state) => ({ ...state, hasUpdate: true })),
});

serviceWorker.js is the same as the default one from pwa-template, I just changed this line to work with NX.

      const swUrl = `${process.env.NX_PUBLIC_URL}/service-worker.js`;
      registerValidSW(swUrl, config);

service-worker.js is the default one and contains some imports

import { clientsClaim } from 'workbox-core';
import { ExpirationPlugin } from 'workbox-expiration';
import { precacheAndRoute, createHandlerBoundToURL } from 'workbox-precaching';
import { registerRoute } from 'workbox-routing';
import { StaleWhileRevalidate } from 'workbox-strategies';

Now, when I deploy and run this project, I am prompted with

Error during service worker registration: DOMException: Failed to register a ServiceWorker for scope ('xxx') with script ('xxx/service-worker.js'): The script has an unsupported MIME type ('text/html').

So, I guess the service-worker is not found when loading from direct url. So I added it on the build

        "assets": [
          "apps/core/src/service-worker.js",
          "apps/core/src/manifest.json",
          "apps/core/src/robots.txt",
          "apps/core/src/favicon.ico",
          "apps/core/src/assets"
        ],

and now, it does find it, but I get prompted with

Uncaught SyntaxError: Cannot use import statement outside a module (at service-worker.js:1:1)

I am unsure on how to fix this now.

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