Tips
- Common UX pattern to ask user to reload when new service worker is installed (opens in a new tab)
- Use a convention like
{command: 'doSomething', message: ''}object whenpostMessageto service worker. So that on the listener, it could do multiple different tasks usingif...else.... - When you are debugging service worker, constantly
clean application cacheto reduce some flaky errors. - If you are redirecting the user to another route, please note workbox by default only cache response with 200 HTTP status (opens in a new tab), if you really want to cache redirected page for the route, you can specify it in
runtimeCachingsuch asoptions.cacheableResponse.statuses=[200,302]. - When debugging issues, you may want to format your generated
sw.jsfile to figure out what's really going on. - Force
next-pwato generate worker box production build by specify the optionmode: 'production'in yourpwasection ofnext.config.js. Thoughnext-pwaautomatically generate the worker box development build during development (by runningnext) and worker box production build during production (by runningnext buildandnext start). You may still want to force it to production build even during development of your web app for following reason:- Reduce logging noise due to production build doesn't include logging.
- Improve performance a bit due to production build is optimized and minified.
- If you just want to disable worker box logging while keeping development build during development, simply put
self.__WB_DISABLE_DEV_LOGS = truein yourworker/index.js(create one if you don't have one) (opens in a new tab). - It is common developers have to use
userAgentstring to determine if users are using Safari/iOS/MacOS or some other platform, ua-parser-js (opens in a new tab) library is a good friend for that purpose.