Skip to content

Conversation

@kettanaito
Copy link
Member

@kettanaito kettanaito commented Jul 18, 2024

Important

Please consider some of URLPattern flaws before introducing it to MSW. We might also consider @remix-run/route-pattern instead as it's backed into Remix 3, provides great feature set, doesn't have the URLPattern shenanigans and overall looks extremely promising.

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

Development

Successfully merging this pull request may close these issues.

Using the wildcard token to try to match random ports doesn't work if preceded by a colon Support URLPattern as request predicate

1 participant