shadowwalker / next-pwa

Zero config PWA plugin for Next.js, with workbox 🧰
MIT License
3.88k stars 324 forks source link

custom service-worker registration failed #242

Open enes-get-local opened 3 years ago

enes-get-local commented 3 years ago

next version : 11.0.1-canary.4 next-pwa: ^5.2.21

I've created custom worker to handle chat message sending when user offline. Followed this example

this custom worker detect stored messages in sync event from indexed db and send them in a http request to our graphql api.
It works on my localhost perfectly but it doesn't work on server. I m using azure app service for next.js project.

I have worker/index.ts file at root of project.

This is next.config.js

pwa: {
    // disable: process.env.NODE_ENV === 'development',
    dest: 'public',
  },

This custom app file (handle service worker)

const srv = createServer((req, res) => {
    if (config.useAppInsights) {
      appInsights.defaultClient.trackNodeHttpRequest({
        request: req,
        response: res,
      });
    }
    const parsedUrl = parse(req.url, true);
    const { pathname } = parsedUrl;

    // handle GET request to /service-worker.js
    if (pathname === '/sw.js' || /^\/(workbox|worker|fallback)-\w+\.js$/.test(pathname)) {
      const filePath = join(__dirname, '.next', pathname);
      app.serveStatic(req, res, filePath);
    } else {
      handleNextRequests(req, res);
    }
  });

I m logging some strings head of the service worker file. It is logging these strings but sync event doesn't work on server.

image

Any help ?

Also i have error message : image

shadowwalker commented 3 years ago

I'm wondering why it's expecting a service-worker.js file served from / root path? When you run yarn build, what files get generated inside public folder?

enes-get-local commented 3 years ago

I'm wondering why it's expecting a service-worker.js file served from / root path? When you run yarn build, what files get generated inside public folder?

image This is how looks after run build. My custom worker which is under worker/index.ts file, generated as worker-CXb2Co........

enes-get-local commented 3 years ago

I'm wondering why it's expecting a service-worker.js file served from / root path? When you run yarn build, what files get generated inside public folder?

@shadowwalker is there any solution ? this issue blocked me. By the way error is gone probably was coming from cache. It doesn't expect /service-worker.js file. But still i can't register custom worker file.

image