Support existing service worker? #1034
-
In the same time we are allowed to have only one service worker for one scope. How could I connect msw to existing service worker? If I use Workbox InjectManifest Plugin, for example, I already have sw.js file with my own settings So I just need to "inject" eventListeners registrations from msw in my sw.js But, as I can see, msw registers new service worker by itself. Is there any way to start created msw worker without new service worker registering? For example: // sw.js
import { setupWorker, rest } from 'msw'
const worker = setupWorker(
rest.post('/login', (req, res, ctx) => {
const { username } = req.body
return res(
ctx.json({
username,
firstName: 'John'
})
)
}),
)
// We are already in service worker
worker.startWithoutRegistration() UPD: I found out we have such cli: Which generates .js from this file: So I can just generate this file and import it from my main sw.js But These event listeners may conflict with my main service worker self.addEventListener('install', ...) // installHandler
self.addEventListener('activate', ...) // activateHandler
self.addEventListener('message', ...) // messageHandler
self.addEventListener('fetch', ...) // fetchHandler Especially Maybe we could export |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
Hey, @crutch12. Combining MSW with another service worker is the same as combining two any workers. I highly recommend using the Here's an example of how you can combine your own service worker with the MSW's one: // public/custom.sw.js
importScript('./mockServiceWorker')
By using Precaution noticeThat being said, keep in mind that MSW is a development tool primarily. You may use it in production for demoing and educational purposes but those are rather corner cases. You are unlikely to use your production workers during development so review carefully if combining workers is really what you need. |
Beta Was this translation helpful? Give feedback.
-
Hi, @kettanaito Thx for your answer! I understand that I can import this script, but I don't get that -- how should I start msw worker? worker.start({
serviceWorker: {
// Points to the custom location of the Service Worker file.
url: '/public/custom.sw.js'
}
}) And another one: |
Beta Was this translation helpful? Give feedback.
-
i've been taking a look at this - our use case is we would like to test out the service worker locally before sending it out to an environment. this would help prevent getting a rogue service worker installed on browser that would be hard to uninstall. i have a working example -> add to your webpack config
src/sw.js ->
enabling it all ->
the main problem i have is that msw must be enabled in order to use the service worker. while i want this locally so i can test everything, once we move to an environment, i want to only enable my production service worker. i'm going through the code to start msw now, but, is there a way that i could conditionally start msw from my workbox service worker? from there i could probably send a message to the workbox service worker containing my environment variable for whether or not mocks should be enabled, and include the mock service worker script. |
Beta Was this translation helpful? Give feedback.
Hey, @crutch12.
Combining MSW with another service worker is the same as combining two any workers. I highly recommend using the
importScript
function globally available in the worker's scope. It allows importing one service worker file within another.Here's an example of how you can combine your own service worker with the MSW's one:
By using
importScript
, the browser automatically imports and executes the referenced worker. This includes registering event listeners likefetch
andmessage
, so you don't have to manually manage…