mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
Fixes https://github.com/vercel/next.js/issues/65970 ---- The browser update happens at https://github.com/vercel/next.js/blob/10d5c278bc0e9eab067df7b5f3943c12ab54a6a9/packages/next/src/client/components/app-router.tsx#L685, which sets the browser location to `canonicalUrl`. `canonicalUrl` was correctly set at https://github.com/vercel/next.js/blob/ab03c3261f53fcff1fb3387673cfd170a626564c/packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts#L98, but then mistakenly overriden at https://github.com/vercel/next.js/blob/51549d92de3069b6dc5cd1f5fcdc04d5b1cbf37a/packages/next/src/client/components/router-reducer/reducers/refresh-reducer.ts#L129. This PR fixes that and includes an E2E test to prevent future regression. --------- Co-authored-by: JJ Kasper <[email protected]> Co-authored-by: Zack Tanner <[email protected]>
34 lines
1017 B
JavaScript
34 lines
1017 B
JavaScript
// @ts-check
|
|
import { NextResponse } from 'next/server'
|
|
import { NEXT_RSC_UNION_QUERY } from 'next/dist/client/components/app-router-headers'
|
|
|
|
if (NEXT_RSC_UNION_QUERY !== '_rsc') {
|
|
throw new Error(`NEXT_RSC_UNION_QUERY should be _rsc`)
|
|
}
|
|
|
|
/**
|
|
* @param {import('next/server').NextRequest} request
|
|
* @returns {NextResponse | undefined}
|
|
*/
|
|
export function middleware(request) {
|
|
const rscQuery = request.nextUrl.searchParams.get(NEXT_RSC_UNION_QUERY)
|
|
|
|
// Test that the RSC query is not present in the middleware
|
|
if (rscQuery) {
|
|
throw new Error('RSC query should not be present in the middleware')
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/redirect-middleware-to-dashboard') {
|
|
return NextResponse.redirect(new URL('/redirect-dest', request.url))
|
|
}
|
|
|
|
if (request.nextUrl.pathname === '/redirect-on-refresh/auth') {
|
|
const cookie = request.cookies.get('token')
|
|
if (cookie) {
|
|
return NextResponse.redirect(
|
|
new URL('/redirect-on-refresh/dashboard', request.url)
|
|
)
|
|
}
|
|
}
|
|
}
|