ooade / NextSimpleStarter

:whale: Simple and Accessible PWA boilerplate with Nextjs 12 and MUI
https://nextss.vercel.app
MIT License
940 stars 200 forks source link

NextJS SW registration failed #174

Closed seraj closed 3 years ago

seraj commented 3 years ago

hi thanks for this awesome repo

I use this repo for my project and it's working fine. but one thing happen for me.

I don't deploy it on now or Heroku. we have our server and I just use ssh for deploying the project on the server.

and this is my console

SW registration failed:  TypeError: Failed to register a ServiceWorker for scope ('https://dibaache.com/') with script ('https://dibaache.com/service-worker.js'): A bad HTTP response code (404) was received when fetching the script.
ooade commented 3 years ago

The service worker is generated in the _next/static directory. It can be accessed via this route.

So, one thing you could do is set a redirect rule to help with that.

seraj commented 3 years ago

UPDATE: i use fs package to copy service-worker.js to public folder and it's work. thank you

copy-sw.js:

const fs = require(`fs`).promises;

Promise.all(
    [`service-worker.js`].map((fileName) =>
        fs.copyFile(`./.next/static/${fileName}`, `./public/${fileName}`).then(() => fileName),
    ),
).then((fileNames) =>
    console.log(`The following files were copied to './static': ${JSON.stringify(fileNames)}`),
);

add this script to package.json

"copy-sw": "node copy-sw.js",
"build": "next build && yarn copy-sw",
ooade commented 3 years ago

UPDATE: i use fs package to copy service-worker.js to public folder and it's work. thank you

copy-sw.js:

const fs = require(`fs`).promises;

Promise.all(
  [`service-worker.js`].map((fileName) =>
      fs.copyFile(`./.next/static/${fileName}`, `./public/${fileName}`).then(() => fileName),
  ),
).then((fileNames) =>
  console.log(`The following files were copied to './static': ${JSON.stringify(fileNames)}`),
);

add this script to package.json

"copy-sw": "node copy-sw.js",
"build": "next build && yarn copy-sw",

Cool. That works! Closing this issue as it's already resolved.