question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Option to call event.respondWith only if we have registered handler for this request

See original GitHub issue

Is your feature request related to a problem? Please describe. A lot of requests are completed with warning

Warning: captured a request without a matching request handler

because MSW calls event.respondWith almost for every request

Describe the solution you’d like It would be really great, if we could configure worker.start() with bypassUnhandledPath/bypassUnknownPath option

worker.start({ bypassUnknownPath: true })

Or with some bypass function, where I could write own match test

const handlers = [] // MSW handlers
const match = (path, url) => { /* ... */ }
worker.start({
  bypass: (req) => {
    const foundHandler = handlers.find((handler) => match(handler.info.path, req.url));
    return !foundHandler;
  },
});

Describe alternatives you’ve considered I’ve added this code to fetch listener in mockServiceWorker.js

// Bypass static files (.js/css/woff2/etc).
if (new URL(request.url).pathname.match(/\.(.*)$/)) {
  return;
}

So at least now I ignore static files. But this is not enough for me.

Additional context I’m not afraid about warnings, I can disable them. In fact I’m afraid of Chrome’s “network” tab, where I see a lot of XHR requests intercepted with MSW. But truly these request are css/js/woff2/html/..etc. And I would like to not intercept them with MSW at all. image

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
evisongcommented, Mar 23, 2022

But I can’t stand this mess in Network tab 😃

Just FYI, in Chrome Devtools, Network tab, you may add -is:service-worker-initiated -is:service-worker-intercepted to filter out service worker requests. Much cleaner after that.

0reactions
kettanaitocommented, Jan 31, 2022

A great find regarding the difference in the x-msw-bypass header! I’ve opened an issue about it https://github.com/mswjs/msw/issues/1085. @crutch12, could you be interested in helping us fix it? I’ll assist you during the code review and make sure we cover this behavior difference. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FetchEvent.respondWith() - Web APIs - MDN Web Docs
The respondWith() method of FetchEvent prevents the browser's default fetch handling, and allows you to provide a promise for a Response ...
Read more >
How to call asynchronous function before event.respondWith ...
if i move url to inside event.responseWith. its work, but do every request, but i need only match url to fetch in service...
Read more >
FetchEvent · Cloudflare Workers docs
If no fetch event handler calls respondWith , then the runtime forwards the request to the origin as if the Worker did not....
Read more >
JavaScript Async - Service Workers - I Programmer
However, if the event handler awaits for some function to complete i.e. is asynchronous before calling respondWith, the thread is freed and the ......
Read more >
Serving - web.dev
When a request comes into your service worker, there are two things you can do; ... But you can't call respondWith() after the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found