forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle redirects in new router (vercel#40396)
Co-authored-by: Sebastian Markbåge <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
- Loading branch information
1 parent
0fb3284
commit c90e5f0
Showing
23 changed files
with
475 additions
and
149 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,22 @@ | ||
/** | ||
* Used to cache in createInfinitePromise | ||
*/ | ||
let infinitePromise: Promise<void> | ||
|
||
/** | ||
* Create a Promise that does not resolve. This is used to suspend when data is not available yet. | ||
*/ | ||
export function createInfinitePromise() { | ||
if (!infinitePromise) { | ||
// Only create the Promise once | ||
infinitePromise = new Promise((/* resolve */) => { | ||
// This is used to debug when the rendering is never updated. | ||
// setTimeout(() => { | ||
// infinitePromise = new Error('Infinite promise') | ||
// resolve() | ||
// }, 5000) | ||
}) | ||
} | ||
|
||
return infinitePromise | ||
} |
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,15 @@ | ||
import React, { experimental_use as use } from 'react' | ||
import { AppRouterContext } from '../../shared/lib/app-router-context' | ||
import { createInfinitePromise } from './infinite-promise' | ||
|
||
export function redirect(url: string) { | ||
const router = use(AppRouterContext) | ||
setTimeout(() => { | ||
// @ts-ignore startTransition exists | ||
React.startTransition(() => { | ||
router.replace(url, {}) | ||
}) | ||
}) | ||
// setTimeout is used to start a new transition during render, this is an intentional hack around React. | ||
use(createInfinitePromise()) | ||
} |
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,9 @@ | ||
export const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT' | ||
|
||
export function redirect(url: string) { | ||
// eslint-disable-next-line no-throw-literal | ||
throw { | ||
url, | ||
code: REDIRECT_ERROR_CODE, | ||
} | ||
} |
Oops, something went wrong.