-
Notifications
You must be signed in to change notification settings - Fork 407
chore(nextjs): Support unstable_rethrow inside clerkMiddleware #4347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jacekradko
merged 10 commits into
feat/nextjs-15-compat
from
elef/sdki-708-support-unstable_rethrow-inside-clerkmiddleware
Oct 22, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
64f9e69
chore(nextjs): Support `unstable_rethrow` inside `clerkMiddleware`
panteliselef 6d1d640
tidy things up
panteliselef ac028eb
add changeset
panteliselef e44e451
update with in-house not found
panteliselef 64836a6
e2e tests for unstable_rethrow on 15rc
panteliselef 6dd8482
Merge branch 'feat/nextjs-15-compat' into elef/sdki-708-support-unsta…
jacekradko 62bbff6
test fixes
jacekradko 13f8966
WIP
jacekradko 8b1713c
add missing integration test page
jacekradko 4ed45dd
Merge branch 'feat/nextjs-15-compat' into elef/sdki-708-support-unsta…
jacekradko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 @@ | ||
| --- | ||
| "@clerk/nextjs": major | ||
| --- | ||
|
|
||
| Support `unstable_rethrow` inside `clerkMiddleware`. | ||
| We changed the errors thrown by `protect()` inside `clerkMiddleware` in order for `unstable_rethrow` to recognise them and rethrow them. |
This file contains hidden or 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 hidden or 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
3 changes: 3 additions & 0 deletions
3
integration/templates/next-app-router/src/app/only-admin/page.tsx
This file contains hidden or 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>User is admin</div>; | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,111 @@ | ||
| /** | ||
| * Clerk's identifiers that are used alongside the ones from Next.js | ||
| */ | ||
| const CONTROL_FLOW_ERROR = { | ||
| FORCE_NOT_FOUND: 'CLERK_PROTECT_REWRITE', | ||
| REDIRECT_TO_URL: 'CLERK_PROTECT_REDIRECT_TO_URL', | ||
| REDIRECT_TO_SIGN_IN: 'CLERK_PROTECT_REDIRECT_TO_SIGN_IN', | ||
| }; | ||
|
|
||
| /** | ||
| * In-house implementation of `notFound()` | ||
| * https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/not-found.ts | ||
| */ | ||
| const NOT_FOUND_ERROR_CODE = 'NEXT_NOT_FOUND'; | ||
|
|
||
| type NotFoundError = Error & { | ||
| digest: typeof NOT_FOUND_ERROR_CODE; | ||
| clerk_digest: typeof CONTROL_FLOW_ERROR.FORCE_NOT_FOUND; | ||
| }; | ||
|
|
||
| function isNextjsNotFoundError(error: unknown): error is NotFoundError { | ||
| if (typeof error !== 'object' || error === null || !('digest' in error)) { | ||
| return false; | ||
| } | ||
|
|
||
| return error.digest === NOT_FOUND_ERROR_CODE; | ||
| } | ||
|
|
||
| function nextjsNotFound(): never { | ||
| const error = new Error(NOT_FOUND_ERROR_CODE); | ||
| (error as NotFoundError).digest = NOT_FOUND_ERROR_CODE; | ||
| (error as NotFoundError).clerk_digest = CONTROL_FLOW_ERROR.FORCE_NOT_FOUND; | ||
| throw error; | ||
| } | ||
|
|
||
| /** | ||
| * In-house implementation of `redirect()` | ||
| * https://github.com/vercel/next.js/blob/canary/packages/next/src/client/components/redirect.ts | ||
| */ | ||
|
|
||
| const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT'; | ||
|
|
||
| type RedirectError<T = unknown> = Error & { | ||
| digest: `${typeof REDIRECT_ERROR_CODE};${'replace'};${string};${307};`; | ||
| clerk_digest: typeof CONTROL_FLOW_ERROR.REDIRECT_TO_URL | typeof CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN; | ||
| } & T; | ||
|
|
||
| function nextjsRedirectError( | ||
| url: string, | ||
| extra: Record<string, unknown>, | ||
| type: 'replace' = 'replace', | ||
| statusCode: 307 = 307, | ||
| ): never { | ||
| const error = new Error(REDIRECT_ERROR_CODE) as RedirectError; | ||
| error.digest = `${REDIRECT_ERROR_CODE};${type};${url};${statusCode};`; | ||
| error.clerk_digest = CONTROL_FLOW_ERROR.REDIRECT_TO_URL; | ||
| Object.assign(error, extra); | ||
| throw error; | ||
| } | ||
|
|
||
| function redirectToSignInError(url: string, returnBackUrl?: string | URL | null): never { | ||
| nextjsRedirectError(url, { | ||
| clerk_digest: CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN, | ||
| returnBackUrl: returnBackUrl === null ? '' : returnBackUrl || url, | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * 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 | ||
| */ | ||
| function isNextjsRedirectError(error: unknown): error is RedirectError<{ redirectUrl: string | URL }> { | ||
| if (typeof error !== 'object' || error === null || !('digest' in error) || typeof error.digest !== 'string') { | ||
| return false; | ||
| } | ||
|
|
||
| const digest = error.digest.split(';'); | ||
| const [errorCode, type] = digest; | ||
| const destination = digest.slice(2, -2).join(';'); | ||
| const status = digest.at(-2); | ||
|
|
||
| const statusCode = Number(status); | ||
|
|
||
| return ( | ||
| errorCode === REDIRECT_ERROR_CODE && | ||
| (type === 'replace' || type === 'push') && | ||
| typeof destination === 'string' && | ||
| !isNaN(statusCode) && | ||
| statusCode === 307 | ||
| ); | ||
| } | ||
|
|
||
| function isRedirectToSignInError(error: unknown): error is RedirectError<{ returnBackUrl: string | URL }> { | ||
| if (isNextjsRedirectError(error) && 'clerk_digest' in error) { | ||
| return error.clerk_digest === CONTROL_FLOW_ERROR.REDIRECT_TO_SIGN_IN; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| export { | ||
| isNextjsNotFoundError, | ||
| nextjsNotFound, | ||
| redirectToSignInError, | ||
| nextjsRedirectError, | ||
| isNextjsRedirectError, | ||
| isRedirectToSignInError, | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.