Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

SW registration failed #127

Closed
MatteoGioioso opened this issue Apr 7, 2019 · 20 comments
Closed

SW registration failed #127

MatteoGioioso opened this issue Apr 7, 2019 · 20 comments
Labels
bug Something isn't working

Comments

@MatteoGioioso
Copy link

MatteoGioioso commented Apr 7, 2019

Bug report 馃悶

Hello there, I am trying to use this library with my custom service-worker file, but unfortunately is not working for my use case.

I am receiving this error:

SW registration failed:  TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script.

I am using Next.js inside a Docker container

Docker file:

FROM node

WORKDIR /usr/src/app

COPY . .

RUN npm install pm2 -g

RUN npm install

RUN npm run build

CMD ["pm2-runtime", "start", "npm", "--", "start"]

Docker compose

#Test service workers configuration
version: '3'

services:
  frontend:
    build: frontend/
    ports:
      - 3000:3000

Next.js config

const config = {
  // Service Worker
  generateSw: false,
  workboxOpts: {
    swDest: "./service-worker.js", // <-- I have tried different path (static/, server/)
    swSrc: path.join(__dirname, "./utils/serviceWorker/sw.js"),
    globPatterns: [
      "**\/*.{ico,html,js,json}"
    ],
    globDirectory: './_next/static',
  },
}
"next": "8.0.3",
"next-offline": "3.3.7",
"react": "16.8.1",
"react-dom": "16.8.1"

After the build process I can see my service worker file appear correctly in the .next folder.

Looks like the custom service worker use case is not common, so if there is something wrong in my config, please forgive me 馃檹 .

This is the repo where you can reproduce the error (In this case I have not use Docker): https://github.com/MatteoGioioso/next-offline-sw-error-2

Thanks

@MatteoGioioso MatteoGioioso changed the title SW registration failed: TypeError: Failed to register a ServiceWorker: A bad HTTP response code (404) was received when fetching the script. SW registration failed Apr 7, 2019
@hanford
Copy link
Owner

hanford commented Apr 8, 2019

Thanks providing a repo where this consistently happens. I'm pretty busy this week but will report back once I have some time to dig in

@straxico
Copy link

i have similar problem

@hanford hanford added the bug Something isn't working label Apr 22, 2019
@MatteoGioioso
Copy link
Author

@hanford hello any update? If you have something in mind, with some guidance I can help to fix it.

@srosset81
Copy link

I'm running into the same issue. @MatteoGioioso I would be glad to hear how you solved it !

@hanford
Copy link
Owner

hanford commented Apr 29, 2019

@srosset81 @MatteoGioioso @straxico

After looking, you need to either have a custom next.js server or a now.json to handle the service worker request.

next-offline needs something to handle the request, as Next.js won't handle it by default.

The Now 1.0 example is probably easiest for existing Next.js applications, I use it in several other personal projects.

The only real changes are the server.js and package.json scripts but this code should basically be a drop in fix for you: https://github.com/hanford/next-offline/tree/master/examples/now1

@hanford
Copy link
Owner

hanford commented Apr 29, 2019

I've fixed the issue in the repro case you shared over here: https://github.com/hanford/next-offline-issue-127

Going to go ahead and close issue. Feel free to drop another comment if you have any questions

@GusRuss89
Copy link

I'm a bit confused by this. If the service worker is generated at compile time, why do I need a custom server to serve it? Can't I just point to the pre-generated service-worker.js as a static file?

@hanford
Copy link
Owner

hanford commented May 6, 2019

@GusRuss89 service workers aren't treated as ordinary JS files.

w3c/ServiceWorker#940

If you're using Now 2.0, you don't need a standalone server, just a now.json. You can see how that works by looking at my personal website

@MatteoGioioso
Copy link
Author

@hanford Hei, sorry, I forgot to say thanks for the help!

@softwarenacho
Copy link

https://github.com/hanford/next-offline-issue-127

The link is down and I have this same issue.

@hanford
Copy link
Owner

hanford commented Jul 4, 2019

@softwarenacho please open a new issue

@MatteoGioioso
Copy link
Author

@hanford
Copy link
Owner

hanford commented Jul 6, 2019

thanks @MatteoGioioso !

@softwarenacho
Copy link

@softwarenacho I have forked it
https://github.com/MatteoGioioso/next-offline-issue-127

Thanks @MatteoGioioso , I managed to solve it following one off the examples @hanford had published at:
https://github.com/hanford/next-offline/blob/master/examples/now2/now.json

I was missing including the service worker in the now configuration.

@ahmadawais
Copy link

ahmadawais commented Jul 16, 2019

For folks who are using workspaces and have the Next.js site in a dir called www make sure you use the following now.json setting.

{
"routes": [
		{
			"src": "/service-worker.js",
			"dest": "/www/_next/static/service-worker.js",
			"headers": {
				"cache-control": "public, max-age=43200, immutable",
				"Service-Worker-Allowed": "/"
			}
		},
]
}

@davisbartce
Copy link

@hanford I have the same problem but in a pm2 server in nginx
image

but in now work perfect any idea ??

@hanford
Copy link
Owner

hanford commented Aug 23, 2019

@davisbartce No idea! maybe you need to set something up manually in nginx?

@davisbartce
Copy link

@hanford the nginx configuration is ok, the site work but the service worker is not found,,. maybe you now where is the directory to generate this file?? y wrote the detail in this isue #141

@Toyurc
Copy link

Toyurc commented Sep 10, 2020

Hi @hanford, this error still occurs till date. I read through this thread and even checked out your website, It is also broken on there, check the console and you would see SW registration failed: TypeError: Failed to register a ServiceWorker for scope ('https://jackhanford.com/') with script ('https://jackhanford.com/service-worker.js'): A bad HTTP response code (404) was received when fetching the script.

@nikhilmeria
Copy link

nikhilmeria commented May 7, 2021

`const withPWA = require('next-pwa');

module.exports = withPWA({
pwa: {
dest: 'public', // this is very imp
disable: process.env.NODE_ENV === 'development',
register: true,
scope: '/',
sw: 'service-worker.js',
},
});`

add this line in next.config.js -> dest: 'public',

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

10 participants