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 }) { ...@@ -422,6 +422,7 @@ function RedirectBoundary({ children }: { children: React.ReactNode }) {
interface NotFoundBoundaryProps { interface NotFoundBoundaryProps {
notFound?: React.ReactNode notFound?: React.ReactNode
notFoundStyles?: React.ReactNode notFoundStyles?: React.ReactNode
asNotFound?: boolean
children: React.ReactNode children: React.ReactNode
} }
...@@ -431,7 +432,7 @@ class NotFoundErrorBoundary extends React.Component< ...@@ -431,7 +432,7 @@ class NotFoundErrorBoundary extends React.Component<
> { > {
constructor(props: NotFoundBoundaryProps) { constructor(props: NotFoundBoundaryProps) {
super(props) super(props)
this.state = { notFoundTriggered: false } this.state = { notFoundTriggered: !!props.asNotFound }
} }
static getDerivedStateFromError(error: any) { static getDerivedStateFromError(error: any) {
...@@ -460,10 +461,15 @@ class NotFoundErrorBoundary extends React.Component< ...@@ -460,10 +461,15 @@ class NotFoundErrorBoundary extends React.Component<
function NotFoundBoundary({ function NotFoundBoundary({
notFound, notFound,
notFoundStyles, notFoundStyles,
asNotFound,
children, children,
}: NotFoundBoundaryProps) { }: NotFoundBoundaryProps) {
return notFound ? ( return notFound ? (
<NotFoundErrorBoundary notFound={notFound} notFoundStyles={notFoundStyles}> <NotFoundErrorBoundary
notFound={notFound}
notFoundStyles={notFoundStyles}
asNotFound={asNotFound}
>
{children} {children}
</NotFoundErrorBoundary> </NotFoundErrorBoundary>
) : ( ) : (
...@@ -488,6 +494,7 @@ export default function OuterLayoutRouter({ ...@@ -488,6 +494,7 @@ export default function OuterLayoutRouter({
template, template,
notFound, notFound,
notFoundStyles, notFoundStyles,
asNotFound,
}: { }: {
parallelRouterKey: string parallelRouterKey: string
segmentPath: FlightSegmentPath segmentPath: FlightSegmentPath
...@@ -501,6 +508,7 @@ export default function OuterLayoutRouter({ ...@@ -501,6 +508,7 @@ export default function OuterLayoutRouter({
hasLoading: boolean hasLoading: boolean
notFound: React.ReactNode | undefined notFound: React.ReactNode | undefined
notFoundStyles: React.ReactNode | undefined notFoundStyles: React.ReactNode | undefined
asNotFound?: boolean
}) { }) {
const context = useContext(LayoutRouterContext) const context = useContext(LayoutRouterContext)
if (!context) { if (!context) {
...@@ -562,6 +570,7 @@ export default function OuterLayoutRouter({ ...@@ -562,6 +570,7 @@ export default function OuterLayoutRouter({
<NotFoundBoundary <NotFoundBoundary
notFound={notFound} notFound={notFound}
notFoundStyles={notFoundStyles} notFoundStyles={notFoundStyles}
asNotFound={asNotFound}
> >
<RedirectBoundary> <RedirectBoundary>
<InnerLayoutRouter <InnerLayoutRouter
......
This diff is collapsed.
...@@ -2,6 +2,7 @@ import type { FlightRouterState } from './app-render' ...@@ -2,6 +2,7 @@ import type { FlightRouterState } from './app-render'
import { nonNullable } from '../lib/non-nullable' import { nonNullable } from '../lib/non-nullable'
import { getTracer } from './lib/trace/tracer' import { getTracer } from './lib/trace/tracer'
import { AppRenderSpan } from './lib/trace/constants' import { AppRenderSpan } from './lib/trace/constants'
import type RenderResult from './render-result'
const queueTask = const queueTask =
process.env.NEXT_RUNTIME === 'edge' ? globalThis.setTimeout : setImmediate process.env.NEXT_RUNTIME === 'edge' ? globalThis.setTimeout : setImmediate
...@@ -21,6 +22,23 @@ export function decodeText( ...@@ -21,6 +22,23 @@ export function decodeText(
return textDecoder.decode(input, { stream: true }) 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>( export function readableStreamTee<T = any>(
readable: ReadableStream<T> readable: ReadableStream<T>
): [ReadableStream<T>, ReadableStream<T>] { ): [ReadableStream<T>, ReadableStream<T>] {
......
...@@ -54,5 +54,15 @@ createNextDescribe( ...@@ -54,5 +54,15 @@ createNextDescribe(
return browser.eval('window.location.pathname + window.location.search') return browser.eval('window.location.pathname + window.location.search')
}, '/header?name=test') }, '/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) { async function action(formData) {
'use server' 'use server'
redirect('/header?name=' + formData.get('name')) redirect('/header?name=' + formData.get('name'))
} }
async function nowhere() {
'use server'
notFound()
}
export default function Form() { export default function Form() {
return ( return (
<> <>
...@@ -16,6 +21,13 @@ export default function Form() { ...@@ -16,6 +21,13 @@ export default function Form() {
Submit Submit
</button> </button>
</form> </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