Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendBeacon causes infinite loop with response patching #1216

Closed
4 tasks done
hpohlmeyer opened this issue Apr 20, 2022 · 4 comments · Fixed by #2353
Closed
4 tasks done

sendBeacon causes infinite loop with response patching #1216

hpohlmeyer opened this issue Apr 20, 2022 · 4 comments · Fixed by #2353
Assignees
Labels
bug Something isn't working help wanted Extra attention is needed scope:browser Related to MSW running in a browser

Comments

@hpohlmeyer
Copy link

hpohlmeyer commented Apr 20, 2022

Prerequisites

Environment check

  • I'm using the latest msw version
  • I'm using Node.js version 14 or higher

Browsers

Chromium (Chrome, Brave, etc.), Firefox, Safari

Reproduction repository

https://github.com/hpohlmeyer/msw-send-beacon-loop

Reproduction steps

  1. Check out the repro: git clone https://github.com/hpohlmeyer/msw-send-beacon-loop.git
  2. Run npm install in the root folder
  3. Run npm start
  4. Go to http://localhost:3002/ in Chrome (FF just freezes without seeing anything in the network tab)
  5. Open the network tab in the devtools
  6. Click on the "Trigger request" button
    • You will see an infinite request loop

Current behavior

It looks like ctx.fetch(req) does not seem to bypass msw and triggers the same request over and over again, causing an infinite request loop.

Expected behavior

ctx.fetch(req) should bypass msw and not trigger the same route handler again.

@hpohlmeyer hpohlmeyer added bug Something isn't working needs:triage Issues that have not been investigated yet. scope:browser Related to MSW running in a browser labels Apr 20, 2022
@kettanaito
Copy link
Member

Hey, @hpohlmeyer. Thanks for reporting this!

I'm currently overwhelmed with the issues but I will keep this one in mind whenever I have a spare minute. Any volunteers are welcome on this one!

@kettanaito kettanaito added the help wanted Extra attention is needed label Apr 27, 2022
@kettanaito
Copy link
Member

kettanaito commented Nov 10, 2024

Hi, @hpohlmeyer. Thanks for reporting this.

All below will be in the context of upgrading your reproduction example to MSW 2.0.

Reason

The infinite request look is cause by the request instance not having the special x-msw-intention header. The header is missing because sendBeacon() requests cannot be modified. They are always POST and none of their properties are configurable.

Fix

I can try migrating from the request header to a request search parameter for the bypassed request mode. Request URL should be configurable even for sendBeacon request because it contains request payload (search params).

The downside of this approach is that the intercepted request URL would have to be parsed into a URL instance on each request, which can be performance implications.

@kettanaito kettanaito self-assigned this Nov 10, 2024
@kettanaito kettanaito removed the needs:triage Issues that have not been investigated yet. label Nov 10, 2024
@kettanaito
Copy link
Member

We can rely on the accept header, which is supported in sendBeacon requests. I've opened a fix at #2353, the existing usage scenarios for bypass requests should pass too.

@kettanaito
Copy link
Member

Released: v2.6.4 🎉

This has been released in v2.6.4!

Make sure to always update to the latest version (npm i msw@latest) to get the newest features and bug fixes.


Predictable release automation by @ossjs/release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed scope:browser Related to MSW running in a browser
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants