Perform Intercepted Requests As-Is in MSW

Share this video with your friends

Social Share Links

Send Tweet
Published 7 months ago
Updated 7 months ago

Mocking responses is great but what if you want to observe requests without affecting them? In this lesson, we will learn how to instruct Mock Service Worker to perform intercepted requests as-is.

Lecturer: [0:04] With MSW, returning mock responses from response resolvers is not mandatory. In fact, you can just observe the network traffic without affecting it in any way. All you have to do is be explicit about it. To do that, import the passThrough function from MSW. Create a request handler for the request you wish to observe or use any existing request handlers.

[0:20] As an example, let's modify the movie recommendations handler we already have. If we return the passThrough function call from this response resolver, MSW will perform this intercepted request as is. This means that the client will receive the original data.

[0:35] In our case, the data returned from the recommendationsLoader function in Remix. We can make great use of this pattern if we pass through requests conditionally. Let's return to the movie recommendations handler and pass-through this request only for a particular movie ID.

[0:57] This way, we can alternate between the mock data and the original data based on the requested resource.

egghead
egghead
~ 17 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today