-
Notifications
You must be signed in to change notification settings - Fork 27.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds updated matching handle for the server to separate out the matching and executing of different route types e.g. page routes, API routes, and app routes. Co-authored-by: JJ Kasper <[email protected]>
- Loading branch information
Showing
129 changed files
with
5,127 additions
and
609 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# `NextResponse.next()` used in a App Route Handler | ||
|
||
#### Why This Error Occurred | ||
|
||
App Route Handler's do not currently support using the `NextResponse.next()` method to forward to the next middleware because the handler is considered the endpoint to the middleware chain. Handlers must always return a `Response` object instead. | ||
|
||
#### Possible Ways to Fix It | ||
|
||
Remove the `NextResponse.next()` and replace it with a correct response handler. | ||
|
||
### Useful Links | ||
|
||
- [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response) | ||
- [`NextResponse`](https://nextjs.org/docs/api-reference/next/server#nextresponse) |
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
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 @@ | ||
export const NOT_FOUND_ERROR_CODE = 'NEXT_NOT_FOUND' | ||
const NOT_FOUND_ERROR_CODE = 'NEXT_NOT_FOUND' | ||
|
||
type NotFoundError = Error & { digest: typeof NOT_FOUND_ERROR_CODE } | ||
|
||
/** | ||
* When used in a React server component, this will set the status code to 404. | ||
* When used in a custom app route it will just send a 404 status. | ||
*/ | ||
export function notFound(): never { | ||
// eslint-disable-next-line no-throw-literal | ||
const error = new Error(NOT_FOUND_ERROR_CODE) | ||
;(error as any).digest = NOT_FOUND_ERROR_CODE | ||
;(error as NotFoundError).digest = NOT_FOUND_ERROR_CODE | ||
throw error | ||
} | ||
|
||
/** | ||
* Checks an error to determine if it's an error generated by the `notFound()` | ||
* helper. | ||
* | ||
* @param error the error that may reference a not found error | ||
* @returns true if the error is a not found error | ||
*/ | ||
export function isNotFoundError(error: any): error is NotFoundError { | ||
return error?.digest === NOT_FOUND_ERROR_CODE | ||
} |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
/* eslint-disable jest/no-try-expect */ | ||
import { redirect, REDIRECT_ERROR_CODE } from './redirect' | ||
import { getURLFromRedirectError, isRedirectError, redirect } from './redirect' | ||
describe('test', () => { | ||
it('should throw a redirect error', () => { | ||
try { | ||
redirect('/dashboard') | ||
throw new Error('did not throw') | ||
} catch (err: any) { | ||
expect(err.message).toBe(REDIRECT_ERROR_CODE) | ||
expect(err.digest).toBe(`${REDIRECT_ERROR_CODE};/dashboard`) | ||
expect(isRedirectError(err)).toBeTruthy() | ||
expect(getURLFromRedirectError(err)).toEqual('/dashboard') | ||
} | ||
}) | ||
}) |
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,55 @@ | ||
export const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT' | ||
const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT' | ||
|
||
type RedirectError<U extends string> = Error & { | ||
digest: `${typeof REDIRECT_ERROR_CODE};${U}` | ||
} | ||
|
||
/** | ||
* When used in a React server component, this will insert a meta tag to | ||
* redirect the user to the target page. When used in a custom app route, it | ||
* will serve a 302 to the caller. | ||
* | ||
* @param url the url to redirect to | ||
*/ | ||
export function redirect(url: string): never { | ||
// eslint-disable-next-line no-throw-literal | ||
const error = new Error(REDIRECT_ERROR_CODE) | ||
;(error as any).digest = REDIRECT_ERROR_CODE + ';' + url | ||
;(error as RedirectError<typeof url>).digest = `${REDIRECT_ERROR_CODE};${url}` | ||
throw error | ||
} | ||
|
||
/** | ||
* Checks an error to determine if it's an error generated by the | ||
* `redirect(url)` helper. | ||
* | ||
* @param error the error that may reference a redirect error | ||
* @returns true if the error is a redirect error | ||
*/ | ||
export function isRedirectError<U extends string>( | ||
error: any | ||
): error is RedirectError<U> { | ||
return ( | ||
typeof error?.digest === 'string' && | ||
error.digest.startsWith(REDIRECT_ERROR_CODE + ';') && | ||
error.digest.length > REDIRECT_ERROR_CODE.length + 1 | ||
) | ||
} | ||
|
||
/** | ||
* Returns the encoded URL from the error if it's a RedirectError, null | ||
* otherwise. Note that this does not validate the URL returned. | ||
* | ||
* @param error the error that may be a redirect error | ||
* @return the url if the error was a redirect error | ||
*/ | ||
export function getURLFromRedirectError<U extends string>( | ||
error: RedirectError<U> | ||
): U | ||
export function getURLFromRedirectError(error: any): string | null | ||
export function getURLFromRedirectError(error: any): string | null { | ||
if (!isRedirectError(error)) return null | ||
|
||
// Slices off the beginning of the digest that contains the code and the | ||
// separating ';'. | ||
return error.digest.slice(REDIRECT_ERROR_CODE.length + 1) | ||
} |
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 function isAppPageRoute(route: string): boolean { | ||
return route.endsWith('/page') | ||
} |
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 function isAppRouteRoute(route: string): boolean { | ||
return route.endsWith('/route') | ||
} |
Oops, something went wrong.