-
-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Switch to Workbox's InjectManifest plugin #9141
Comments
I like this approach, and particularly agree with renaming the file. From what I understand, you say that it should be compatible with existing projects - or did I misunderstand, and existing CRA users may need to make changes? If so, can we automate that (easily)? |
Well, in the proposal, My point about compatibility was that I think moving forward, this setup would mean that local customizations to the service worker source file would be decoupled from the |
Yes, I understand - I think we could automate that rename as a part as an upgrade, in which case I'd be on board. |
Great! I could put together a PR for this if there's buy-in. Is there an upcoming major semver release of |
Actually, it's due very soon I think - @ianschmitz was working on a few last pieces. CC @iansu. |
The existing service worker file is already managed by the dev. I'm wondering if this could just be part of release notes to copy from the template if they want to use? Would be much simpler I imagine. |
Okay—I've filed a PR with the changes discussed above. Based on past experience, I do want to make sure that this isn't something "rushed" out there to meet to a major release deadline, and I hope the community gets to try out this approach in some prereleases. I obviously trust the judgment of the maintainers 😄 |
I think this is the only san way to bring full service worker functionality into CRA, since SW is basically a separate 'daemon' running side by side with the front end part, and it needs webpack support for complicated logic. Currently, I am ejecting the project then change the webpack config via ImportScripts. This kind of work for simple logic but if I need to pull in other supporting libraries(say I need encryption from aes-js), it becomes complicated as my hand coded script is not part of the build process of webpack. |
Is your proposal related to a problem?
Many want more control over their service worker. The current
c-r-a
setup uses Workbox'sGenerateSW
mode, which uses a declarativewebpack
configuration to generate the final service worker. Changing the behavior of the service worker therefore requires updating thewebpack
config, which is not allowed inc-r-a
.Describe the solution you'd like
In Workbox v5, the
InjectManifest
mode will take an "source" service worker file (either in JavaScript or TypeScript) and run it through awebpack
child compilation, which allows you to write your service worker source file just like any other modern JavaScript code, including using ES module imports for the Workbox runtime.The
InjectManifest
plugin also takes care of reading the list of assets generated by thewebpack
compilation and injecting a precache manifest into the service worker file that will precache those assets.I'd like to switch from
GenerateSW
toInjectManifest
in thec-r-a
webpack
configuration, and create a newpackages/cra-template/template/service-worker.js
(and TypeScript equivalent) that roughly contained:This would lead to a service worker that, by default, behaved equivalently to what the previous
GenerateSW
config created. The benefits are that any part of thatservice-worker.js
could be edited by the end developer, leading to, e.g., custom service worker runtime caching.I would also like to rename the current
packages/cra-template/template/serviceWorker.js
file topackages/cra-template/template/serviceWorkerRegistration.js
to make it clearer that the file is used in thewindow
context to perform registration, and that it does not include the code for the service worker itself.In general, giving developers control over their source service worker in this manner should be relatively decoupled from the underlying
webpack
config. Even if developers made significant changes to their source service worker, by adding in new Workbox modules or other third-party code, the samewebpack
configuration should still be compatible. The one requirement is that the developer would need to keep the stringself.__WB_MANIFEST
somewhere in their source service worker file, asInjectManifest
checks for this and will fail if it's not present. (It could be inside of a comment like, e.g./* self.__WB_MANIFEST */
as a workaround for developers who don't want to use precaching.)Describe alternatives you've considered
#8822 is an alternative that will just update to Workbox v5.1.2 and keep using the
GenerateSW
mode, which is okay, but I think developers would be happier if they had the ability to control their service worker. Workbox v5 allows that to be done cleanly in a way that wasn't possible with prior releases.Additional context
See #7966 for a similar proposal.
The difference between the two mode is described in more detail at https://developers.google.com/web/tools/workbox/modules/workbox-webpack-plugin#which_plugin_to_use
The text was updated successfully, but these errors were encountered: