Unverified Commit b4be6eb2 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Continue page rendering after handling server requests (#47089)

This moves the position of action handling so it can continue page
rendering afterwards. And a `asNotFound` option is added to
`LayoutRouter` to indicate the router to opt into the root not-found
boundary, so not found pages can be programmatically rendered instead of
relying on route mismatching (this is also needed by NEXT-463 later).

Fixes NEXT-467.
parent c3fa14d7
......@@ -422,6 +422,7 @@ function RedirectBoundary({ children }: { children: React.ReactNode }) {
interface NotFoundBoundaryProps {
notFound?: React.ReactNode
notFoundStyles?: React.ReactNode
asNotFound?: boolean
children: React.ReactNode
}
......@@ -431,7 +432,7 @@ class NotFoundErrorBoundary extends React.Component<
> {
constructor(props: NotFoundBoundaryProps) {
super(props)
this.state = { notFoundTriggered: false }
this.state = { notFoundTriggered: !!props.asNotFound }
}
static getDerivedStateFromError(error: any) {
......@@ -460,10 +461,15 @@ class NotFoundErrorBoundary extends React.Component<
function NotFoundBoundary({
notFound,
notFoundStyles,
asNotFound,
children,
}: NotFoundBoundaryProps) {
return notFound ? (
<NotFoundErrorBoundary notFound={notFound} notFoundStyles={notFoundStyles}>
<NotFoundErrorBoundary
notFound={notFound}
notFoundStyles={notFoundStyles}
asNotFound={asNotFound}
>
{children}
</NotFoundErrorBoundary>
) : (
......@@ -488,6 +494,7 @@ export default function OuterLayoutRouter({
template,
notFound,
notFoundStyles,
asNotFound,
}: {
parallelRouterKey: string
segmentPath: FlightSegmentPath
......@@ -501,6 +508,7 @@ export default function OuterLayoutRouter({
hasLoading: boolean
notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined
asNotFound?: boolean
}) {
const context = useContext(LayoutRouterContext)
if (!context) {
......@@ -562,6 +570,7 @@ export default function OuterLayoutRouter({
<NotFoundBoundary
notFound={notFound}
notFoundStyles={notFoundStyles}
asNotFound={asNotFound}
>
<RedirectBoundary>
<InnerLayoutRouter
......
This diff is collapsed.
......@@ -2,6 +2,7 @@ import type { FlightRouterState } from './app-render'
import { nonNullable } from '../lib/non-nullable'
import { getTracer } from './lib/trace/tracer'
import { AppRenderSpan } from './lib/trace/constants'
import type RenderResult from './render-result'
const queueTask =
process.env.NEXT_RUNTIME === 'edge' ? globalThis.setTimeout : setImmediate
......@@ -21,6 +22,23 @@ export function decodeText(
return textDecoder.decode(input, { stream: true })
}
export const streamToBufferedResult = async (
renderResult: RenderResult
): Promise<string> => {
const renderChunks: string[] = []
const textDecoder = new TextDecoder()
const writable = {
write(chunk: any) {
renderChunks.push(decodeText(chunk, textDecoder))
},
end() {},
destroy() {},
}
await renderResult.pipe(writable as any)
return renderChunks.join('')
}
export function readableStreamTee<T = any>(
readable: ReadableStream<T>
): [ReadableStream<T>, ReadableStream<T>] {
......
......@@ -54,5 +54,15 @@ createNextDescribe(
return browser.eval('window.location.pathname + window.location.search')
}, '/header?name=test')
})
it('should support notFound', async () => {
const browser = await next.browser('/server')
await browser.elementByCss('#nowhere').click()
await check(() => {
return browser.elementByCss('h1').text()
}, 'my-not-found')
})
}
)
export default function NotFound() {
return <h1>my-not-found</h1>
}
import { redirect } from 'next/navigation'
import { redirect, notFound } from 'next/navigation'
async function action(formData) {
'use server'
redirect('/header?name=' + formData.get('name'))
}
async function nowhere() {
'use server'
notFound()
}
export default function Form() {
return (
<>
......@@ -16,6 +21,13 @@ export default function Form() {
Submit
</button>
</form>
<hr />
<form action="" method="POST">
<input type="text" name="$$id" value={nowhere.$$id} hidden readOnly />
<button type="submit" id="nowhere">
Go nowhere
</button>
</form>
</>
)
}
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