Skip to content

Use @mswjs/interceptors for transparently cache responses #1546

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

You must be logged in to vote

Hey, @c3n21. That's a great question.

What you describing should be possible to do with MSW. If you're in the browser, you can use the setupWorker() API to intercept any outgoing requests, regardless if they are issued by your code or by a third-party that loads web components.

import { setupWorker, rest } from 'msw'

const worker = setupWorker(
  rest.get('/third-party-call-url', (req, res, ctx) => {
    // Lookup this request in some internal cache.
    const response = someCache.get(req)

    // Respond with a cached response, if found.
    if (typeof response !== 'undefined') {
      return res(
    }

    // If no cache hit, retrieve the original response
    // and cache it.
    const 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@c3n21
Comment options

Answer selected by c3n21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants