undici icon indicating copy to clipboard operation
undici copied to clipboard

maxRedirections option doesn't work anymore since v7

Open fraxken opened this issue 6 months ago • 5 comments

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.

fraxken avatar Jun 29 '25 12:06 fraxken

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 avatar Jun 29 '25 14:06 metcoder95

@metcoder95 Is there is an example of how to achieve equivalent with the redirect interceptor?

I will open a PR to remove maxRedirections

fraxken avatar Jun 29 '25 15:06 fraxken

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?

fraxken avatar Jun 29 '25 15:06 fraxken

Yeah, there was no validation in place, #4311 has been opened to address that 👍

metcoder95 avatar Jun 30 '25 06:06 metcoder95

This is the documentation for the redirect interceptor: https://undici.nodejs.org/#/docs/api/Dispatcher?id=redirect

metcoder95 avatar Jun 30 '25 06:06 metcoder95