I am trying to create a Next.js PWA
with WebpackPwaManifest
and NextWorkboxPlugin
.
Whenever I start the app the service worker
is registered but it can't find the manifest.json
.
Here is my next.config.js
file, it contains how I generate manifest
and serviceworker
on each build:
const webpack = require('webpack')
const path = require('path');
const NextWorkboxPlugin = require('next-workbox-webpack-plugin');
const WebpackPwaManifest = require('webpack-pwa-manifest');
const { parsed: keys } = require('dotenv').config({
path: './keys.env'
})
module.exports = {
webpack(config, { isServer, buildId, dev }) {
...
const workboxOptions = {
clientsClaim: true,
skipWaiting: true,
globPatterns: [path.resolve('.next/static/*'), path.resolve('.next/static/commons/*')],
modifyUrlPrefix: {
'.next': '_next',
},
runtimeCaching: [
{
urlPattern: '/',
handler: 'networkFirst',
options: {
cacheName: 'html-cache',
},
},
...
],
};
config.plugins.push(
new NextWorkboxPlugin({
buildId,
...workboxOptions,
}),
new WebpackPwaManifest({
name: 'appname',
short_name: 'shortname',
description: 'desc',
background_color: '#ffffff',
crossorigin: 'use-credentials',
inject: true,
start_url: "/",
icons: [
{
src: path.resolve('./public/images/Iconlogo.svg'),
sizes: [96, 128, 192, 256, 384, 512],
},
{
src: path.resolve('./public/images/Iconlogo.svg'),
size: '1024x1024'
},
{
src: path.resolve('./public/images/Iconlogo.svg'),
size: '1024x1024',
purpose: 'maskable'
}
]
}));
return config
},
...
}
So this is working as expected... generated the SW and the manifest as well.
But when I run the app, I get this error:
GET http://localhost:3000/manifest.json 404 (Not Found)
manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.
I am including manifest in _document.tsx, using next.js Head
tags:
export default class MyDocument extends Document {
render() {
return (
<Html lang="en">
<Head>
<link rel="manifest" crossOrigin="use-credentials" href="/manifest.json" />
</Head>
< body >
<Main />
<NextScript />
</body>
</Html>
)
}
}
Any idea? Thanks in advance.