Skip to content

fix(ws): support relative urls in ws.link#2661

Merged
kettanaito merged 3 commits intomainfrom
fix/ws-url-relative
Feb 6, 2026
Merged

fix(ws): support relative urls in ws.link#2661
kettanaito merged 3 commits intomainfrom
fix/ws-url-relative

Conversation

@kettanaito
Copy link
Member

@kettanaito kettanaito commented Feb 6, 2026

Looks like ws.link() doesn't resolve relative URLs against the page's origin. The logic is there on MSW side, but Interceptors throw on relative WebSocket URLs because it never resolves them according to the specification.

Our logic didn't account that the WebSocket constructor will enforce the ws/wss scheme on the actual URL. Since a relative handler URL is resolved against location.href, it always produced a mismatch.

ws.link('/api') // "http://localhost/api"
new WebSocket('/api') // "ws://localhost/api"

We are now correctly resolving relative paths in the ws.link before passing them to the URL matching.

Caution

Relative URLs are still not allowed in Node.js. However, if you have baseUrl defined in the resolution context, MSW will use it to resolve any relative WebSocket link URLs against it.

@kettanaito kettanaito marked this pull request as draft February 6, 2026 12:26
@kettanaito kettanaito changed the title fix(ws): support relative link urls fix(ws): support relative urls in ws.link Feb 6, 2026
@kettanaito kettanaito marked this pull request as ready for review February 6, 2026 14:23
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 6, 2026

Open in StackBlitz

npm i https://pkg.pr.new/msw@2661

commit: 1e3ab42

@kettanaito kettanaito merged commit 2f1d23c into main Feb 6, 2026
21 checks passed
@kettanaito kettanaito deleted the fix/ws-url-relative branch February 6, 2026 14:48
@kettanaito
Copy link
Member Author

Released: v2.12.9 🎉

This has been released in v2.12.9.

Get these changes by running the following command:

npm i msw@latest

Predictable release automation by Release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant