Skip to content

fix(fetch): update response.url correctly#714

Merged
kettanaito merged 4 commits intomainfrom
fix/response-url-list
Apr 5, 2025
Merged

fix(fetch): update response.url correctly#714
kettanaito merged 4 commits intomainfrom
fix/response-url-list

Conversation

@kettanaito
Copy link
Copy Markdown
Member

@kettanaito kettanaito commented Apr 4, 2025

Changes

Our Fetch API representation of the response now sets response.url value correctly.

If working with Undici, instead of redefining the url property, it grabs the response internal state and pushes a new entry to its urlList. This way, Interceptors doesn't interfere with Undici's url getter on the response, which is implemented per spec.

Also per spec, response.url will be '' if the Response instance was constructed manually, which is the case for Interceptors. During regular requests, the response urlList becomes a clone of the request's urlList (see this). We cannot do that, so we push a new entry to the list instead.

If working with other Fetch implementations, Interceptors will keep redefining the url property because that is the lowest predictable surface for us (no guarantee those third-party implementations implement urlList at all).

This pull request also modifies the existing tests and adds new response.url compliance tests.

Important

Interceptors will still check if the provided response.url is a valid URL instance. Relative URLs will not work as they never have**. Setting such invalid URLs will be skipped, and response.url will remain an empty string. This is done intentionally to provide a better, spec-aligned experience vs having the user deal with Invalid URL cryptic errors.

@kettanaito kettanaito changed the title Fix/response url list fix(fetch): update response.url correctly Apr 4, 2025

static setUrl(url: string | undefined, response: Response): void {
if (!url) {
if (url == null || url === 'about:') {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

about: requests must keep response.url value equal to '':

Screenshot 2025-04-04 at 11 42 57

@kettanaito kettanaito requested a review from mikicho April 4, 2025 09:52
@kettanaito
Copy link
Copy Markdown
Member Author

Hi, @mikicho 👋 Solving the response.url issue in this one. Could you please take a look at this once you have a minute? Thank you.

Copy link
Copy Markdown
Member

@mikicho mikicho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! 🚀
verified in nock.

@kettanaito kettanaito merged commit d39e65b into main Apr 5, 2025
2 checks passed
@kettanaito kettanaito deleted the fix/response-url-list branch April 5, 2025 19:36
@kettanaito
Copy link
Copy Markdown
Member Author

Released: v0.38.1 🎉

This has been released in v0.38.1!

Make sure to always update to the latest version (npm i @mswjs/interceptors@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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FetchResponse does not set the urlList

2 participants