next-pwa icon indicating copy to clipboard operation
next-pwa copied to clipboard

next-pwa not working in Production Envrionment

Open dhavalveera opened this issue 2 years ago • 18 comments

Hello,

My Website => I am unable to use PWA on Production Environment on both Desktop & Mobile.

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

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: 'public',
		sw: 'service-worker.js'
	}
});

here is my next.config.js code, but it is not working on Production Environment, whereas it is working on Development Environment.

also, after upgrading the npm dependencies packages it's not working on the Production Environment but working perfectly fine on Development Environment.

please help me @shadowwalker

dhavalveera avatar Nov 13 '21 07:11 dhavalveera

This seems to be a bug (maybe) with nextjs https://github.com/vercel/next.js/issues/30692

twlite avatar Nov 16 '21 17:11 twlite

I don't think that it's a bug with nextjs, because if I switch to an older version - the first release of next-pwa, then it's working perfectly fine.

dhavalveera avatar Nov 17 '21 02:11 dhavalveera

@dhavalveera yesterday I went through different solutions I found and somehow PWA started to work after updating my config to

const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: "public",
		register: true,
		skipWaiting: true,
		runtimeCaching,
		buildExcludes: [/middleware-manifest.json$/]
	}
});

package versions:

"next": "^12.0.4",
"next-pwa": "^5.4.0"

before updating the config, I could not get PWA to work on prod

I dont really know if this helps you though.

twlite avatar Nov 17 '21 06:11 twlite

@dhavalveera yesterday I went through different solutions I found and somehow PWA started to work after updating my config to

const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: "public",
		register: true,
		skipWaiting: true,
		runtimeCaching,
		buildExcludes: [/middleware-manifest.json$/]
	}
});

package versions:

"next": "^12.0.4",
"next-pwa": "^5.4.0"

before updating the config, I could not get PWA to work on prod

I dont really know if this helps you though.

Discussion's been on going on #288 and so far this is the proposed solution, but people seem to have issues using _middleware.js and a custom error page after this.

SimHoZebs avatar Nov 17 '21 22:11 SimHoZebs

@dhavalveera yesterday I went through different solutions I found and somehow PWA started to work after updating my config to

const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: "public",
		register: true,
		skipWaiting: true,
		runtimeCaching,
		buildExcludes: [/middleware-manifest.json$/]
	}
});

package versions:

"next": "^12.0.4",
"next-pwa": "^5.4.0"

before updating the config, I could not get PWA to work on prod

I dont really know if this helps you though.

Worked for me! image

gitnasr avatar Nov 17 '21 23:11 gitnasr

Workbox needs to be updated to 6.0

patgpt avatar Nov 22 '21 00:11 patgpt

Workbox needs to be updated to 6.0

how to update Workbox in the Next.js app?

dhavalveera avatar Nov 22 '21 07:11 dhavalveera

@dhavalveera yesterday I went through different solutions I found and somehow PWA started to work after updating my config to

const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: "public",
		register: true,
		skipWaiting: true,
		runtimeCaching,
		buildExcludes: [/middleware-manifest.json$/]
	}
});

package versions:

"next": "^12.0.4",
"next-pwa": "^5.4.0"

before updating the config, I could not get PWA to work on prod

I dont really know if this helps you though.

This worked for me! Thanks.

tordsta avatar Nov 22 '21 10:11 tordsta

Workbox needs to be updated to 6.0

how to update Workbox in the Next.js app?

The template use using an old version of the withPWA plugin so I updated it to the latest in the package.json

patgpt avatar Nov 22 '21 14:11 patgpt

Hi! I'm having the same problem. In the local environment everything works fine. In production, deployed with Cloudflare Pages, it doesn't work because the service worker doesn't get loaded. I'm using the latest version of next-pwa (5.4.1). I also check the package-lock.json file, and everywhere the version of workbox is 6.4.1 (Including packages like workbox-background-sync)

next.config.js

const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

module.exports = withPWA({
  pwa: {
    dest: "public",
    runtimeCaching,
  }
});

When running next build && next export, the files sw.js and workbox-XXXXXXXX.js appear in the out director.

Error message

image Looking in the out/_next directory, I cannot find a file called middleware-manifest.json (I've checked every dir). There is a file called DcKWOchXOD8AY8wgjWFqP/_middlewareManifest.js, with this content.

self.__MIDDLEWARE_MANIFEST=[];self.__MIDDLEWARE_MANIFEST_CB&&self.__MIDDLEWARE_MANIFEST_CB()

Does that mean that workbox is somehow is trying to load a file that it isn't supposed to?

Paultje52 avatar Nov 30 '21 14:11 Paultje52

this working for me too! Thanks

TheaRong avatar Dec 03 '21 03:12 TheaRong

This is not working for me. I confirmed the precaching error goes away but I cannot login to the app. Removing withPWA solves the problem.

This was not an issue before next 12 upgrade.

const withPWA = require("next-pwa");

module.exports = (phase, { defaultConfig }) => {
  return withPWA({
    pwa: {
      dest: "public",
      register: true,
      skipWaiting: true,
      buildExcludes: [/middleware-manifest\.json$/],
      disable: process.env.NODE_ENV === "development",
    },
    images: {
      loader: "imgix",
      path: "/",
    },
    swcMinify: true,
  });
};

I'm using amplify.js to handle login via cognito and when I select sign-in the serviceworker loads a static chunk but the spinner just goes indefinitely... Not sure how to further debug this.

Edit: this works if I remove

    swcMinify: true,

ahurlburt avatar Jan 20 '22 17:01 ahurlburt

I too am having trouble running next-pwa in a Production environment. Many of the key requests are returning 404s after refreshing the initial page (supposedly when the service worker kicks in).

Screen Shot 2022-01-20 at 8 10 24 PM Screen Shot 2022-01-20 at 8 09 59 PM

As you can see, some requests are being served from the service worker just fine, but most are not.

Here are the details:

node version:: v16.5.0 next version: v12.0.8 next-pwa version: v5.4.0

next.config.js

const path = require('path');
const withPWA = require('next-pwa');
const runtimeCaching = require('next-pwa/cache');

/** @type {import('next').NextConfig} */
const nextConfig = withPWA({
  reactStrictMode: true,
  pwa: {
    dest: 'public',
    sw: 'service-worker.js',
    register: true,
    disable: process.env.NODE_ENV === 'development',
    skipWaiting: true,
    runtimeCaching,
    buildExcludes: [
      /middleware-manifest.json$/
    ]
  }
});

module.exports = nextConfig;

It seems like this is related to the OP's issue; does anyone have any insight?

caleb531 avatar Jan 21 '22 04:01 caleb531

Figured out the issue; turns out I had an old index.html in my public folder (from before I migrated my project to NextJS), and the next-pwa service worker was trying to cache that and the assets it references. As soon as I removed that index.html and rebuilt the project with npx next build && npx next start, the service worker started working beautifully.

caleb531 avatar Jan 21 '22 16:01 caleb531

@dhavalveera yesterday I went through different solutions I found and somehow PWA started to work after updating my config to

const withPWA = require("next-pwa");
const runtimeCaching = require("next-pwa/cache");

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: "public",
		register: true,
		skipWaiting: true,
		runtimeCaching,
		buildExcludes: [/middleware-manifest.json$/]
	}
});

package versions:

"next": "^12.0.4",
"next-pwa": "^5.4.0"

before updating the config, I could not get PWA to work on prod

I dont really know if this helps you though.

Worked for me. Thanks.

robertseghedi avatar Apr 23 '22 12:04 robertseghedi

    "next": "latest",
    "next-pwa": "5.5.4",

in next.config.js

const withPWA = require('next-pwa');
const withPlugins = require('next-compose-plugins');

module.exports = withPWA({
	reactStrictMode: true,
	pwa: {
		dest: 'public',
                runtimeCaching,
                disable: process.env.NODE_ENV === 'development',
	}
});

komailf67 avatar Aug 22 '22 11:08 komailf67

next-pwa/cache

Hi I am still working on this issue, not able to resolve it yet, can you help is some way ?

aashishnutanix avatar Nov 05 '22 19:11 aashishnutanix

can anyone help me?? i am facing this problem

ahadalichowdhury avatar Jul 30 '23 15:07 ahadalichowdhury