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

feat!: use URLPattern for request matching #2209

Draft
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

kettanaito
Copy link
Member

@kettanaito kettanaito commented Jul 18, 2024

Warning

As much as I hate this, this is a breaking change. Some of your existing handlers may start matching differently if they contain one of the dropped features (path params merging or trailing slashes). Even if this is a change for the better, this should land in v3.0.

This also significantly improves the test coverage for path matching in MSW.

This change does not intend to support URLPattern as the input. For now, we will use it as internal mechanism (thus a polyfill version). Once the API gains global browser support, we may consider shipping an input support.

Changes to document

  • Multiple named groups are not supported: /:a/foo/:a. Not a thing in the HTTP spec. They are supported by path-to-regexp but URLPattern decided not to implement them and stick closer to the spec.
  • Trailing slashes matter now. A /foo handler will not match a /foo/ request, and vice versa. I think MSW should respect that URLPattern behavior too to bring wider adoption of the API and be spec-compliant.

Roadmap

@kettanaito
Copy link
Member Author

This failing test:

 FAIL  src/core/utils/matching/matchRequestUrl.test.ts > merges multiple same-named groups into an array of values
TypeError: Failed to construct 'URLPattern': invalid pathname pattern '/user/:segment/bar/:segment'.
 ❯ new me node_modules/.pnpm/[email protected]/node_modules/urlpattern-polyfill/dist/urlpattern.js:1:14389
 ❯ Module.matchRequestUrl src/core/utils/matching/matchRequestUrl.ts:96:19

Is caused by whatwg/urlpattern#226.

@kettanaito kettanaito added release candidate BREAKING CHANGE Pull request introducing breaking changes. labels Aug 28, 2024
@kettanaito kettanaito changed the title feat: use URLPattern for request matching feat!: use URLPattern for request matching Aug 28, 2024
@kettanaito kettanaito added this to the 3.0 milestone Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
BREAKING CHANGE Pull request introducing breaking changes. release candidate
Projects
None yet
1 participant