next-pwa: Next v13 `app-build-manifest.json` - Does not register a service worker that controls page and start_url
Summary
I was running into “Does not register a service worker that controls page and start_url” after migrating to NextJS v13.

After some investigation, I noticed that I was running into a bad precaching runtime error due to http://localhost:3000/_next/app-build-manifest.json.

Does this have something to do with the fact that Next13 uses app directory?
Versions:
next-pwa: 5.6.0next: 13
Steps to reproduce the behavior: Migrate to NextJS v13.
Expected Behaviors: Service worker should be succesfully registered.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 38
- Comments: 26
I ended up forking this repo: https://github.com/DuCanhGH/next-pwa (it works with App Router and has built-in TypeScript, JSDoc support - all examples are also written in TypeScript).
This happens because this line doesn’t check for
app-build-manifest.jsonalongside withbuild-manifest.jsonalthough it is supposed to as there hasn’t been any activity on this repo since App Router was released.One alternative is changing your
next-pwaconfig:Edit: updated the solution based on @Schular’s comment.
For anyone that wants a working solution using only the
next-pwa(keep in mind that this repo seems like is not maintained anymore), I created a sample repository with a workaround: next.config.jsThe above repo is deployed on Vercel (app & pages dirs concomitantly) with the following pages:
This example is a simple Next.js starter project that should get ~100% on all Lighthouse categories.

Basically we add
buildExcludes: ["app-build-manifest.json"]atnext-pwaconfig, as the others are already excluded on thenext-pwaimplementation (https://github.com/shadowwalker/next-pwa/issues/424#issuecomment-1332258575) and we modify the webpack config with the custommain-appentry (https://github.com/shadowwalker/next-pwa/pull/427)Thanks @DuCanhGH, floatingdino
@Murkrage so far I’ve done these changes to the package:
appDir(app/_offline/page.tsxnow also automatically enables offline fallback for document).babeltoswc(the original package usednext/babelto build fallback/custom worker).You should have.swc-loaderand@swc/corein yourdevDependenciesshould you use any of these features (no longer the case in 8.1.0)PluginOptions.workboxOptions(this change is why the package is at its 7.x right now, and if you have// @ts-checkat the top of yournext.config.jsyou should also be informed if you pass invalid options, for example having both GenerateSW-specfic and InjectManifest-specific options).const withPWA = require("@ducanh2912/next-pwa").default(), rather thanconst withPWA = require("next-pwa")().app/~offline/page.jsto automatically enable the fallback worker.These are the ones I can pull off the top of my head 😃
In v8, I’ve also added these:
tsconfig.jsonpath aliases in custom workers. (8.0.0)subdomainPrefixandworkboxOptions.exclude. (8.0.0)extendDefaultRuntimeCaching, which allows you to extend the default runtime caching array, rather than overriding it. (8.5.0)aggressiveFrontEndNavCaching- when combined withcacheOnFrontEndNav, it will cache<link rel="stylesheet" href=""and<script src="" />on page navigation. (8.7.0)These are the changes in v9:
next/dist/build/swcfirst before@swc/coreto save disk space. (9.0.0)Thanks @DuCanhGH that works for me. I did try and exclude the
app-build-manifestbut think I was usingbuildExcludesinstead ofexclude.Temporary fix
For anyone coming here for the same reason and before this PR gets merged - https://github.com/shadowwalker/next-pwa/pull/427 - the below works for me in production with the following caveat.
This excludes the nextjs build manifest which includes all the page URLs that get generated. In my testing, the below will pre cache static files but it won’t cache any URL until you first visit the page after the service-worker has been installed on first visit.
next-pwa": "^5.6.0next.config.js
app/layout.tsx
Custom PWA Header component that runs in the client
app/Pwa.tsx
Does anyone know if there are any plans to also add this fix to this repo anytime soon?
@DuCanhGH fully agree - yeah the HeadPwa only ended up being there due to iterations to get to this point. Adding it to the
layout.tsxwill be just as good. I have updated the snippet above.By all means this is not a
fixbut it should get people by for the time being.I did try to contact @shadowwalker on Twitter to see if they were getting notifications or if there is any way I can help to get the PR merged in, but no luck yet ☹️
I was struggling to implement PWA in Next13 App directory. Thank you @DuCanhGH. Your solution worked like a charm. 😃
Had the same issue. @DuCanhGH You package solves the issue. Thank you!
@AaronLayton I don’t think his solution would work. If you want to use next-pwa, you can try adding this to your next-pwa config:
and you still have to register the SW yourself.
@Schular nice, that sounds like a great solution! I’ve updated my own comment to match this repo as well.
@DuCanhGH I see you’ve made lots of changes since you forked the repo. I’m inclined to go with your fork instead but unsure what kind of impact your changes have on the package.
@AaronLayton I’d also suggest changing your
app/Pwa.tsxa bit:I think this looks best 😃 I also think it’s kinda hard to contact the author, let alone have him accept the PR as he hasn’t been working on this project for months…
@AaronLayton I think
<HeadPwa />should be put in yourapp/layout.tsxinstead.And that PR won’t completely fix the problem, it will only make auto registration work in appDir, it won’t fix that
bad-precaching-responseissue. Not that I think the maintainers will merge it, I think this repo seems dead (despite the author saying otherwise).Also,
disable: process.env.NODE_ENV === 'development'can be changed todisable: isDev. Just saying 😃Done! Create a head.tsx in app root directory as client component, then with a use effect hook register the service worker.
Not a fix, but a behavior I found that gets PWA working briefly on apps not using
/pages/:next.config.js(breaks website)npm run build,npm run devlocalhost:3000npm run build,npm run devconsoleandapplicationstab in dev tools. Once I ranLighthouse, things stop working again.