next.js icon indicating copy to clipboard operation
next.js copied to clipboard

Rewritten paths cause a client side redirect unexpectedly when set up with basePath: false and an active basePath

Open mareksuscak opened this issue 1 year ago • 2 comments

Link to the code that reproduces this issue

https://github.com/mareksuscak/router-bug/blob/main/next.config.mjs#L5-L18

To Reproduce

  1. Start the application in development mode (next dev)
  2. Navigate to http://localhost:3000/destination

Current vs. Expected behavior

Expected: page is displayed and the address bar in the browser reads /destination, not /prefix/destination Current: page is displayed and the address bar in the browser reads /prefix/destination, not /destination

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 23.4.0: Fri Mar 15 00:12:37 PDT 2024; root:xnu-10063.101.17~1/RELEASE_ARM64_T6031
  Available memory (MB): 65536
  Available CPU cores: 16
Binaries:
  Node: 20.11.1
  npm: 10.2.4
  Yarn: 1.22.22
  pnpm: 8.15.5
Relevant Packages:
  next: 14.2.3 // Latest available version is detected (14.2.3).
  eslint-config-next: 14.2.3
  react: 18.3.0
  react-dom: 18.3.0
  typescript: 5.4.5
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Pages Router

Which stage(s) are affected? (Select all that apply)

next dev (local), next build (local), next start (local), Vercel (Deployed), Other (Deployed)

Additional context

I tested latest canary release 14.3.0-canary.23 and the bug is still present there. It's been happening for months. What's interesting is that this seems to be a client-side triggered redirect because the server returns HTTP 200 initially and the path changes in the browser right after. I've also tried writing a middleware to handle the rewrite instead of using rewrites in the config and it's still happening - likely a bug in the client side routing code. Here's the middleware I tried:

import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'

export function middleware(request: NextRequest) {
  if (request.nextUrl.pathname.startsWith('/destination')) {
    return NextResponse.rewrite(new URL('/prefix/destination', request.url))
  }
}

mareksuscak avatar Apr 25 '24 20:04 mareksuscak

I found bug about Link with basePath #66660 maybe this is related

hscstudio avatar Jun 08 '24 04:06 hscstudio

This issue has been automatically marked as stale due to inactivity. It will be closed in 7 days unless there’s further input. If you believe this issue is still relevant, please leave a comment or provide updated details. Thank you.

nextjs-bot avatar Dec 05 '25 23:12 nextjs-bot

This issue has been automatically closed due to inactivity. If you’re still experiencing a similar problem or have additional details to share, please open a new issue following our current issue template. Your updated report helps us investigate and address concerns more efficiently. Thank you for your understanding!

nextjs-bot avatar Dec 12 '25 23:12 nextjs-bot