next-pwa: no matching service worker detected. you may need to reload the page or check that the scope of the service worker for the current page encloses the scope and start url from the manifest
Hello, I’ve been trying to setup next-pwa for the past two days but with no luck at all. Here’s my next config:
require('dotenv').config();
const path = require('path');
const Dotenv = require('dotenv-webpack');
const withPWA = require('next-pwa');
const withPlugins = require('next-compose-plugins');
const nextConfig = {
webpack: (config, { isServer }) => {
// Fixes npm packages that depend on `fs` module
if (!isServer) {
config.node = {
fs: 'empty',
};
}
// Read the .env file
config.plugins = [
...config.plugins,
new Dotenv({
path: path.join(__dirname, process.env.ENV_FILE_PATH || '.env'),
systemvars: true,
}),
];
return config;
},
};
module.exports = withPlugins(
[
[
withPWA,
{
pwa: {
dest: 'public',
sw: 'service-worker.js',
maximumFileSizeToCacheInBytes: 3000000,
},
},
],
],
nextConfig
);
Next.js v9.2.1
server.js
even though it's not required since v9 as mentioned in the docs, but I tried anyways
if (
pathname === '/service-worker.js' ||
pathname.startsWith('/workbox-')
) {
const filePath = join(__dirname, '.next', pathname);
app.serveStatic(req, res, filePath);
}
/public/manifest.json
{
"name": "hello",
"short_name": "hello",
"theme_color": "#00a699",
"background_color": "#ffffff",
"display": "standalone",
"orientation": "portrait",
"scope": "/",
"start_url": "/",
"icons": [
{
"src": "images/icons/icon-72x72.png",
"sizes": "72x72",
"type": "image/png"
},
{
"src": "images/icons/icon-96x96.png",
"sizes": "96x96",
"type": "image/png"
},
{
"src": "images/icons/icon-128x128.png",
"sizes": "128x128",
"type": "image/png"
},
{
"src": "images/icons/icon-144x144.png",
"sizes": "144x144",
"type": "image/png"
},
{
"src": "images/icons/icon-152x152.png",
"sizes": "152x152",
"type": "image/png"
},
{
"src": "images/icons/icon-192x192.png",
"sizes": "192x192",
"type": "image/png"
},
{
"src": "images/icons/icon-384x384.png",
"sizes": "384x384",
"type": "image/png"
},
{
"src": "images/icons/icon-512x512.png",
"sizes": "512x512",
"type": "image/png"
}
],
"splash_pages": null
}
Any help is highly appreciated.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 30 (10 by maintainers)
@LucasMallmann Sure thing, here you go(Although I shouldn’t do that):
I had the same error message, “No matching service worker detected…” and I came to this topic, but I’m not using AMP and the middleware was causing the issue and this solution solved my me.
Basically, I added the
runtimeCachingandbuildExcludesto the PWA config inside thenext.config.js.I think you can set the scope for non-AMP service worker to
/and change rules inruntimeCachingto exclude the AMP routes. That way it will not do anything on AMP pages.@shadowwalker Hello, I just wanted to let you and any future developers who come across this issue know how I solved it. Since our website is half amp, half SSR, what I’ve done was creating two separate service workers, one for amp controlling amp pages only and one for the other pages.
AMP docs on how to install/register amp service worker: https://amp.dev/documentation/examples/components/amp-install-serviceworker/ working example provided by Nextjs: https://github.com/vercel/next.js/tree/canary/examples/amp-first
and this is the server endpoints as suggested by you guys:
and server endpoint for amp service worker:
next-pwa config:
As you can see, for the non-amp service worker I had to set the scope to a single page only which is the search page. Any ways I could make it handle all other pages that the amp service worker doesn’t handle? (amp controlls / and /properties)
@omar-dulaimi I understand it’s frustrating to debug those issue. I would assume AMP won’t work well with PWA, (will have to experiment this a bit later). Both technologies are trying to speed up page loads, but they are running in totally different direction. AMP pages will be cached on Google Server and will be served by Google to Visitors once cached. I assume for security reasons, those pages won’t work well with service worker. My suggestions here
Hey @shadowwalker, I just tried that, and it still only works on Non AMP pages.
Hey @LucasMallmann I moved both handlers above, right below
server.use(csrf({ cookie: true }));But it still didn’t work.