hanford / next-offline

make your Next.js application work offline using service workers via Google's workbox

Home Page:https://github.com/hanford/next-offline

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Next-offline works localhost but not when deployed

matepaiva opened this issue · comments

Hi, all. First of all, thank you for your hard work. I am dealing with a very weird behavior with next-offline: the SW is never registered when deploying in the server.

If I do the same steps (next build and next start) on localhost, it works and the SW is registered with success.

I already checked the process.env.NODE_ENV in the server and it's set as production. The only different thing is that in the server I am using pm2, while localhost I just type the npm scripts.

Any idea that could help me? I am not using now.

"next-offline": "^4.0.6",
"next": "^9.3.1",

Note: I am using next-offline@4.0.6 because when I tried to upgrade to @5 I got this error:
Error: Cannot find module 'webpack/lib/node/NodeTemplatePlugin'

next.config.js:

const path = require('path');
const withOffline = require('next-offline');
const withImages = require('next-images');

const rootPath = path.join(__dirname, '../.env');

require('dotenv').config({ path: rootPath });

const Dotenv = require('dotenv-webpack');

const withImagesConfig = withImages({
  webpack: config => {
    config.plugins = config.plugins || [];

    config.plugins = [
      ...config.plugins,

      // Read the .env file
      new Dotenv({
        path: rootPath,
        systemvars: true,
      }),
    ];

    config.module.rules.push({
      test: /\.(eot|woff|woff2|ttf|svg|png|jpg|gif)?(\?v=[0-9]\.[0-9]\.[0-9])?$/,
    });

    return config;
  },
});

module.exports = withOffline(withImagesConfig);

Thanks a lot :)

Never mind. I couldn't figure out why, but I decided to register the service worker by myself, and that works.