Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
a3a20dcb
Unverified
Commit
a3a20dcb
authored
Sep 09, 2022
by
Tim Neutkens
Committed by
GitHub
Sep 09, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add template and error file types (#39808)
Co-authored-by:
Jiachi Liu
<
inbox@huozhi.im
>
parent
9ab5c012
Hide whitespace changes
Inline
Side-by-side
Showing
17 changed files
with
448 additions
and
93 deletions
+448
-93
next-app-loader.ts
packages/next/build/webpack/loaders/next-app-loader.ts
+41
-21
app-index.tsx
packages/next/client/app-index.tsx
+10
-18
layout-router.client.tsx
packages/next/client/components/layout-router.client.tsx
+98
-18
render-from-template-context.client.tsx
...client/components/render-from-template-context.client.tsx
+7
-0
app-render.tsx
packages/next/server/app-render.tsx
+89
-35
app-router-context.ts
packages/next/shared/lib/app-router-context.ts
+3
-0
constants.ts
packages/next/shared/lib/constants.ts
+1
-1
error.client.js
...e2e/app-dir/app/app/error/clientcomponent/error.client.js
+10
-0
page.client.js
.../e2e/app-dir/app/app/error/clientcomponent/page.client.js
+18
-0
page.client.js
...r/app/app/error/ssr-error-client-component/page.client.js
+3
-0
page.server.js
...dir/app/app/template/clientcomponent/other/page.server.js
+11
-0
page.server.js
...e/app-dir/app/app/template/clientcomponent/page.server.js
+12
-0
template.client.js
...p-dir/app/app/template/clientcomponent/template.client.js
+12
-0
page.server.js
...dir/app/app/template/servercomponent/other/page.server.js
+11
-0
page.server.js
...e/app-dir/app/app/template/servercomponent/page.server.js
+12
-0
template.server.js
...p-dir/app/app/template/servercomponent/template.server.js
+10
-0
index.test.ts
test/e2e/app-dir/index.test.ts
+100
-0
No files found.
packages/next/build/webpack/loaders/next-app-loader.ts
View file @
a3a20dcb
import
type
webpack
from
'webpack'
import
type
webpack
from
'webpack'
import
type
{
ValueOf
}
from
'../../../shared/lib/constants'
import
{
NODE_RESOLVE_OPTIONS
}
from
'../../webpack-config'
import
{
NODE_RESOLVE_OPTIONS
}
from
'../../webpack-config'
import
{
getModuleBuildInfo
}
from
'./get-module-build-info'
import
{
getModuleBuildInfo
}
from
'./get-module-build-info'
export
const
FILE_TYPES
=
{
layout
:
'layout'
,
template
:
'template'
,
error
:
'error'
,
loading
:
'loading'
,
}
as
const
// TODO-APP: check if this can be narrowed.
type
ComponentModule
=
()
=>
any
export
type
ComponentsType
=
{
readonly
[
componentKey
in
ValueOf
<
typeof
FILE_TYPES
>
]?:
ComponentModule
}
&
{
readonly
layoutOrPagePath
?:
string
readonly
page
?:
ComponentModule
}
async
function
createTreeCodeFromPath
({
async
function
createTreeCodeFromPath
({
pagePath
,
pagePath
,
resolve
,
resolve
,
...
@@ -39,7 +56,7 @@ async function createTreeCodeFromPath({
...
@@ -39,7 +56,7 @@ async function createTreeCodeFromPath({
const
matchedPagePath
=
`
${
appDirPrefix
}${
parallelSegmentPath
}
`
const
matchedPagePath
=
`
${
appDirPrefix
}${
parallelSegmentPath
}
`
const
resolvedPagePath
=
await
resolve
(
matchedPagePath
)
const
resolvedPagePath
=
await
resolve
(
matchedPagePath
)
// Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it.
// Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it.
props
[
parallelKey
]
=
`['', {}, {
fil
ePath:
${
JSON
.
stringify
(
props
[
parallelKey
]
=
`['', {}, {
layoutOrPag
ePath:
${
JSON
.
stringify
(
resolvedPagePath
resolvedPagePath
)}
, page: () => require(
${
JSON
.
stringify
(
resolvedPagePath
)}
)}]`
)}
, page: () => require(
${
JSON
.
stringify
(
resolvedPagePath
)}
)}]`
continue
continue
...
@@ -50,31 +67,33 @@ async function createTreeCodeFromPath({
...
@@ -50,31 +67,33 @@ async function createTreeCodeFromPath({
parallelSegment
,
parallelSegment
,
])
])
// For segmentPath === '' avoid double `/`
// `page` is not included here as it's added above.
const
layoutPath
=
`
${
appDirPrefix
}${
parallelSegmentPath
}
/layout`
const
filePaths
=
await
Promise
.
all
(
// For segmentPath === '' avoid double `/`
Object
.
values
(
FILE_TYPES
).
map
(
async
(
file
)
=>
{
const
loadingPath
=
`
${
appDirPrefix
}${
parallelSegmentPath
}
/loading`
return
[
file
,
const
resolvedLayoutPath
=
await
resolve
(
layoutPath
)
await
resolve
(
`
${
appDirPrefix
}${
parallelSegmentPath
}
/
${
file
}
`
),
const
resolvedLoadingPath
=
await
resolve
(
loadingPath
)
]
as
const
})
)
props
[
parallelKey
]
=
`[
props
[
parallelKey
]
=
`[
'
${
parallelSegment
}
',
'
${
parallelSegment
}
',
${
subtree
}
,
${
subtree
}
,
{
{
filePath:
${
JSON
.
stringify
(
resolvedLayoutPath
)}
,
${
filePaths
${
.
filter
(([,
filePath
])
=>
filePath
!==
undefined
)
resolvedLayoutPath
.
map
(([
file
,
filePath
])
=>
{
?
`layout: () => require(
${
JSON
.
stringify
(
resolvedLayoutPath
)}
),`
if
(
filePath
===
undefined
)
{
:
''
return
''
}
}
${
return
`
${
resolvedLoadingPath
file
===
FILE_TYPES
.
layout
?
`loading: () => require(
${
JSON
.
stringify
(
?
`layoutOrPagePath: '
${
filePath
}
',`
resolvedLoadingPath
:
''
)}
),`
}${
file
}
: () => require(
${
JSON
.
stringify
(
filePath
)}
),`
:
''
})
}
.
join
(
'
\
n'
)
}
}
}
]`
]`
}
}
...
@@ -164,6 +183,7 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
...
@@ -164,6 +183,7 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
export const AppRouter = require('next/dist/client/components/app-router.client.js').default
export const AppRouter = require('next/dist/client/components/app-router.client.js').default
export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default
export const LayoutRouter = require('next/dist/client/components/layout-router.client.js').default
export const RenderFromTemplateContext = require('next/dist/client/components/render-from-template-context.client.js').default
export const HotReloader =
${
export const HotReloader =
${
// Disable HotReloader component in production
// Disable HotReloader component in production
this
.
mode
===
'development'
this
.
mode
===
'development'
...
...
packages/next/client/app-index.tsx
View file @
a3a20dcb
...
@@ -2,7 +2,6 @@
...
@@ -2,7 +2,6 @@
import
'../build/polyfills/polyfill-module'
import
'../build/polyfills/polyfill-module'
// @ts-ignore react-dom/client exists when using React 18
// @ts-ignore react-dom/client exists when using React 18
import
ReactDOMClient
from
'react-dom/client'
import
ReactDOMClient
from
'react-dom/client'
// @ts-ignore startTransition exists when using React 18
import
React
from
'react'
import
React
from
'react'
import
{
createFromReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack'
import
{
createFromReadableStream
}
from
'next/dist/compiled/react-server-dom-webpack'
...
@@ -44,21 +43,6 @@ export const version = process.env.__NEXT_VERSION
...
@@ -44,21 +43,6 @@ export const version = process.env.__NEXT_VERSION
const
appElement
:
HTMLElement
|
Document
|
null
=
document
const
appElement
:
HTMLElement
|
Document
|
null
=
document
let
reactRoot
:
any
=
null
function
renderReactElement
(
domEl
:
HTMLElement
|
Document
,
fn
:
()
=>
JSX
.
Element
):
void
{
const
reactEl
=
fn
()
if
(
!
reactRoot
)
{
// Unlike with createRoot, you don't need a separate root.render() call here
reactRoot
=
(
ReactDOMClient
as
any
).
hydrateRoot
(
domEl
,
reactEl
)
}
else
{
reactRoot
.
render
(
reactEl
)
}
}
const
getCacheKey
=
()
=>
{
const
getCacheKey
=
()
=>
{
const
{
pathname
,
search
}
=
location
const
{
pathname
,
search
}
=
location
return
pathname
+
search
return
pathname
+
search
...
@@ -182,11 +166,19 @@ function RSCComponent(props: any) {
...
@@ -182,11 +166,19 @@ function RSCComponent(props: any) {
}
}
export
function
hydrate
()
{
export
function
hydrate
()
{
renderReactElement
(
appElement
!
,
()
=>
(
const
reactEl
=
(
<
React
.
StrictMode
>
<
React
.
StrictMode
>
<
Root
>
<
Root
>
<
RSCComponent
/>
<
RSCComponent
/>
</
Root
>
</
Root
>
</
React
.
StrictMode
>
</
React
.
StrictMode
>
))
)
const
isError
=
document
.
documentElement
.
id
===
'__next_error__'
const
reactRoot
=
isError
?
(
ReactDOMClient
as
any
).
createRoot
(
appElement
)
:
(
ReactDOMClient
as
any
).
hydrateRoot
(
appElement
,
reactEl
)
if
(
isError
)
{
reactRoot
.
render
(
reactEl
)
}
}
}
packages/next/client/components/layout-router.client.tsx
View file @
a3a20dcb
...
@@ -12,6 +12,7 @@ import type {
...
@@ -12,6 +12,7 @@ import type {
import
{
import
{
LayoutRouterContext
,
LayoutRouterContext
,
GlobalLayoutRouterContext
,
GlobalLayoutRouterContext
,
TemplateContext
,
}
from
'../../shared/lib/app-router-context'
}
from
'../../shared/lib/app-router-context'
import
{
fetchServerResponse
}
from
'./app-router.client'
import
{
fetchServerResponse
}
from
'./app-router.client'
// import { matchSegment } from './match-segments'
// import { matchSegment } from './match-segments'
...
@@ -312,7 +313,7 @@ function LoadingBoundary({
...
@@ -312,7 +313,7 @@ function LoadingBoundary({
}:
{
}:
{
children
:
React
.
ReactNode
children
:
React
.
ReactNode
loading
?:
React
.
ReactNode
loading
?:
React
.
ReactNode
})
{
})
:
JSX
.
Element
{
if
(
loading
)
{
if
(
loading
)
{
return
<
React
.
Suspense
fallback=
{
loading
}
>
{
children
}
</
React
.
Suspense
>
return
<
React
.
Suspense
fallback=
{
loading
}
>
{
children
}
</
React
.
Suspense
>
}
}
...
@@ -320,6 +321,65 @@ function LoadingBoundary({
...
@@ -320,6 +321,65 @@ function LoadingBoundary({
return
<>
{
children
}
</>
return
<>
{
children
}
</>
}
}
type
ErrorComponent
=
React
.
ComponentType
<
{
error
:
Error
;
reset
:
()
=>
void
}
>
interface
ErrorBoundaryProps
{
errorComponent
:
ErrorComponent
}
/**
* Handles errors through `getDerivedStateFromError`.
* Renders the provided error component and provides a way to `reset` the error boundary state.
*/
class
ErrorBoundaryHandler
extends
React
.
Component
<
ErrorBoundaryProps
,
{
error
:
Error
|
null
}
>
{
constructor
(
props
:
ErrorBoundaryProps
)
{
super
(
props
)
this
.
state
=
{
error
:
null
}
}
static
getDerivedStateFromError
(
error
:
Error
)
{
return
{
error
}
}
reset
=
()
=>
{
this
.
setState
({
error
:
null
})
}
render
()
{
if
(
this
.
state
.
error
)
{
return
(
<
this
.
props
.
errorComponent
error=
{
this
.
state
.
error
}
reset=
{
this
.
reset
}
/>
)
}
return
this
.
props
.
children
}
}
/**
* Renders error boundary with the provided "errorComponent" property as the fallback.
* If no "errorComponent" property is provided it renders the children without an error boundary.
*/
function
ErrorBoundary
({
errorComponent
,
children
,
}:
ErrorBoundaryProps
&
{
children
:
React
.
ReactNode
}):
JSX
.
Element
{
if
(
errorComponent
)
{
return
(
<
ErrorBoundaryHandler
errorComponent=
{
errorComponent
}
>
{
children
}
</
ErrorBoundaryHandler
>
)
}
return
<>
{
children
}
</>
}
/**
/**
* OuterLayoutRouter handles the current segment as well as <Offscreen> rendering of other segments.
* OuterLayoutRouter handles the current segment as well as <Offscreen> rendering of other segments.
* It can be rendered next to each other with a different `parallelRouterKey`, allowing for Parallel routes.
* It can be rendered next to each other with a different `parallelRouterKey`, allowing for Parallel routes.
...
@@ -328,12 +388,16 @@ export default function OuterLayoutRouter({
...
@@ -328,12 +388,16 @@ export default function OuterLayoutRouter({
parallelRouterKey
,
parallelRouterKey
,
segmentPath
,
segmentPath
,
childProp
,
childProp
,
error
,
loading
,
loading
,
template
,
rootLayoutIncluded
,
rootLayoutIncluded
,
}:
{
}:
{
parallelRouterKey
:
string
parallelRouterKey
:
string
segmentPath
:
FlightSegmentPath
segmentPath
:
FlightSegmentPath
childProp
:
ChildProp
childProp
:
ChildProp
error
:
ErrorComponent
template
:
React
.
ReactNode
loading
:
React
.
ReactNode
|
undefined
loading
:
React
.
ReactNode
|
undefined
rootLayoutIncluded
:
boolean
rootLayoutIncluded
:
boolean
})
{
})
{
...
@@ -371,23 +435,39 @@ export default function OuterLayoutRouter({
...
@@ -371,23 +435,39 @@ export default function OuterLayoutRouter({
<>
<>
{
preservedSegments
.
map
((
preservedSegment
)
=>
{
{
preservedSegments
.
map
((
preservedSegment
)
=>
{
return
(
return
(
// Loading boundary is render for each segment to ensure they have their own loading state.
/*
// The loading boundary is passed to the router during rendering to ensure it can be immediately rendered when suspending on a Flight fetch.
- Error boundary
<
LoadingBoundary
loading=
{
loading
}
key=
{
preservedSegment
}
>
- Only renders error boundary if error component is provided.
<
InnerLayoutRouter
- Rendered for each segment to ensure they have their own error state.
parallelRouterKey=
{
parallelRouterKey
}
- Loading boundary
url=
{
url
}
- Only renders suspense boundary if loading components is provided.
tree=
{
tree
}
- Rendered for each segment to ensure they have their own loading state.
childNodes=
{
childNodesForParallelRouter
!
}
- Passed to the router during rendering to ensure it can be immediately rendered when suspending on a Flight fetch.
childProp=
{
*/
childPropSegment
===
preservedSegment
?
childProp
:
null
<
TemplateContext
.
Provider
}
key=
{
preservedSegment
}
segmentPath=
{
segmentPath
}
value=
{
path=
{
preservedSegment
}
<
ErrorBoundary
errorComponent=
{
error
}
>
isActive=
{
currentChildSegment
===
preservedSegment
}
<
LoadingBoundary
loading=
{
loading
}
>
rootLayoutIncluded=
{
rootLayoutIncluded
}
<
InnerLayoutRouter
/>
parallelRouterKey=
{
parallelRouterKey
}
</
LoadingBoundary
>
url=
{
url
}
tree=
{
tree
}
childNodes=
{
childNodesForParallelRouter
!
}
childProp=
{
childPropSegment
===
preservedSegment
?
childProp
:
null
}
segmentPath=
{
segmentPath
}
path=
{
preservedSegment
}
isActive=
{
currentChildSegment
===
preservedSegment
}
rootLayoutIncluded=
{
rootLayoutIncluded
}
/>
</
LoadingBoundary
>
</
ErrorBoundary
>
}
>
{
template
}
</
TemplateContext
.
Provider
>
)
)
})
}
})
}
</>
</>
...
...
packages/next/client/components/render-from-template-context.client.tsx
0 → 100644
View file @
a3a20dcb
import
React
,
{
useContext
}
from
'react'
import
{
TemplateContext
}
from
'../../shared/lib/app-router-context'
export
default
function
RenderFromTemplateContext
():
JSX
.
Element
{
const
children
=
useContext
(
TemplateContext
)
return
<>
{
children
}
</>
}
packages/next/server/app-render.tsx
View file @
a3a20dcb
...
@@ -26,6 +26,7 @@ import {
...
@@ -26,6 +26,7 @@ import {
FlightManifest
,
FlightManifest
,
}
from
'../build/webpack/plugins/flight-manifest-plugin'
}
from
'../build/webpack/plugins/flight-manifest-plugin'
import
{
FlushEffectsContext
}
from
'../client/components/hooks-client'
import
{
FlushEffectsContext
}
from
'../client/components/hooks-client'
import
type
{
ComponentsType
}
from
'../build/webpack/loaders/next-app-loader'
// this needs to be required lazily so that `next-server` can set
// this needs to be required lazily so that `next-server` can set
// the env before we require
// the env before we require
...
@@ -290,12 +291,7 @@ export type Segment =
...
@@ -290,12 +291,7 @@ export type Segment =
type
LoaderTree
=
[
type
LoaderTree
=
[
segment
:
string
,
segment
:
string
,
parallelRoutes
:
{
[
parallelRouterKey
:
string
]:
LoaderTree
},
parallelRoutes
:
{
[
parallelRouterKey
:
string
]:
LoaderTree
},
components
:
{
components
:
ComponentsType
filePath
:
string
layout
?:
()
=>
any
loading
?:
()
=>
any
page
?:
()
=>
any
}
]
]
/**
/**
...
@@ -524,6 +520,8 @@ export async function renderToHTMLOrFlight(
...
@@ -524,6 +520,8 @@ export async function renderToHTMLOrFlight(
const
pageIsDynamic
=
isDynamicRoute
(
pathname
)
const
pageIsDynamic
=
isDynamicRoute
(
pathname
)
const
LayoutRouter
=
const
LayoutRouter
=
ComponentMod
.
LayoutRouter
as
typeof
import
(
'../client/components/layout-router.client'
).
default
ComponentMod
.
LayoutRouter
as
typeof
import
(
'../client/components/layout-router.client'
).
default
const
RenderFromTemplateContext
=
ComponentMod
.
RenderFromTemplateContext
as
typeof
import
(
'../client/components/render-from-template-context.client'
).
default
const
HotReloader
=
ComponentMod
.
HotReloader
as
const
HotReloader
=
ComponentMod
.
HotReloader
as
|
typeof
import
(
'../client/components/hot-reloader.client'
).
default
|
typeof
import
(
'../client/components/hot-reloader.client'
).
default
|
null
|
null
...
@@ -654,7 +652,11 @@ export async function renderToHTMLOrFlight(
...
@@ -654,7 +652,11 @@ export async function renderToHTMLOrFlight(
*/
*/
const
createComponentTree
=
async
({
const
createComponentTree
=
async
({
createSegmentPath
,
createSegmentPath
,
loaderTree
:
[
segment
,
parallelRoutes
,
{
filePath
,
layout
,
loading
,
page
}],
loaderTree
:
[
segment
,
parallelRoutes
,
{
layoutOrPagePath
,
layout
,
template
,
error
,
loading
,
page
},
],
parentParams
,
parentParams
,
firstItem
,
firstItem
,
rootLayoutIncluded
,
rootLayoutIncluded
,
...
@@ -666,11 +668,17 @@ export async function renderToHTMLOrFlight(
...
@@ -666,11 +668,17 @@ export async function renderToHTMLOrFlight(
firstItem
?:
boolean
firstItem
?:
boolean
}):
Promise
<
{
Component
:
React
.
ComponentType
}
>
=>
{
}):
Promise
<
{
Component
:
React
.
ComponentType
}
>
=>
{
// TODO-APP: enable stylesheet per layout/page
// TODO-APP: enable stylesheet per layout/page
const
stylesheets
=
getCssInlinedLinkTags
(
const
stylesheets
:
string
[]
=
layoutOrPagePath
serverComponentManifest
,
?
getCssInlinedLinkTags
(
serverCSSManifest
!
,
serverComponentManifest
,
filePath
serverCSSManifest
!
,
)
layoutOrPagePath
)
:
[]
const
Template
=
template
?
await
interopDefault
(
template
())
:
React
.
Fragment
const
ErrorComponent
=
error
?
await
interopDefault
(
error
())
:
undefined
const
Loading
=
loading
?
await
interopDefault
(
loading
())
:
undefined
const
Loading
=
loading
?
await
interopDefault
(
loading
())
:
undefined
const
isLayout
=
typeof
layout
!==
'undefined'
const
isLayout
=
typeof
layout
!==
'undefined'
const
isPage
=
typeof
page
!==
'undefined'
const
isPage
=
typeof
page
!==
'undefined'
...
@@ -746,6 +754,12 @@ export async function renderToHTMLOrFlight(
...
@@ -746,6 +754,12 @@ export async function renderToHTMLOrFlight(
parallelRouterKey=
{
parallelRouteKey
}
parallelRouterKey=
{
parallelRouteKey
}
segmentPath=
{
createSegmentPath
(
currentSegmentPath
)
}
segmentPath=
{
createSegmentPath
(
currentSegmentPath
)
}
loading=
{
Loading
?
<
Loading
/>
:
undefined
}
loading=
{
Loading
?
<
Loading
/>
:
undefined
}
error=
{
ErrorComponent
}
template=
{
<
Template
>
<
RenderFromTemplateContext
/>
</
Template
>
}
childProp=
{
childProp
}
childProp=
{
childProp
}
rootLayoutIncluded=
{
rootLayoutIncludedAtThisLevelOrAbove
}
rootLayoutIncluded=
{
rootLayoutIncludedAtThisLevelOrAbove
}
/>,
/>,
...
@@ -769,13 +783,21 @@ export async function renderToHTMLOrFlight(
...
@@ -769,13 +783,21 @@ export async function renderToHTMLOrFlight(
:
childSegment
,
:
childSegment
,
}
}
const
segmentPath
=
createSegmentPath
(
currentSegmentPath
)
// This is turned back into an object below.
// This is turned back into an object below.
return
[
return
[
parallelRouteKey
,
parallelRouteKey
,
<
LayoutRouter
<
LayoutRouter
parallelRouterKey=
{
parallelRouteKey
}
parallelRouterKey=
{
parallelRouteKey
}
segmentPath=
{
createSegmentPath
(
currentSegmentPath
)
}
segmentPath=
{
segmentPath
}
error=
{
ErrorComponent
}
loading=
{
Loading
?
<
Loading
/>
:
undefined
}
loading=
{
Loading
?
<
Loading
/>
:
undefined
}
template=
{
<
Template
>
<
RenderFromTemplateContext
/>
</
Template
>
}
childProp=
{
childProp
}
childProp=
{
childProp
}
rootLayoutIncluded=
{
rootLayoutIncludedAtThisLevelOrAbove
}
rootLayoutIncluded=
{
rootLayoutIncludedAtThisLevelOrAbove
}
/>,
/>,
...
@@ -1142,29 +1164,61 @@ export async function renderToHTMLOrFlight(
...
@@ -1142,29 +1164,61 @@ export async function renderToHTMLOrFlight(
return
flushed
return
flushed
}
}
const
renderStream
=
await
renderToInitialStream
({
try
{
ReactDOMServer
,
const
renderStream
=
await
renderToInitialStream
({
element
:
content
,
ReactDOMServer
,
streamOptions
:
{
element
:
content
,
nonce
,
streamOptions
:
{
// Include hydration scripts in the HTML
nonce
,
bootstrapScripts
:
subresourceIntegrityManifest
// Include hydration scripts in the HTML
?
buildManifest
.
rootMainFiles
.
map
((
src
)
=>
({
bootstrapScripts
:
subresourceIntegrityManifest
src
:
`
${
renderOpts
.
assetPrefix
||
''
}
/_next/`
+
src
,
?
buildManifest
.
rootMainFiles
.
map
((
src
)
=>
({
integrity
:
subresourceIntegrityManifest
[
src
],
src
:
`
${
renderOpts
.
assetPrefix
||
''
}
/_next/`
+
src
,
}))
integrity
:
subresourceIntegrityManifest
[
src
],
:
buildManifest
.
rootMainFiles
.
map
(
}))
(
src
)
=>
`
${
renderOpts
.
assetPrefix
||
''
}
/_next/`
+
src
:
buildManifest
.
rootMainFiles
.
map
(
),
(
src
)
=>
`
${
renderOpts
.
assetPrefix
||
''
}
/_next/`
+
src
},
),
})
},
})
return
await
continueFromInitialStream
(
renderStream
,
{
return
await
continueFromInitialStream
(
renderStream
,
{
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
generateStaticHTML
:
generateStaticHTML
,
generateStaticHTML
:
generateStaticHTML
,
flushEffectHandler
,
flushEffectHandler
,
flushEffectsToHead
:
true
,
flushEffectsToHead
:
true
,
})
})
}
catch
(
err
)
{
// TODO-APP: show error overlay in development. `element` should probably be wrapped in AppRouter for this case.
const
renderStream
=
await
renderToInitialStream
({
ReactDOMServer
,
element
:
(
<
html
id=
"__next_error__"
>
<
head
></
head
>
<
body
></
body
>
</
html
>
),
streamOptions
:
{
nonce
,
// Include hydration scripts in the HTML
bootstrapScripts
:
subresourceIntegrityManifest
?
buildManifest
.
rootMainFiles
.
map
((
src
)
=>
({
src
:
`
${
renderOpts
.
assetPrefix
||
''
}
/_next/`
+
src
,
integrity
:
subresourceIntegrityManifest
[
src
],
}))
:
buildManifest
.
rootMainFiles
.
map
(
(
src
)
=>
`
${
renderOpts
.
assetPrefix
||
''
}
/_next/`
+
src
),
},
})
return
await
continueFromInitialStream
(
renderStream
,
{
dataStream
:
serverComponentsInlinedTransformStream
?.
readable
,
generateStaticHTML
:
generateStaticHTML
,
flushEffectHandler
,
flushEffectsToHead
:
true
,
})
}
}
}
return
new
RenderResult
(
await
bodyResult
())
return
new
RenderResult
(
await
bodyResult
())
...
...
packages/next/shared/lib/app-router-context.ts
View file @
a3a20dcb
...
@@ -66,8 +66,11 @@ export const GlobalLayoutRouterContext = React.createContext<{
...
@@ -66,8 +66,11 @@ export const GlobalLayoutRouterContext = React.createContext<{
focusAndScrollRef
:
FocusAndScrollRef
focusAndScrollRef
:
FocusAndScrollRef
}
>
(
null
as
any
)
}
>
(
null
as
any
)
export
const
TemplateContext
=
React
.
createContext
<
React
.
ReactNode
>
(
null
as
any
)
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
if
(
process
.
env
.
NODE_ENV
!==
'production'
)
{
AppRouterContext
.
displayName
=
'AppRouterContext'
AppRouterContext
.
displayName
=
'AppRouterContext'
LayoutRouterContext
.
displayName
=
'LayoutRouterContext'
LayoutRouterContext
.
displayName
=
'LayoutRouterContext'
GlobalLayoutRouterContext
.
displayName
=
'GlobalLayoutRouterContext'
GlobalLayoutRouterContext
.
displayName
=
'GlobalLayoutRouterContext'
TemplateContext
.
displayName
=
'TemplateContext'
}
}
packages/next/shared/lib/constants.ts
View file @
a3a20dcb
type
ValueOf
<
T
>
=
Required
<
T
>
[
keyof
T
]
export
type
ValueOf
<
T
>
=
Required
<
T
>
[
keyof
T
]
export
const
COMPILER_NAMES
=
{
export
const
COMPILER_NAMES
=
{
client
:
'client'
,
client
:
'client'
,
...
...
test/e2e/app-dir/app/app/error/clientcomponent/error.client.js
0 → 100644
View file @
a3a20dcb
export
default
function
ErrorBoundary
({
error
,
reset
})
{
return
(
<>
<
p
id
=
"error-boundary-message"
>
An
error
occurred
:
{
error
.
message
}
<
/p
>
<
button
id
=
"reset"
onClick
=
{()
=>
reset
()}
>
Try
again
<
/button
>
<
/
>
)
}
test/e2e/app-dir/app/app/error/clientcomponent/page.client.js
0 → 100644
View file @
a3a20dcb
import
{
useState
}
from
'react'
export
default
function
Page
()
{
const
[
clicked
,
setClicked
]
=
useState
(
false
)
if
(
clicked
)
{
throw
new
Error
(
'this is a test'
)
}
return
(
<
button
id
=
"error-trigger-button"
onClick
=
{()
=>
{
setClicked
(
true
)
}}
>
Trigger
Error
!
<
/button
>
)
}
test/e2e/app-dir/app/app/error/ssr-error-client-component/page.client.js
0 → 100644
View file @
a3a20dcb
export
default
function
Page
()
{
throw
new
Error
(
'Error during SSR'
)
}
test/e2e/app-dir/app/app/template/clientcomponent/other/page.server.js
0 → 100644
View file @
a3a20dcb
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<>
<
p
id
=
"other-page"
>
Other
Page
<
/p
>
<
Link
href
=
"/template/clientcomponent"
>
<
a
id
=
"link"
>
To
Page
<
/a
>
<
/Link
>
<
/
>
)
}
test/e2e/app-dir/app/app/template/clientcomponent/page.server.js
0 → 100644
View file @
a3a20dcb
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<>
<
p
id
=
"page"
>
Page
<
/p
>
<
Link
href
=
"/template/clientcomponent/other"
>
<
a
id
=
"link"
>
To
Other
<
/a
>
<
/Link
>
<
/
>
)
}
test/e2e/app-dir/app/app/template/clientcomponent/template.client.js
0 → 100644
View file @
a3a20dcb
import
{
useState
}
from
'react'
export
default
function
Template
({
children
})
{
const
[
count
,
setCount
]
=
useState
(
0
)
return
(
<>
<
h1
>
Template
{
count
}
<
/h1
>
<
button
onClick
=
{()
=>
setCount
(
count
+
1
)}
>
Increment
<
/button
>
{
children
}
<
/
>
)
}
test/e2e/app-dir/app/app/template/servercomponent/other/page.server.js
0 → 100644
View file @
a3a20dcb
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<>
<
p
id
=
"other-page"
>
Other
Page
<
/p
>
<
Link
href
=
"/template/servercomponent"
>
<
a
id
=
"link"
>
To
Page
<
/a
>
<
/Link
>
<
/
>
)
}
test/e2e/app-dir/app/app/template/servercomponent/page.server.js
0 → 100644
View file @
a3a20dcb
import
Link
from
'next/link'
export
default
function
Page
()
{
return
(
<>
<
p
id
=
"page"
>
Page
<
/p
>
<
Link
href
=
"/template/servercomponent/other"
>
<
a
id
=
"link"
>
To
Other
<
/a
>
<
/Link
>
<
/
>
)
}
test/e2e/app-dir/app/app/template/servercomponent/template.server.js
0 → 100644
View file @
a3a20dcb
export
default
function
Template
({
children
})
{
return
(
<>
<
h1
>
Template
<
span
id
=
"performance-now"
>
{
performance
.
now
()}
<
/span
>
<
/h1
>
{
children
}
<
/
>
)
}
test/e2e/app-dir/index.test.ts
View file @
a3a20dcb
...
@@ -1317,6 +1317,106 @@ describe('app dir', () => {
...
@@ -1317,6 +1317,106 @@ describe('app dir', () => {
expect
(
res
.
status
).
toBe
(
500
)
expect
(
res
.
status
).
toBe
(
500
)
})
})
})
})
describe
(
'template component'
,
()
=>
{
it
(
'should render the template that holds state in a client component and reset on navigation'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/template/clientcomponent'
)
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toBe
(
'Template 0'
)
await
browser
.
elementByCss
(
'button'
).
click
()
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toBe
(
'Template 1'
)
await
browser
.
elementByCss
(
'#link'
).
click
()
await
browser
.
waitForElementByCss
(
'#other-page'
)
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toBe
(
'Template 0'
)
await
browser
.
elementByCss
(
'button'
).
click
()
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toBe
(
'Template 1'
)
await
browser
.
elementByCss
(
'#link'
).
click
()
await
browser
.
waitForElementByCss
(
'#page'
)
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toBe
(
'Template 0'
)
})
it
(
'should render the template that is a server component and rerender on navigation'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/template/servercomponent'
)
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toStartWith
(
'Template'
)
const
currentTime
=
await
browser
.
elementByCss
(
'#performance-now'
)
.
text
()
await
browser
.
elementByCss
(
'#link'
).
click
()
await
browser
.
waitForElementByCss
(
'#other-page'
)
expect
(
await
browser
.
elementByCss
(
'h1'
).
text
()).
toStartWith
(
'Template'
)
// template should rerender on navigation even when it's a server component
expect
(
await
browser
.
elementByCss
(
'#performance-now'
).
text
()).
toBe
(
currentTime
)
await
browser
.
elementByCss
(
'#link'
).
click
()
await
browser
.
waitForElementByCss
(
'#page'
)
expect
(
await
browser
.
elementByCss
(
'#performance-now'
).
text
()).
toBe
(
currentTime
)
})
})
// TODO-APP: This is disabled for development as the error overlay needs to be reworked.
;(
isDev
?
describe
.
skip
:
describe
)(
'error component'
,
()
=>
{
it
(
'should trigger error component when an error happens during rendering'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/clientcomponent'
)
await
browser
.
elementByCss
(
'#error-trigger-button'
)
.
click
()
.
waitForElementByCss
(
'#error-boundary-message'
)
expect
(
await
browser
.
elementByCss
(
'#error-boundary-message'
).
text
()
).
toBe
(
'An error occurred: this is a test'
)
})
it
(
'should allow resetting error boundary'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/clientcomponent'
)
// Try triggering and resetting a few times in a row
for
(
let
i
=
0
;
i
<
5
;
i
++
)
{
await
browser
.
elementByCss
(
'#error-trigger-button'
)
.
click
()
.
waitForElementByCss
(
'#error-boundary-message'
)
expect
(
await
browser
.
elementByCss
(
'#error-boundary-message'
).
text
()
).
toBe
(
'An error occurred: this is a test'
)
await
browser
.
elementByCss
(
'#reset'
)
.
click
()
.
waitForElementByCss
(
'#error-trigger-button'
)
expect
(
await
browser
.
elementByCss
(
'#error-trigger-button'
).
text
()
).
toBe
(
'Trigger Error!'
)
}
})
it
(
'should hydrate empty shell to handle server-side rendering errors'
,
async
()
=>
{
const
browser
=
await
webdriver
(
next
.
url
,
'/error/ssr-error-client-component'
)
const
logs
=
await
browser
.
log
()
const
errors
=
logs
.
filter
((
x
)
=>
x
.
source
===
'error'
)
.
map
((
x
)
=>
x
.
message
)
.
join
(
'
\
n'
)
expect
(
errors
).
toInclude
(
'Error during SSR'
)
})
})
}
}
describe
(
'without assetPrefix'
,
()
=>
{
describe
(
'without assetPrefix'
,
()
=>
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment