Skip to content

feat: implement redirect property for fetch#734

Merged
saghul merged 2 commits intosaghul:masterfrom
jspngh:fetch-redirect
Jan 8, 2026
Merged

feat: implement redirect property for fetch#734
saghul merged 2 commits intosaghul:masterfrom
jspngh:fetch-redirect

Conversation

@jspngh
Copy link
Copy Markdown
Contributor

@jspngh jspngh commented Jan 7, 2026

This implements the redirect functionality for the Fetch API.
More info on the redirect property can be found here.

Since fetch is implemented in terms of XMLHttpRequest, this poses some issues.
Standard XMLHttpRequest always follows redirects and does not provide an API to change the behaviour.
Therefore, I had to add a non-standard redirectMode property.

I do believe this is justified, since the Fetch API is promoted as a replacement for XMLHttpRequest (e.g. here).
So having a more feature-complete implementation for Fetch should take priority, in my opinion.

Internally it is implemented by:

What do you think?

@saghul
Copy link
Copy Markdown
Owner

saghul commented Jan 7, 2026

Code looks good! Can you run make js and commit the results?

@jspngh
Copy link
Copy Markdown
Contributor Author

jspngh commented Jan 7, 2026

test-xhr-progress.js is still failing, but for me that's the case on master as well.
I think the checks are executed before the onprogress event runs.

@saghul
Copy link
Copy Markdown
Owner

saghul commented Jan 7, 2026

Hum, I'll take a look.

@jspngh
Copy link
Copy Markdown
Contributor Author

jspngh commented Jan 8, 2026

If I change the URL in test-xhr-progress.js to https://httpbin.org/get, the test does succeed.
I think some part of postman-echo changed recently, and it no longer seems to send a content-length header.

Since the content-length header isn't mandatory, the progress callback will probably not work properly for some sites.
So changing the URL is only a temporary stopgap measure to get the tests to pass.
I can commit it as part of this PR if you want.

@saghul
Copy link
Copy Markdown
Owner

saghul commented Jan 8, 2026

Sure, sounds good, thanks for chasing it down!

jspngh added 2 commits January 8, 2026 10:31
This implements the `redirect` functionality for the fetch API.

Since fetch is implemented in terms of `XMLHttpRequest`,
a non-standard `redirectMode` property had to be added.

Internally it is implemented using the `CURLOPT_FOLLOWLOCATION` and
`CURLOPT_MAXREDIRS` options.
The XHR progress test now fails when using `postman-echo.com/get`.
This seems to be because `postman-echo` is no longer sending the
Content-Length response header (which is not mandatory).

As an intermediate "solution", to get the test to pass,
we switch to a url that still does send the Content-Length header.
@saghul saghul merged commit 1925756 into saghul:master Jan 8, 2026
19 checks passed
@saghul
Copy link
Copy Markdown
Owner

saghul commented Jan 8, 2026

Cheers!

@jspngh jspngh deleted the fetch-redirect branch January 8, 2026 10:34
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.

2 participants