Unverified Commit 976ccce5 authored by Tim Neutkens's avatar Tim Neutkens Committed by GitHub

Handle redirect in same way as 404 in new router (#40796)

parent 5d9f390f
......@@ -11,7 +11,10 @@ import type {
ChildProp,
//Segment
} from '../../server/app-render'
import type { ChildSegmentMap } from '../../shared/lib/app-router-context'
import type {
AppRouterInstance,
ChildSegmentMap,
} from '../../shared/lib/app-router-context'
import type {
FlightRouterState,
FlightSegmentPath,
......@@ -21,6 +24,7 @@ import {
LayoutRouterContext,
GlobalLayoutRouterContext,
TemplateContext,
AppRouterContext,
} from '../../shared/lib/app-router-context'
import { fetchServerResponse } from './app-router.client'
import { createInfinitePromise } from './infinite-promise'
......@@ -285,6 +289,56 @@ function LoadingBoundary({
return <>{children}</>
}
interface RedirectBoundaryProps {
router: AppRouterInstance
children: React.ReactNode
}
function InfinitePromiseComponent() {
use(createInfinitePromise())
return <></>
}
class RedirectErrorBoundary extends React.Component<
RedirectBoundaryProps,
{ redirect: string | null }
> {
constructor(props: RedirectBoundaryProps) {
super(props)
this.state = { redirect: null }
}
static getDerivedStateFromError(error: any) {
if (error.code === 'NEXT_REDIRECT') {
return { redirect: error.url }
}
// Re-throw if error is not for 404
throw error
}
render() {
const redirect = this.state.redirect
if (redirect !== null) {
setTimeout(() => {
// @ts-ignore startTransition exists
React.startTransition(() => {
this.props.router.replace(redirect, {})
})
})
return <InfinitePromiseComponent />
}
return this.props.children
}
}
function RedirectBoundary({ children }: { children: React.ReactNode }) {
const router = useContext(AppRouterContext)
return (
<RedirectErrorBoundary router={router}>{children}</RedirectErrorBoundary>
)
}
interface NotFoundBoundaryProps {
notFound?: React.ReactNode
children: React.ReactNode
......@@ -455,23 +509,27 @@ export default function OuterLayoutRouter({
key={preservedSegment}
value={
<ErrorBoundary errorComponent={error}>
<NotFoundBoundary notFound={notFound}>
<LoadingBoundary loading={loading}>
<InnerLayoutRouter
parallelRouterKey={parallelRouterKey}
url={url}
tree={tree}
childNodes={childNodesForParallelRouter!}
childProp={
childPropSegment === preservedSegment ? childProp : null
}
segmentPath={segmentPath}
path={preservedSegment}
isActive={currentChildSegment === preservedSegment}
rootLayoutIncluded={rootLayoutIncluded}
/>
</LoadingBoundary>
</NotFoundBoundary>
<LoadingBoundary loading={loading}>
<NotFoundBoundary notFound={notFound}>
<RedirectBoundary>
<InnerLayoutRouter
parallelRouterKey={parallelRouterKey}
url={url}
tree={tree}
childNodes={childNodesForParallelRouter!}
childProp={
childPropSegment === preservedSegment
? childProp
: null
}
segmentPath={segmentPath}
path={preservedSegment}
isActive={currentChildSegment === preservedSegment}
rootLayoutIncluded={rootLayoutIncluded}
/>
</RedirectBoundary>
</NotFoundBoundary>
</LoadingBoundary>
</ErrorBoundary>
}
>
......
import React, { experimental_use as use } from 'react'
import { AppRouterContext } from '../../shared/lib/app-router-context'
import { createInfinitePromise } from './infinite-promise'
export const REDIRECT_ERROR_CODE = 'NEXT_REDIRECT'
export function redirect(url: string) {
if (process.browser) {
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())
}
// eslint-disable-next-line no-throw-literal
const error = new Error(REDIRECT_ERROR_CODE)
;(error as any).url = url
......
......@@ -1513,7 +1513,7 @@ describe('app dir', () => {
)
})
it('should redirect in a client component', async () => {
it.skip('should redirect in a client component', async () => {
const browser = await webdriver(next.url, '/redirect/clientcomponent')
await browser.waitForElementByCss('#result-page')
expect(await browser.elementByCss('#result-page').text()).toBe(
......@@ -1521,7 +1521,8 @@ describe('app dir', () => {
)
})
it('should redirect client-side', async () => {
// TODO-APP: Enable in development
;(isDev ? it.skip : it)('should redirect client-side', async () => {
const browser = await webdriver(next.url, '/redirect/client-side')
await browser
.elementByCss('button')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment