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

vite-plugin-pwa removes mock service worker #693

Open
houtan-rocky opened this issue Mar 30, 2024 · 5 comments
Open

vite-plugin-pwa removes mock service worker #693

houtan-rocky opened this issue Mar 30, 2024 · 5 comments

Comments

@houtan-rocky
Copy link

houtan-rocky commented Mar 30, 2024

Issue: when VitePWA is activated, it conflicts with my MSWJS mock service worker and apparently removes Mock Service Worker from my application.

steps to reproduce:
Install React-App through vite installation

    "vite": "^5.0.7",
"vite-plugin-pwa": "^0.19.7",

install and configure vite-plugin-pwa:

    VitePWA({
      registerType: 'autoUpdate',
      devOptions: {
        enabled: true,
      },
      injectRegister: 'auto',
      manifest: {
        name: 'test-app',
        short_name: 'TA',
        start_url: '',
        display: 'fullscreen',
        theme_color: '#000',
        background_color: '#fff',
        icons: [
          {
            src: 'favicon/android-chrome-192x192.png',
            sizes: '192x192',
            type: 'image/png',
          },
          {
            src: 'favicon/android-chrome-512x512.png',
            sizes: '512x512',
            type: 'image/png',
          },
        ],
      },
    }),

Behaviour:
the vite-pwa extension, when enabled, removes the mock service worker, and the below error is shown in the console:

createStartHandler.ts:57 Uncaught (in promise) Error: [MSW] Failed to locate the Service Worker registration using a custom "findWorker" predicate.

Please ensure that the custom predicate properly locates the Service Worker registration at "/mockServiceWorker.js".
More details: https://mswjs.io/docs/api/setup-worker/start#findworker

    at startWorkerInstance (createStartHandler.ts:57:15)
    at async SetupWorkerApi.start (setupWorker.ts:179:12)

Expected:
Extension to not conflict and remove the mock service worker

@houtan-rocky houtan-rocky changed the title Removes mock service worker vite-plugin-pwa removes mock service worker Mar 30, 2024
@userquin
Copy link
Member

you can try adding an env var when testing, then use it to disable PWA.

@houtan-rocky
Copy link
Author

houtan-rocky commented Mar 30, 2024

@userquin Isn't there a way to not unregister other SW's?
If you can show me the direction I might be able to help with that.

@userquin
Copy link
Member

using generateSW I guess no, can you provide a minimal reproduction? Maybe I can check what's happening...

@userquin
Copy link
Member

userquin commented Mar 30, 2024

If you use a custom service worker you can import mockServiceWorker.js script, but since you use generateSW strategy there is no way to include that script (iirc you can only have one sw per scope).

@userquin
Copy link
Member

check registering the msw sw in another scope: check last entry here https://stackoverflow.com/questions/77292614/vite-pwa-not-able-to-register-mock-service-worker-in-parallel-with-other-servi

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