-
Notifications
You must be signed in to change notification settings - Fork 27.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added rewrite headers after user-supplied rewrites (#74776)
Next.js's client router needs to know the pathname and query that the request was rewritten to in order to facilitate reuse of static RSC payloads generated from fallbacks. This takes the form of additional headers being sent back on the response that includes the correct rewritten pathname that later the client can take into account when generating the client route key.
- Loading branch information
Showing
13 changed files
with
576 additions
and
14 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
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
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
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
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 default async function Page(props) { | ||
const { name } = await props.params | ||
return <div>Hello {name}</div> | ||
} | ||
|
||
export async function generateStaticParams() { | ||
return [{ name: 'world' }, { name: 'wyatt' }, { name: 'admin' }] | ||
} |
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,11 @@ | ||
import { Suspense } from 'react' | ||
|
||
export default function RootLayout({ children }) { | ||
return ( | ||
<html lang="en"> | ||
<body> | ||
<Suspense>{children}</Suspense> | ||
</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,3 @@ | ||
export default function Page() { | ||
return <div>Other Page</div> | ||
} |
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 Home() { | ||
return <div>Hello World</div> | ||
} |
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,36 @@ | ||
import { NextResponse } from 'next/server' | ||
|
||
export const config = { | ||
matcher: [ | ||
/* | ||
* Match all request paths except for the ones starting with: | ||
* - api (API routes) | ||
* - _next/static (static files) | ||
* - _next/image (image optimization files) | ||
* - favicon.ico, sitemap.xml, robots.txt (metadata files) | ||
*/ | ||
'/((?!api|_next/static|_next/image|favicon.ico|sitemap.xml|robots.txt).*)', | ||
], | ||
} | ||
|
||
export default function middleware(req) { | ||
const url = new URL(req.url) | ||
|
||
if (url.pathname === '/hello/wyatt') { | ||
return NextResponse.rewrite(new URL('/hello/admin?key=value', url)) | ||
} | ||
|
||
if (url.pathname === '/hello/bob') { | ||
return NextResponse.rewrite(new URL('/hello/bobby', url)) | ||
} | ||
|
||
if (url.pathname === '/hello/john') { | ||
return NextResponse.rewrite(new URL('/hello/john?key=value', url)) | ||
} | ||
|
||
if (url.pathname === '/hello/vercel') { | ||
return NextResponse.rewrite('https://www.vercel.com') | ||
} | ||
|
||
return NextResponse.next() | ||
} |
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,23 @@ | ||
/** @type {import('next').NextConfig} */ | ||
module.exports = { | ||
rewrites: async () => { | ||
return [ | ||
{ | ||
source: '/hello/sam', | ||
destination: '/hello/samantha', | ||
}, | ||
{ | ||
source: '/hello/other', | ||
destination: '/other', | ||
}, | ||
{ | ||
source: '/hello/fred', | ||
destination: '/other?key=value', | ||
}, | ||
{ | ||
source: '/hello/(.*)/google', | ||
destination: 'https://www.google.$1/', | ||
}, | ||
] | ||
}, | ||
} |
Oops, something went wrong.