Service worker loaded by PWA causing constant reload
I have a service worker loaded by the @vite-pwa/nuxt plugin config in nuxt.config.ts which handles push notifications as well as offline install.
This all works initially, but once loaded if I reset notification sin the browser this sometimes cause some issue where the site constantly reloads.
Initially it says
But then when resetting notifications, in the console it says this and triggers a constant reload situation
Eventually it does seem to resolve itself (stop reloading) but is there a way I can stop it having to refresh so much - if the file is already cached, proceed rather than reload
The config is
22 Replies
Could it be because part of the service worker is doing this? https://web.dev/articles/offline-fallback-page and the other part is managing push notifications
web.dev
Create an offline fallback page | Articles | web.dev
Learn how to create a simple offline experience for your app.
just remove that import script, the offline page will be the
navigateFallback
, just prerender it or use a custom page (don't forget to prerender it)how will the service worker be loaded in a PWA then?
above issue seems to have resolved itself, but SW doesn't seem to be working on live deployment
it appears its hloaded though
your sw loading itself via importScript
check sw.js in source tab
you need to prerender / route or add a custom page and prerender it
you can use empty / route with a middleware to redirect to login when required, otherwise redirect to the "home" page
check elk.zone github repo
but arent you suggesting to remove the importScript line?
also the SW is handling push notifications not just offline mode
use custom sw
use what now?
its possible the SW is loading fine but itsn't delivering push notifications for another reason
use
injectManifest
strategy and configure your options via workbox modules(i appreciate my issue here has changed since yesterday)
check service-worker folder in elk.zone, exclude that folder from nuxt ts, check
typescript
entry in nuxt config file and the typecheck script in package.json (if you are running some typechecking in your project)
the configuration can be found in config/pwa.ts
module and modules/pwa/i18n.ts
(if you don't need i18n/theme-color in the webmanifest just use pwa option in your nuxt config file)
importScripts
should be used only in the service worker file, no idea if using in generateSW
works (via workbox-build node module): it seems workbox-build doing weird things with your sw@Joaquín inject manifest should look like this?
mentioned in this which seems appropriate to the packages im using
https://stackoverflow.com/questions/76190074/how-to-add-custom-service-worker-to-nuxt-3-with-vite-pwa
Stack Overflow
How to add custom service worker to Nuxt 3 with vite-pwa?
I am trying to glue together a Nuxt 3 PWA with vite-plugin-pwa, more specifically @vite-pwa/nuxt. I installed the module and set up basic configuration (I basically followed this video)
What I want...
yes, add also srcDir and filename: https://github.com/elk-zone/elk/blob/main/config/pwa.ts#L9-L11
move workbox options to injectManifest entry, you should remove some entries like cleanupOutdated and client claims
use https://github.com/elk-zone/elk/blob/main/service-worker/sw.ts#L3 for cleanupOutdated
for claims add this logic
this is where the config is at at the moment
import { clientsClaim } from 'workbox-core'
add both lines at the end, before adding push notification listener
change workbox:
with injectManifest:
or remove the entry
navigateFallback should be configured in the sw
you can use the workbox recipe if you wantweb.dev
Create an offline fallback page | Articles | web.dev
Learn how to create a simple offline experience for your app.
Chrome for Developers
workbox-recipes | Modules | Chrome for Developers
Easily use common workbox patterns without needing to set them up yourself from individual packages.
there is a recipe for offline
you can use recipes or just workbox modules
the recipes will use modules
don't use
maskable any
in pwa icons' purpose
if you've a favicon.svg or logo.svg check pwa assets generator to generate the pwa icons: https://vite-pwa-org.netlify.app/assets-generator/cli.html
https://vite-pwa-org.netlify.app/assets-generator/#preset-minimal-2023
or you can generate pwa icons on the fly with just a single icon: https://vite-pwa-org.netlify.app/assets-generator/integrations.html and https://vite-pwa-org.netlify.app/frameworks/nuxt.html#pwa-assets
here an example: https://github.com/vite-pwa/nuxt/tree/main/playground-assets
check also the Elk docs: https://docs.elk.zone/pwa
there are some hints to test push notifications in dev onn real device
(android and chromium based browsers only)Ok theres a lot there to pick through (lots that I don't necessarily understand), but it appears to now be working.
I think a combination of following this https://stackoverflow.com/questions/76190074/how-to-add-custom-service-worker-to-nuxt-3-with-vite-pwa and removing the importScripts line (possibly it was double loading and causing some sort of conflict.
Thanks so much for helping me work through the problem and I'll pick through the feedback above too
Stack Overflow
How to add custom service worker to Nuxt 3 with vite-pwa?
I am trying to glue together a Nuxt 3 PWA with vite-plugin-pwa, more specifically @vite-pwa/nuxt. I installed the module and set up basic configuration (I basically followed this video)
What I want...