-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix rewrites to edge routes (#58797)
### What? Rewrites to an edge route currently throw an invariant rather than properly serving up the page that is rewritten to. ### Why? The `NextRequest` object that is provided to the edge route handler contains pathname information only for the "origin" request (e.g., when visiting `/one/example` which rewrites to `/two/example`, the pathname is still `/one/example`. This hits an invariant since the route matcher is unable to find `/one/example` since it does not exist. ### How? This updates the module wrapper to grab the pathname from the route definition rather than the request object. For dynamic segments, we extract them from `request.nextUrl` since we know that even if `nextUrl` is referencing the origin path, the parameters it has are relevant for the rewrite. This adds the `getUtils` utility that's also used in base-server to handle the URL normalization, which also provides an interface for normalizing dynamic params from a `ParsedUrlQuery`. Closes NEXT-1724 Fixes #48295
- Loading branch information
Showing
7 changed files
with
92 additions
and
20 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
8 changes: 8 additions & 0 deletions
8
test/e2e/app-dir/edge-route-rewrite/app/dynamic/[slug]/route.ts
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,8 @@ | ||
export const runtime = 'edge' | ||
export const dynamic = 'force-dynamic' | ||
|
||
export function GET(req, { params }) { | ||
return new Response( | ||
`Hello from /app/dynamic/[slug]/route.ts. Slug: ${params.slug}` | ||
) | ||
} |
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 @@ | ||
export const metadata = { | ||
title: 'Next.js', | ||
description: 'Generated by Next.js', | ||
} | ||
|
||
export default function RootLayout({ | ||
children, | ||
}: { | ||
children: React.ReactNode | ||
}) { | ||
return ( | ||
<html lang="en"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
} |
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,6 @@ | ||
export const runtime = 'edge' | ||
export const dynamic = 'force-dynamic' | ||
|
||
export function GET() { | ||
return new Response('Hello from /app/two/example/route.ts') | ||
} |
21 changes: 21 additions & 0 deletions
21
test/e2e/app-dir/edge-route-rewrite/edge-route-rewrite.test.ts
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,21 @@ | ||
import { createNextDescribe } from 'e2e-utils' | ||
|
||
createNextDescribe( | ||
'edge-route-rewrite', | ||
{ | ||
files: __dirname, | ||
}, | ||
({ next }) => { | ||
it('it should support a rewrite to an edge route', async () => { | ||
const result = await next.render('/one/example') | ||
expect(result).toContain('Hello from /app/two/example/route.ts') | ||
}) | ||
|
||
it('it should support a rewrite to a dynamic edge route', async () => { | ||
const result = await next.render('/dynamic-test/foo') | ||
expect(result).toContain( | ||
'Hello from /app/dynamic/[slug]/route.ts. Slug: foo' | ||
) | ||
}) | ||
} | ||
) |
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,21 @@ | ||
import { type NextRequest, NextResponse } from 'next/server' | ||
|
||
export function middleware(request: NextRequest) { | ||
const url = request.nextUrl | ||
|
||
let originalPathname = url.pathname | ||
|
||
if (url.pathname.includes('/one')) { | ||
url.pathname = '/two/example' | ||
} else if (url.pathname.includes('/dynamic-test')) { | ||
url.pathname = '/dynamic/foo' | ||
} | ||
|
||
if (url.pathname !== originalPathname) { | ||
return NextResponse.rewrite(url) | ||
} | ||
} | ||
|
||
export const config = { | ||
matcher: ['/one/:path*', '/dynamic-test/:path*'], | ||
} |
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,6 @@ | ||
/** | ||
* @type {import('next').NextConfig} | ||
*/ | ||
const nextConfig = {} | ||
|
||
module.exports = nextConfig |