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 whenpostMessage
to service worker. So that on the listener, it could do multiple different tasks usingif...else...
. - When you are debugging service worker, constantly
clean application cache
to 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
runtimeCaching
such asoptions.cacheableResponse.statuses=[200,302]
. - When debugging issues, you may want to format your generated
sw.js
file to figure out what's really going on. - Force
next-pwa
to generate worker box production build by specify the optionmode: 'production'
in yourpwa
section ofnext.config.js
. Thoughnext-pwa
automatically generate the worker box development build during development (by runningnext
) and worker box production build during production (by runningnext build
andnext 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 = true
in yourworker/index.js
(create one if you don't have one) (opens in a new tab). - It is common developers have to use
userAgent
string 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.