Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mask Flight Parameters from Middleware (#39939)
This masks flight parameters from middleware so it doesn't interfere with RSC or routing. ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm lint` - [ ] The examples guidelines are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing.md#adding-examples) Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
- Loading branch information
1 parent
a8e54e7
commit 33a6dca
Showing
13 changed files
with
263 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import type { NextParsedUrlQuery } from './request-meta' | ||
|
||
const INTERNAL_QUERY_NAMES = [ | ||
'__nextFallback', | ||
'__nextLocale', | ||
'__nextDefaultLocale', | ||
'__nextIsNotFound', | ||
// RSC | ||
'__flight__', | ||
// Routing | ||
'__flight_router_state_tree__', | ||
'__flight_prefetch__', | ||
] as const | ||
|
||
const EXTENDED_INTERNAL_QUERY_NAMES = ['__nextDataReq'] as const | ||
|
||
export function stripInternalQueries(query: NextParsedUrlQuery) { | ||
for (const name of INTERNAL_QUERY_NAMES) { | ||
delete query[name] | ||
} | ||
} | ||
|
||
export function stripInternalSearchParams( | ||
searchParams: URLSearchParams, | ||
extended?: boolean | ||
) { | ||
for (const name of INTERNAL_QUERY_NAMES) { | ||
searchParams.delete(name) | ||
} | ||
|
||
if (extended) { | ||
for (const name of EXTENDED_INTERNAL_QUERY_NAMES) { | ||
searchParams.delete(name) | ||
} | ||
} | ||
|
||
return searchParams | ||
} |
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,3 @@ | ||
export default function Page() { | ||
return <div id="failure">Failure</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,18 @@ | ||
import Link from 'next/link' | ||
|
||
export default function Page() { | ||
return ( | ||
<div> | ||
<div> | ||
<Link href="/internal/test/rewrite"> | ||
<a id="navigate-rewrite">Navigate Rewrite</a> | ||
</Link> | ||
</div> | ||
<div> | ||
<Link href="/internal/test/redirect"> | ||
<a id="navigate-redirect">Navigate Redirect</a> | ||
</Link> | ||
</div> | ||
</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 Page() { | ||
return <div id="success">Success</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 |
---|---|---|
@@ -1,8 +1,25 @@ | ||
// @ts-check | ||
import { NextResponse } from 'next/server' | ||
|
||
/** | ||
* @param {import('next/server').NextRequest} request | ||
* @returns {NextResponse | undefined} | ||
*/ | ||
export function middleware(request) { | ||
if (request.nextUrl.pathname === '/middleware-to-dashboard') { | ||
// TODO: this does not copy __flight__ and __flight_router_state_tree__ | ||
return NextResponse.rewrite(new URL('/dashboard', request.url)) | ||
} | ||
|
||
if (request.nextUrl.pathname.startsWith('/internal/test')) { | ||
const method = request.nextUrl.pathname.endsWith('rewrite') | ||
? 'rewrite' | ||
: 'redirect' | ||
|
||
const internal = ['__flight__', '__flight_router_state_tree__'] | ||
if (internal.some((name) => request.nextUrl.searchParams.has(name))) { | ||
return NextResponse[method](new URL('/internal/failure', request.url)) | ||
} | ||
|
||
return NextResponse[method](new URL('/internal/success', request.url)) | ||
} | ||
} |
Oops, something went wrong.