Skip to content
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

How to use service_worker_register when using Vite workbox integration #1479

Open
esomore opened this issue Oct 29, 2024 · 2 comments
Open

Comments

@esomore
Copy link

esomore commented Oct 29, 2024

I am currently unable to use the library with service worker functionality because it is breaking my service worker update prompts (https://github.com/vite-pwa/vite-plugin-pwa/blob/main/docs/frameworks/react.md#prompt-for-update). I think using service_worker_register might be the solution. Still, I'm unsure how to implement it.

This is my current working SW.tsx:

import Button from '@mui/material/Button';
import { useCallback, useEffect, useRef } from 'react';
import { useRegisterSW } from 'virtual:pwa-register/react';
import type { SnackbarKey } from 'notistack';
import useNotifications from '@/store/notifications';

function SW() {
  const [, notificationsActions] = useNotifications();
  const notificationKey = useRef<SnackbarKey | null>(null);
  const {
    offlineReady: [offlineReady, setOfflineReady],
    needRefresh: [needRefresh, setNeedRefresh],
    updateServiceWorker,
  } = useRegisterSW();

  const close = useCallback(() => {
    setOfflineReady(false);
    setNeedRefresh(false);

    if (notificationKey.current !== null && notificationKey.current !== undefined) {
      notificationsActions.close(notificationKey.current);
    }
  }, [setOfflineReady, setNeedRefresh, notificationsActions]);

  useEffect(() => {
    if (offlineReady) {
      notificationsActions.push({
        options: {
          autoHideDuration: 4500,
          content: <Alert severity="success">App is ready to work offline.</Alert>,
        },
      });
    } else if (needRefresh) {
      notificationKey.current = notificationsActions.push({
        message: 'New content is available, click on reload button to update.',
        options: {
          variant: 'warning',
          persist: true,
          action: (
            <>
              <Button onClick={async () => updateServiceWorker()}>Reload</Button>
              <Button onClick={close}>Close</Button>
            </>
          ),
        },
      });
    }
  }, [close, needRefresh, offlineReady, notificationsActions, updateServiceWorker]);

  return null;
}

export default SW;
@rjhoffmann
Copy link

@esomore, did you get any resolution to this, I'm currently running into the same issue.

@esomore
Copy link
Author

esomore commented Dec 13, 2024

@esomore, did you get any resolution to this, I'm currently running into the same issue.

Not yet :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants