workbox icon indicating copy to clipboard operation
workbox copied to clipboard

Workbox-webpack plugin doesn't work with webpack 5.42.1

Open SaravgiYash opened this issue 5 years ago • 4 comments

Welcome! Please use this template for reporting bugs or requesting features. For questions about using Workbox, the best place to ask is Stack Overflow, tagged with [workbox]: https://stackoverflow.com/questions/ask?tags=workbox

Library Affected: workbox-webpack-plugin

Browser & Platform: All browser

Issue or Feature Request Description: I am getting an error when I run make js after updating the webpack from 4 to 5 Error: https://github.com/internetarchive/openlibrary/pull/5286/checks?check_run_id=3059150118

> env NODE_ENV=production WEBPACK_PACKAGE=webpack5 npx webpack --mode=production

/home/runner/work/openlibrary/openlibrary/node_modules/webpack5/lib/util/AsyncQueue.js:308
				if (inCallback) throw err;
				                ^

TypeError: dependency.getCondition is not a function
    at ModuleGraph.setResolvedModule (/home/runner/work/openlibrary/openlibrary/node_modules/webpack5/lib/ModuleGraph.js:169:15)
    at /home/runner/work/openlibrary/openlibrary/node_modules/webpack5/lib/Compilation.js:1599:19
    at /home/runner/work/openlibrary/openlibrary/node_modules/webpack5/lib/util/AsyncQueue.js:352:5
    at Hook.eval [as callAsync] (eval at create (/home/runner/work/openlibrary/openlibrary/node_modules/webpack5/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)

Webpack update PR: https://github.com/internetarchive/openlibrary/pull/5245/files PWA PR: https://github.com/internetarchive/openlibrary/pull/5286/files

SaravgiYash avatar Jul 14 '21 11:07 SaravgiYash

Generally speaking, workbox-webpack-plugin should be compatible with webpack v5.x. I will take a closer look at what's triggering that specific exception.

Taking a step back, I see that you're using the InjectManifest plugin with exclude: [/.*/] configured, i.e. with everything excluded from the precache manifest. If you would prefer not to precache anything, then you probably should not use the InjectManifest plugin at all—the whole purpose of the plugin is to generate and inject a manifest of assets to precache.

You can use Workbox's runtime caching logic without a plugin by just writing your service worker using ES modules like you're currently doing, and then bundling the code by creating an entry in your webpack configuration that corresponds to your service worker, just like you'd bundle any other JavaScript code. As long as you use a consistent filename for the output (service-worker.js), you should just be able to register that bundled JavaScript code your service worker.

That will lead to a simpler build process and presumably avoid whatever error condition you're encountering.

jeffposnick avatar Jul 14 '21 13:07 jeffposnick

@jeffposnick Thanks for the solution, in the future, we might need to precache, but for now, it's good.

I wondered if you could guide me on how I can cache our reading log page (https://openlibrary.org/people/yashs911/books/want-to-read) without an initial visit and also keep using the Network First Strategy. I went through numerous StackOverflow questions and also tried using Warm strategy cache, but as the URL contain the dynamic username, I am unable to implement it.

Currently, we redirect the URL but it can be set to the final URL: /account/books → /people/username/books → /people/username/books/want-to-read

SaravgiYash avatar Jul 19 '21 13:07 SaravgiYash

Hello @Yashs911—I'm not exactly sure I understand your use case, but in general, if you have multiple URLs and you want to consider them equivalent from the perspective of a runtime strategy, you could accomplish that by implement a Workbox plugin.

A custom plugin implementing cacheKeyWillBeUsed, for instance, can override which URL is used when reading/writing to the cache.

I'm not 100% sure that will solve your use case, though. If you have any follow-ups, it would be best to ask on Stack Overflow or a new issue.

jeffposnick avatar Jul 21 '21 18:07 jeffposnick

I just tried with both 6.2.4 and latest and this is still happening when using InjectManifest with Webpack 5. Is not happening with GenerateSW though.

jorgekleeen avatar Aug 19 '22 04:08 jorgekleeen