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

Preserve the original request URL in "onUnhandledRequest" warnings #2125

Closed
4 tasks done
kettanaito opened this issue Apr 5, 2024 · 3 comments · Fixed by #2128
Closed
4 tasks done

Preserve the original request URL in "onUnhandledRequest" warnings #2125

kettanaito opened this issue Apr 5, 2024 · 3 comments · Fixed by #2128
Labels
bug Something isn't working good first issue Good for newcomers scope:browser Related to MSW running in a browser scope:node Related to MSW running in Node

Comments

@kettanaito
Copy link
Member

Prerequisites

Environment check

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

Node.js version

v20.11.0

Reproduction repository

Always specify repros. Don't follow my lead.

Reproduction steps

  1. Set up MSW in the browser.
  2. Configure the onUnhandledRequest callback:
await worker.start({
  onUnhandledRequest(request, print) {
    console.log('URL:', request.url)
    if (/\.(css|js|json|png|jpg|gif)$/.test(request.url)) {
      return
    }

    print.warning()
  },
})
  1. Make a request like /fo/bar/?t=123

Current behavior

MSW prints a clean URL, making it hard to understand why my custom onUnhandledRequest logic is not matching this request:

Screenshot 2024-04-05 at 15 45 08

Expected behavior

MSW preserves the unhandled request URL as-is, which includes keeping its query parameters and fragments.

@kettanaito kettanaito added bug Something isn't working scope:node Related to MSW running in Node needs:triage Issues that have not been investigated yet. good first issue Good for newcomers and removed needs:triage Issues that have not been investigated yet. labels Apr 5, 2024
@kettanaito
Copy link
Member Author

kettanaito commented Apr 5, 2024

How to fix this

The fix would be right here:

const publicUrl = toPublicUrl(url)

The reason we use toPublicUrl() here is to omit the document's origin and print a relative URL. There are two ways to fix this:

  1. Always print an absolute URL. I.e. remove toPublicUrl and rely on request.url as-is.
  2. Still get a potentially relative URL but copy the search and fragment from the original request.url (we construct a URL instance out of it anyway).

@kettanaito kettanaito added the scope:browser Related to MSW running in a browser label Apr 5, 2024
@nagadevkrishna
Copy link
Contributor

Hi, I made a change and raised the PR: #2128

@kettanaito
Copy link
Member Author

Released: v2.2.14 🎉

This has been released in v2.2.14!

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 good first issue Good for newcomers scope:browser Related to MSW running in a browser scope:node Related to MSW running in Node
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants