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
ef579532
Unverified
Commit
ef579532
authored
Nov 15, 2021
by
Jiachi Liu
Committed by
GitHub
Nov 15, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Close stream when fatal error occurs (#31164)
* support custom 500 error in streaming * remove unused imports
parent
eb0bd63a
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
96 additions
and
23 deletions
+96
-23
entries.ts
packages/next/build/entries.ts
+1
-0
index.ts
...build/webpack/loaders/next-middleware-ssr-loader/index.ts
+49
-23
hot-reloader.ts
packages/next/server/dev/hot-reloader.ts
+1
-0
err.js
...on/react-streaming-and-server-components/app/pages/err.js
+5
-0
render.js
...t-streaming-and-server-components/app/pages/err/render.js
+7
-0
suspense.js
...streaming-and-server-components/app/pages/err/suspense.js
+20
-0
index.test.js
.../react-streaming-and-server-components/test/index.test.js
+13
-0
No files found.
packages/next/build/entries.ts
View file @
ef579532
...
@@ -162,6 +162,7 @@ export function createEntrypoints(
...
@@ -162,6 +162,7 @@ export function createEntrypoints(
page
,
page
,
absoluteAppPath
:
pages
[
'/_app'
],
absoluteAppPath
:
pages
[
'/_app'
],
absoluteDocumentPath
:
pages
[
'/_document'
],
absoluteDocumentPath
:
pages
[
'/_document'
],
absoluteErrorPath
:
pages
[
'/_error'
],
absolutePagePath
,
absolutePagePath
,
isServerComponent
:
isFlight
,
isServerComponent
:
isFlight
,
buildId
,
buildId
,
...
...
packages/next/build/webpack/loaders/next-middleware-ssr-loader/index.ts
View file @
ef579532
...
@@ -5,6 +5,7 @@ export default async function middlewareRSCLoader(this: any) {
...
@@ -5,6 +5,7 @@ export default async function middlewareRSCLoader(this: any) {
absolutePagePath
,
absolutePagePath
,
absoluteAppPath
,
absoluteAppPath
,
absoluteDocumentPath
,
absoluteDocumentPath
,
absoluteErrorPath
,
basePath
,
basePath
,
isServerComponent
:
isServerComponentQuery
,
isServerComponent
:
isServerComponentQuery
,
assetPrefix
,
assetPrefix
,
...
@@ -14,23 +15,28 @@ export default async function middlewareRSCLoader(this: any) {
...
@@ -14,23 +15,28 @@ export default async function middlewareRSCLoader(this: any) {
const
isServerComponent
=
isServerComponentQuery
===
'true'
const
isServerComponent
=
isServerComponentQuery
===
'true'
const
stringifiedAbsolutePagePath
=
stringifyRequest
(
this
,
absolutePagePath
)
const
stringifiedAbsolutePagePath
=
stringifyRequest
(
this
,
absolutePagePath
)
const
stringifiedAbsoluteAppPath
=
stringifyRequest
(
this
,
absoluteAppPath
)
const
stringifiedAbsoluteAppPath
=
stringifyRequest
(
this
,
absoluteAppPath
)
const
stringifiedAbsoluteErrorPath
=
stringifyRequest
(
this
,
absoluteErrorPath
)
const
stringified500PagePath
=
stringifyRequest
(
this
,
'./pages/500'
)
const
stringifiedAbsoluteDocumentPath
=
stringifyRequest
(
const
stringifiedAbsoluteDocumentPath
=
stringifyRequest
(
this
,
this
,
absoluteDocumentPath
absoluteDocumentPath
)
)
let
appDefinition
=
`const App = require(
${
stringifiedAbsoluteAppPath
}
).default`
let
documentDefinition
=
`const Document = require(
${
stringifiedAbsoluteDocumentPath
}
).default`
const
transformed
=
`
const
transformed
=
`
import { adapter } from 'next/dist/server/web/adapter'
import { adapter } from 'next/dist/server/web/adapter'
import { RouterContext } from 'next/dist/shared/lib/router-context'
import { RouterContext } from 'next/dist/shared/lib/router-context'
import { renderToHTML } from 'next/dist/server/web/render'
import { renderToHTML } from 'next/dist/server/web/render'
${
appDefinition
}
import App from
${
stringifiedAbsoluteAppPath
}
${
documentDefinition
}
import Document from
${
stringifiedAbsoluteDocumentPath
}
let ErrorPage
try {
ErrorPage = require(
${
stringified500PagePath
}
).default
} catch (_) {
ErrorPage = require(
${
stringifiedAbsoluteErrorPath
}
).default
}
const {
const {
default: Page,
default: Page,
config,
config,
...
@@ -66,6 +72,7 @@ export default async function middlewareRSCLoader(this: any) {
...
@@ -66,6 +72,7 @@ export default async function middlewareRSCLoader(this: any) {
}
}
delete query.__flight__
delete query.__flight__
const req = { url: pathname }
const renderOpts = {
const renderOpts = {
Component,
Component,
pageConfig: config || {},
pageConfig: config || {},
...
@@ -97,32 +104,51 @@ export default async function middlewareRSCLoader(this: any) {
...
@@ -97,32 +104,51 @@ export default async function middlewareRSCLoader(this: any) {
const transformStream = new TransformStream()
const transformStream = new TransformStream()
const writer = transformStream.writable.getWriter()
const writer = transformStream.writable.getWriter()
const encoder = new TextEncoder()
const encoder = new TextEncoder()
let result
let renderError
let statusCode = 200
try {
try {
const
result = await renderToHTML(
result = await renderToHTML(
{ url: pathname }
,
req
,
{},
{},
pathname,
pathname,
query,
query,
renderOpts
renderOpts
)
)
result.pipe({
write: str => writer.write(encoder.encode(str)),
end: () => writer.close(),
// Not implemented: cork/uncork/on/removeListener
})
} catch (err) {
} catch (err) {
return new Response(
renderError = err
(err || 'An error occurred while rendering ' + pathname + '.').toString(),
statusCode = 500
{
}
status: 500,
if (renderError) {
headers: { 'x-middleware-ssr': '1' }
try {
}
const errorRes = { statusCode, err: renderError }
)
result = await renderToHTML(
req,
errorRes,
pathname,
query,
{ ...renderOpts, Component: ErrorPage }
)
} catch (err) {
return new Response(
(err || 'An error occurred while rendering ' + pathname + '.').toString(),
{
status: 500,
headers: { 'x-middleware-ssr': '1' }
}
)
}
}
}
result.pipe({
write: str => writer.write(encoder.encode(str)),
end: () => writer.close(),
// Not implemented: cork/uncork/on/removeListener
})
return new Response(transformStream.readable, {
return new Response(transformStream.readable, {
headers: { 'x-middleware-ssr': '1' }
headers: { 'x-middleware-ssr': '1' },
status: statusCode
})
})
}
}
...
...
packages/next/server/dev/hot-reloader.ts
View file @
ef579532
...
@@ -528,6 +528,7 @@ export default class HotReloader {
...
@@ -528,6 +528,7 @@ export default class HotReloader {
page
,
page
,
absoluteAppPath
:
this
.
pagesMapping
[
'/_app'
],
absoluteAppPath
:
this
.
pagesMapping
[
'/_app'
],
absoluteDocumentPath
:
this
.
pagesMapping
[
'/_document'
],
absoluteDocumentPath
:
this
.
pagesMapping
[
'/_document'
],
absoluteErrorPath
:
this
.
pagesMapping
[
'/_error'
],
absolutePagePath
,
absolutePagePath
,
isServerComponent
,
isServerComponent
,
buildId
:
this
.
buildId
,
buildId
:
this
.
buildId
,
...
...
test/integration/react-streaming-and-server-components/app/pages/err.js
0 → 100644
View file @
ef579532
const
page
=
()
=>
'page with err'
page
.
getInitialProps
=
()
=>
{
throw
new
Error
(
'oops'
)
}
export
default
page
test/integration/react-streaming-and-server-components/app/pages/err/render.js
0 → 100644
View file @
ef579532
let
did
=
false
export
default
function
Error
()
{
if
(
!
did
&&
typeof
window
===
'undefined'
)
{
did
=
true
throw
new
Error
(
'oops'
)
}
}
test/integration/react-streaming-and-server-components/app/pages/err/suspense.js
0 → 100644
View file @
ef579532
import
{
Suspense
}
from
'react'
let
did
=
false
function
Error
()
{
if
(
!
did
&&
typeof
window
===
'undefined'
)
{
did
=
true
throw
new
Error
(
'broken page'
)
}
}
export
default
function
page
()
{
return
(
<>
<
h1
>
Hey
Error
<
/h1
>
<
Suspense
>
<
Error
/>
<
/Suspense
>
<
/
>
)
}
test/integration/react-streaming-and-server-components/test/index.test.js
View file @
ef579532
...
@@ -24,6 +24,7 @@ const nativeModuleTestAppDir = join(__dirname, '../unsupported-native-module')
...
@@ -24,6 +24,7 @@ const nativeModuleTestAppDir = join(__dirname, '../unsupported-native-module')
const
distDir
=
join
(
__dirname
,
'../app/.next'
)
const
distDir
=
join
(
__dirname
,
'../app/.next'
)
const
documentPage
=
new
File
(
join
(
appDir
,
'pages/_document.jsx'
))
const
documentPage
=
new
File
(
join
(
appDir
,
'pages/_document.jsx'
))
const
appPage
=
new
File
(
join
(
appDir
,
'pages/_app.js'
))
const
appPage
=
new
File
(
join
(
appDir
,
'pages/_app.js'
))
const
error500Page
=
new
File
(
join
(
appDir
,
'pages/500.js'
))
const
documentWithGip
=
`
const
documentWithGip
=
`
import { Html, Head, Main, NextScript } from 'next/document'
import { Html, Head, Main, NextScript } from 'next/document'
...
@@ -55,6 +56,12 @@ function App({ Component, pageProps }) {
...
@@ -55,6 +56,12 @@ function App({ Component, pageProps }) {
export default App
export default App
`
`
const
page500
=
`
export default function Page500() {
return 'custom-500-page'
}
`
async
function
nextBuild
(
dir
)
{
async
function
nextBuild
(
dir
)
{
return
await
_nextBuild
(
dir
,
[],
{
return
await
_nextBuild
(
dir
,
[],
{
stdout
:
true
,
stdout
:
true
,
...
@@ -100,11 +107,13 @@ describe('concurrentFeatures - prod', () => {
...
@@ -100,11 +107,13 @@ describe('concurrentFeatures - prod', () => {
const
context
=
{
appDir
}
const
context
=
{
appDir
}
beforeAll
(
async
()
=>
{
beforeAll
(
async
()
=>
{
error500Page
.
write
(
page500
)
context
.
appPort
=
await
findPort
()
context
.
appPort
=
await
findPort
()
await
nextBuild
(
context
.
appDir
)
await
nextBuild
(
context
.
appDir
)
context
.
server
=
await
nextStart
(
context
.
appDir
,
context
.
appPort
)
context
.
server
=
await
nextStart
(
context
.
appDir
,
context
.
appPort
)
})
})
afterAll
(
async
()
=>
{
afterAll
(
async
()
=>
{
error500Page
.
delete
()
await
killApp
(
context
.
server
)
await
killApp
(
context
.
server
)
})
})
...
@@ -155,10 +164,12 @@ describe('concurrentFeatures - dev', () => {
...
@@ -155,10 +164,12 @@ describe('concurrentFeatures - dev', () => {
const
context
=
{
appDir
}
const
context
=
{
appDir
}
beforeAll
(
async
()
=>
{
beforeAll
(
async
()
=>
{
error500Page
.
write
(
page500
)
context
.
appPort
=
await
findPort
()
context
.
appPort
=
await
findPort
()
context
.
server
=
await
nextDev
(
context
.
appDir
,
context
.
appPort
)
context
.
server
=
await
nextDev
(
context
.
appDir
,
context
.
appPort
)
})
})
afterAll
(
async
()
=>
{
afterAll
(
async
()
=>
{
error500Page
.
delete
()
await
killApp
(
context
.
server
)
await
killApp
(
context
.
server
)
})
})
...
@@ -217,6 +228,7 @@ async function runBasicTests(context) {
...
@@ -217,6 +228,7 @@ async function runBasicTests(context) {
)
)
const
path404HTML
=
await
renderViaHTTP
(
context
.
appPort
,
'/404'
)
const
path404HTML
=
await
renderViaHTTP
(
context
.
appPort
,
'/404'
)
const
path500HTML
=
await
renderViaHTTP
(
context
.
appPort
,
'/err'
)
const
pathNotFoundHTML
=
await
renderViaHTTP
(
const
pathNotFoundHTML
=
await
renderViaHTTP
(
context
.
appPort
,
context
.
appPort
,
'/this-is-not-found'
'/this-is-not-found'
...
@@ -230,6 +242,7 @@ async function runBasicTests(context) {
...
@@ -230,6 +242,7 @@ async function runBasicTests(context) {
expect
(
dynamicRouteHTML2
).
toContain
(
'[pid]'
)
expect
(
dynamicRouteHTML2
).
toContain
(
'[pid]'
)
expect
(
path404HTML
).
toContain
(
'custom-404-page'
)
expect
(
path404HTML
).
toContain
(
'custom-404-page'
)
expect
(
path500HTML
).
toContain
(
'custom-500-page'
)
expect
(
pathNotFoundHTML
).
toContain
(
'custom-404-page'
)
expect
(
pathNotFoundHTML
).
toContain
(
'custom-404-page'
)
})
})
...
...
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