Skip to content

Support existing service worker? #1034

Answered by kettanaito
crutch12 asked this question in Q&A
Discussion options

You must be logged in to vote

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:

// public/custom.sw.js
importScript('./mockServiceWorker')

This example implies you've got the ./public/mockServiceWorker.js file generated via npx msw init CLI.

By using importScript, the browser automatically imports and executes the referenced worker. This includes registering event listeners like fetch and message, so you don't have to manually manage…

Replies: 3 comments 7 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by crutch12
Comment options

You must be logged in to vote
5 replies
@kettanaito
Comment options

@crutch12
Comment options

@arekucr
Comment options

@crutch12
Comment options

@kettanaito
Comment options

Comment options

You must be logged in to vote
2 replies
@kettanaito
Comment options

@bmarti44
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1033 on December 17, 2021 11:42.