Files
5a7cec1e57 fix(router): Prevent redirect loop on root data requests with basePath (#81096)
Fixes #64910

This PR resolves an issue where a client-side navigation to the root
page fails when both `basePath` and `middleware` are active. This is
caused by an incorrect redirect loop on the `/_next/data` request.

The sequence of events is as follows:
1. Next.js enforces its `trailingSlash: false` policy by adding an
internal redirect from `{basePath}/` to `{basePath}`.
2. For middleware to be invoked correctly, a data request for the root
page is normalized to `/`.
3. The path is then re-joined with the `basePath` using
`path.posix.join(config.basePath, '/')`, which incorrectly results in
`{basePath}/`.
4. This malformed path triggers the redirect from step 1, causing the
data fetch to fail with a 308 status code and resulting in empty
`pageProps`.

This fix isolates the root path (`/`) during path normalization to
prevent an extra trailing slash from being added. By ensuring the path
resolves to `{basePath}` instead of `{basePath}/`, the redirect loop is
avoided.

The change correctly handles the root path while leaving the logic for
all other paths untouched.

---------

Co-authored-by: JJ Kasper <[email protected]>
2025-07-15 17:36:54 +00:00
..