maxRedirections option doesn't work anymore since v7
Bug Description
Since version seven (or six I didn't test) the maxRedirections option do not work anymore (redirections are not followed anymore).
Reproducible By
Here is a minimal reproduction I made using undici 7.11.0 and fastify. The redirection work using undici v5.29.0
index.js
import undici from "undici";
import { createServer } from "./server.js";
await using server = createServer();
await server.listen({ port: 3000 });
const response = await undici.request("http://localhost:3000/redirect", {
method: "GET",
maxRedirections: 1
});
const data = await response.body.text();
console.log({
data,
statusCode: response.statusCode,
headers: response.headers
});
server.js
import fastify from "fastify";
export function createServer() {
const server = fastify({ logger: false });
server.get("/", async() => {
return {
uptime: process.uptime()
};
});
server.get("/redirect", (_, reply) => {
reply.redirect("/");
});
server[Symbol.asyncDispose] = async() => {
await server.close();
}
return server;
}
Expected Behavior
Follow the redirections
Environment
Node.js v22.x and v24.x
There is probably a breaking that I missed but I wonder why maxRedirections is still available.
Yeah, the maxRedirections shouldn’t be available, maybe a left over; the redirections feature was moved into the redirect interceptor.
Contributions are welcomed if the TS does not align or documentation does not reflect this
@metcoder95 Is there is an example of how to achieve equivalent with the redirect interceptor?
I will open a PR to remove maxRedirections
Ok I found some examples for other interceptors
const agent = new undici.Agent()
.compose(undici.interceptors.redirect());
undici.setGlobalDispatcher(agent);
I wonder if maxRedirections was conserved to be able to pass the option to interceptors?
Yeah, there was no validation in place, #4311 has been opened to address that 👍
This is the documentation for the redirect interceptor: https://undici.nodejs.org/#/docs/api/Dispatcher?id=redirect