-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfix: ensure prefetch aliasing doesn't apply URL redirect (#69153)
Another follow up to #68340: When a prefetch is aliased (meaning we returned a prefetch entry that corresponded with the same pathname, but different / missing search params), we signal to the router that it should only copy the loading state from this prefetch entry, and null everything else out so it can be fetched from the server. However it's possible that we return an aliased entry that corresponds with a redirect (such as in middleware) meaning the server will supply a `canonicalUrlOverride` value that will correspond with the aliased entry, rather than the requested entry. This updates the handling to only apply the canonical URL override when we're not using an aliased entry. This will defer the redirect handling to when the actual dynamic request (in the server patch reducer) so that the aliased entry will only handle the loading copying and not do any redirects. This copies the existing tests that covered this behavior but adds a middleware redirect to verify the behavior is the same.
- Loading branch information
Showing
6 changed files
with
189 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
test/e2e/app-dir/searchparams-reuse-loading/app/with-middleware/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<ul> | ||
<li> | ||
<Link href="/with-middleware/search-params?id=1"> | ||
/search-params?id=1 (prefetch: true) | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/with-middleware/search-params?id=2"> | ||
/search-params?id=2 | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/with-middleware/search-params?id=3" prefetch> | ||
/search-params?id=3 (prefetch: true) | ||
</Link> | ||
</li> | ||
<li> | ||
<Link href="/with-middleware/search-params" prefetch> | ||
/search-params (prefetch: true) | ||
</Link> | ||
</li> | ||
</ul> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
test/e2e/app-dir/searchparams-reuse-loading/app/with-middleware/search-params/loading.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function Loading() { | ||
return <h1 id="loading">Loading...</h1> | ||
} |
16 changes: 16 additions & 0 deletions
16
...e/app-dir/searchparams-reuse-loading/app/with-middleware/search-params/someValue/page.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Link from 'next/link' | ||
|
||
export default async function Page({ | ||
searchParams, | ||
}: { | ||
searchParams: Record<string, string> | ||
}) { | ||
// sleep for 500ms | ||
await new Promise((resolve) => setTimeout(resolve, 500)) | ||
return ( | ||
<> | ||
<h1 id="params">{JSON.stringify(searchParams)}</h1> | ||
<Link href="/with-middleware">Back</Link> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { NextResponse } from 'next/server' | ||
import type { NextRequest } from 'next/server' | ||
|
||
export function middleware(request: NextRequest) { | ||
const { pathname, search } = request.nextUrl | ||
|
||
if ( | ||
pathname.startsWith('/with-middleware/search-params') && | ||
!pathname.includes('someValue') | ||
) { | ||
const newPathname = pathname.replace( | ||
'/with-middleware/search-params', | ||
'/with-middleware/search-params/someValue' | ||
) | ||
console.log('performing redirect') | ||
return NextResponse.redirect( | ||
new URL(`${newPathname}${search}`, request.url) | ||
) | ||
} | ||
|
||
return NextResponse.next() | ||
} | ||
|
||
export const config = { | ||
matcher: '/with-middleware/search-params', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters