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
64665380
Unverified
Commit
64665380
authored
Apr 06, 2023
by
Leah
Committed by
GitHub
Apr 06, 2023
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
allow turbopack HMR to recover from react errors without hard refresh (#47771)
parent
be2237fc
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
338 additions
and
280 deletions
+338
-280
hmr-client.ts
packages/next-swc/crates/next-core/js/src/dev/hmr-client.ts
+185
-188
fallback.tsx
packages/next-swc/crates/next-core/js/src/entry/fallback.tsx
+21
-14
ErrorBoundary.tsx
...rates/next-core/js/src/overlay/internal/ErrorBoundary.tsx
+48
-14
ReactDevOverlay.tsx
...tes/next-core/js/src/overlay/internal/ReactDevOverlay.tsx
+84
-64
No files found.
packages/next-swc/crates/next-core/js/src/dev/hmr-client.ts
View file @
64665380
This diff is collapsed.
Click to expand it.
packages/next-swc/crates/next-core/js/src/entry/fallback.tsx
View file @
64665380
import
"@vercel/turbopack-next/internal/shims-client"
;
import
'@vercel/turbopack-next/internal/shims-client'
import
{
createRoot
}
from
"react-dom/client"
;
import
{
createRoot
}
from
'react-dom/client'
import
{
import
{
initializeHMR
,
initializeHMR
,
ReactDevOverlay
,
ReactDevOverlay
,
}
from
"@vercel/turbopack-next/dev/client"
;
}
from
'@vercel/turbopack-next/dev/client'
import
{
subscribeToUpdate
}
from
"@vercel/turbopack-next/dev/hmr-client"
;
import
{
subscribeToUpdate
}
from
'@vercel/turbopack-next/dev/hmr-client'
const
pageChunkPath
=
location
.
pathname
.
slice
(
1
)
;
const
pageChunkPath
=
location
.
pathname
.
slice
(
1
)
subscribeToUpdate
(
subscribeToUpdate
(
{
{
path
:
pageChunkPath
,
path
:
pageChunkPath
,
headers
:
{
headers
:
{
accept
:
"text/html"
,
accept
:
'text/html'
,
},
},
},
},
(
update
)
=>
{
(
update
)
=>
{
if
(
update
.
type
===
"restart"
||
update
.
type
===
"notFound"
)
{
if
(
update
.
type
===
'restart'
||
update
.
type
===
'notFound'
)
{
location
.
reload
()
;
location
.
reload
()
}
}
}
}
)
;
)
initializeHMR
({
initializeHMR
({
assetPrefix
:
""
,
assetPrefix
:
''
,
})
;
})
const
el
=
document
.
getElementById
(
"__next"
)
!
;
const
el
=
document
.
getElementById
(
'__next'
)
!
el
.
innerText
=
""
;
createRoot
(
el
).
render
(<
ReactDevOverlay
/>);
const
innerHtml
=
{
__html
:
el
.
innerHTML
,
}
createRoot
(
el
).
render
(
<
ReactDevOverlay
>
<
div
dangerouslySetInnerHTML=
{
innerHtml
}
></
div
>
</
ReactDevOverlay
>
)
packages/next-swc/crates/next-core/js/src/overlay/internal/ErrorBoundary.tsx
View file @
64665380
import
React
from
"react"
;
import
React
from
'react'
type
ErrorBoundaryProps
=
{
type
ErrorBoundaryProps
=
{
onError
:
(
error
:
Error
,
componentStack
:
string
|
null
)
=>
void
;
error
:
Error
|
null
fallback
:
React
.
ReactNode
|
null
;
onError
:
(
error
:
Error
,
componentStack
:
string
|
null
)
=>
void
children
?:
React
.
ReactNode
;
fallback
:
React
.
ReactNode
|
null
};
children
?:
React
.
ReactNode
}
type
ErrorBoundaryState
=
{
error
:
Error
|
null
};
type
ErrorBoundaryState
=
{
error
:
Error
|
null
lastPropsError
:
Error
|
null
}
class
ErrorBoundary
extends
React
.
PureComponent
<
class
ErrorBoundary
extends
React
.
PureComponent
<
ErrorBoundaryProps
,
ErrorBoundaryProps
,
ErrorBoundaryState
ErrorBoundaryState
>
{
>
{
static
getDerivedStateFromError
(
error
:
Error
)
{
static
getDerivedStateFromError
(
error
:
Error
)
{
return
{
error
}
;
return
{
error
}
}
}
state
=
{
error
:
null
};
state
=
{
error
:
null
,
lastPropsError
:
null
,
}
componentDidCatch
(
componentDidCatch
(
error
:
Error
,
error
:
Error
,
...
@@ -24,21 +31,48 @@ class ErrorBoundary extends React.PureComponent<
...
@@ -24,21 +31,48 @@ class ErrorBoundary extends React.PureComponent<
// accidentally excluded in some versions.
// accidentally excluded in some versions.
errorInfo
?:
{
componentStack
?:
string
|
null
}
errorInfo
?:
{
componentStack
?:
string
|
null
}
)
{
)
{
this
.
props
.
onError
(
error
,
errorInfo
?.
componentStack
??
null
);
this
.
props
.
onError
(
error
,
errorInfo
?.
componentStack
??
null
)
}
// I don't like this, but it works to clear the error boundary
//
// props.error is only set 1 render after state.error, so we can't treat it as the source of truth all the time.
// to get around this we store the error from props in state and only accept updates if the stored error from props
// matches the error in state.
//
// this is definitely not a controlled component and it only works if the error matches exactly
static
getDerivedStateFromProps
(
props
:
ErrorBoundaryProps
,
state
:
ErrorBoundaryState
):
ErrorBoundaryState
|
null
{
if
(
state
.
lastPropsError
===
props
.
error
)
{
return
null
}
let
error
=
state
.
error
if
(
state
.
error
===
state
.
lastPropsError
)
{
error
=
props
.
error
}
return
{
error
,
lastPropsError
:
props
.
error
,
}
}
}
render
()
{
render
()
{
const
{
error
}
=
this
.
state
;
const
{
error
}
=
this
.
state
const
{
fallback
}
=
this
.
props
;
const
{
fallback
}
=
this
.
props
// The component has to be unmounted or else it would continue to error
// The component has to be unmounted or else it would continue to error
if
(
error
!=
null
)
{
if
(
error
!=
null
)
{
return
fallback
;
return
fallback
}
}
return
this
.
props
.
children
;
return
this
.
props
.
children
}
}
}
}
export
{
ErrorBoundary
}
;
export
{
ErrorBoundary
}
packages/next-swc/crates/next-core/js/src/overlay/internal/ReactDevOverlay.tsx
View file @
64665380
import
*
as
React
from
"react"
;
import
*
as
React
from
'react'
import
type
{
Issue
}
from
"@vercel/turbopack-dev-runtime/types/protocol"
;
import
type
{
Issue
}
from
'@vercel/turbopack-dev-runtime/types/protocol'
import
*
as
Bus
from
"./bus"
;
import
*
as
Bus
from
'./bus'
import
{
ShadowPortal
}
from
"./components/ShadowPortal"
;
import
{
ShadowPortal
}
from
'./components/ShadowPortal'
import
{
Errors
,
SupportedErrorEvent
}
from
"./container/Errors"
;
import
{
Errors
,
SupportedErrorEvent
}
from
'./container/Errors'
import
{
ErrorBoundary
}
from
"./ErrorBoundary"
;
import
{
ErrorBoundary
}
from
'./ErrorBoundary'
import
{
Base
}
from
"./styles/Base"
;
import
{
Base
}
from
'./styles/Base'
import
{
ComponentStyles
}
from
"./styles/ComponentStyles"
;
import
{
ComponentStyles
}
from
'./styles/ComponentStyles'
import
{
CssReset
}
from
"./styles/CssReset"
;
import
{
CssReset
}
from
'./styles/CssReset'
type
RefreshState
=
type
RefreshState
=
|
{
|
{
// No refresh in progress.
// No refresh in progress.
type
:
"idle"
;
type
:
'idle'
}
}
|
{
|
{
// The refresh process has been triggered, but the new code has not been
// The refresh process has been triggered, but the new code has not been
// executed yet.
// executed yet.
type
:
"pending"
;
type
:
'pending'
errors
:
SupportedErrorEvent
[];
errors
:
SupportedErrorEvent
[]
};
reactError
:
Error
|
null
}
type
OverlayState
=
{
type
OverlayState
=
{
nextId
:
number
;
nextId
:
number
// issues are from turbopack
// issues are from turbopack
issues
:
Issue
[]
;
issues
:
Issue
[]
// errors are client side
// errors are client side
errors
:
SupportedErrorEvent
[]
;
errors
:
SupportedErrorEvent
[]
refreshState
:
RefreshState
;
refreshState
:
RefreshState
reactError
:
Error
|
null
;
reactError
:
Error
|
null
}
;
}
function
pushErrorFilterDuplicates
(
function
pushErrorFilterDuplicates
(
errors
:
SupportedErrorEvent
[],
errors
:
SupportedErrorEvent
[],
...
@@ -43,29 +44,25 @@ function pushErrorFilterDuplicates(
...
@@ -43,29 +44,25 @@ function pushErrorFilterDuplicates(
return
[
return
[
...
errors
.
filter
((
e
)
=>
{
...
errors
.
filter
((
e
)
=>
{
// Filter out duplicate errors
// Filter out duplicate errors
return
e
.
event
.
reason
!==
err
.
event
.
reason
;
return
e
.
event
.
reason
!==
err
.
event
.
reason
}),
}),
err
,
err
,
]
;
]
}
}
function
reducer
(
state
:
OverlayState
,
ev
:
Bus
.
BusEvent
):
OverlayState
{
function
reducer
(
state
:
OverlayState
,
ev
:
Bus
.
BusEvent
):
OverlayState
{
switch
(
ev
.
type
)
{
switch
(
ev
.
type
)
{
case
Bus
.
TYPE_BUILD_OK
:
{
case
Bus
.
TYPE_BUILD_OK
:
{
if
(
state
.
reactError
!=
null
)
{
return
{
...
state
}
console
.
warn
(
"[Fast Refresh] performing full reload because your application had an unrecoverable error"
);
window
.
location
.
reload
();
}
return
{
...
state
};
}
}
case
Bus
.
TYPE_TURBOPACK_ISSUES
:
{
case
Bus
.
TYPE_TURBOPACK_ISSUES
:
{
return
{
...
state
,
issues
:
ev
.
issues
}
;
return
{
...
state
,
issues
:
ev
.
issues
}
}
}
case
Bus
.
TYPE_BEFORE_REFRESH
:
{
case
Bus
.
TYPE_BEFORE_REFRESH
:
{
return
{
...
state
,
refreshState
:
{
type
:
"pending"
,
errors
:
[]
}
};
return
{
...
state
,
refreshState
:
{
type
:
'pending'
,
errors
:
[],
reactError
:
null
},
}
}
}
case
Bus
.
TYPE_REFRESH
:
{
case
Bus
.
TYPE_REFRESH
:
{
return
{
return
{
...
@@ -77,16 +74,20 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
...
@@ -77,16 +74,20 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
// around until the next refresh. Otherwise we run into a race
// around until the next refresh. Otherwise we run into a race
// condition where those errors would be cleared on refresh completion
// condition where those errors would be cleared on refresh completion
// before they can be displayed.
// before they can be displayed.
state
.
refreshState
.
type
===
"pending"
state
.
refreshState
.
type
===
'pending'
?
state
.
refreshState
.
errors
?
state
.
refreshState
.
errors
:
[],
:
[],
refreshState
:
{
type
:
"idle"
},
reactError
:
};
state
.
refreshState
.
type
===
'pending'
?
state
.
refreshState
.
reactError
:
null
,
refreshState
:
{
type
:
'idle'
},
}
}
}
case
Bus
.
TYPE_UNHANDLED_ERROR
:
case
Bus
.
TYPE_UNHANDLED_ERROR
:
case
Bus
.
TYPE_UNHANDLED_REJECTION
:
{
case
Bus
.
TYPE_UNHANDLED_REJECTION
:
{
switch
(
state
.
refreshState
.
type
)
{
switch
(
state
.
refreshState
.
type
)
{
case
"idle"
:
{
case
'idle'
:
{
return
{
return
{
...
state
,
...
state
,
nextId
:
state
.
nextId
+
1
,
nextId
:
state
.
nextId
+
1
,
...
@@ -94,9 +95,9 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
...
@@ -94,9 +95,9 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
id
:
state
.
nextId
,
id
:
state
.
nextId
,
event
:
ev
,
event
:
ev
,
}),
}),
}
;
}
}
}
case
"pending"
:
{
case
'pending'
:
{
return
{
return
{
...
state
,
...
state
,
nextId
:
state
.
nextId
+
1
,
nextId
:
state
.
nextId
+
1
,
...
@@ -107,38 +108,56 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
...
@@ -107,38 +108,56 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
event
:
ev
,
event
:
ev
,
}),
}),
},
},
}
;
}
}
}
default
:
default
:
return
state
;
return
state
}
}
}
}
case
Bus
.
TYPE_REACT_ERROR
:
{
case
Bus
.
TYPE_REACT_ERROR
:
{
return
{
...
state
,
reactError
:
ev
.
error
};
switch
(
state
.
refreshState
.
type
)
{
case
'idle'
:
{
return
{
...
state
,
reactError
:
ev
.
error
,
}
}
case
'pending'
:
{
return
{
...
state
,
refreshState
:
{
...
state
.
refreshState
,
reactError
:
ev
.
error
,
},
}
}
default
:
return
state
}
}
}
default
:
{
default
:
{
return
state
;
return
state
}
}
}
}
}
}
type
ErrorType
=
"runtime"
|
"build"
;
type
ErrorType
=
'runtime'
|
'build'
const
shouldPreventDisplay
=
(
const
shouldPreventDisplay
=
(
errorType
?:
ErrorType
|
null
,
errorType
?:
ErrorType
|
null
,
preventType
?:
ErrorType
[]
|
null
preventType
?:
ErrorType
[]
|
null
)
=>
{
)
=>
{
if
(
!
preventType
||
!
errorType
)
{
if
(
!
preventType
||
!
errorType
)
{
return
false
;
return
false
}
}
return
preventType
.
includes
(
errorType
)
;
return
preventType
.
includes
(
errorType
)
}
;
}
type
ReactDevOverlayProps
=
{
type
ReactDevOverlayProps
=
{
globalOverlay
?:
boolean
;
globalOverlay
?:
boolean
preventDisplay
?:
ErrorType
[]
;
preventDisplay
?:
ErrorType
[]
children
?:
React
.
ReactNode
;
children
?:
React
.
ReactNode
}
;
}
export
default
function
ReactDevOverlay
({
export
default
function
ReactDevOverlay
({
children
,
children
,
...
@@ -152,17 +171,17 @@ export default function ReactDevOverlay({
...
@@ -152,17 +171,17 @@ export default function ReactDevOverlay({
issues
:
[],
issues
:
[],
errors
:
[],
errors
:
[],
refreshState
:
{
refreshState
:
{
type
:
"idle"
,
type
:
'idle'
,
},
},
reactError
:
null
,
reactError
:
null
,
})
;
})
React
.
useEffect
(()
=>
{
React
.
useEffect
(()
=>
{
Bus
.
on
(
dispatch
)
;
Bus
.
on
(
dispatch
)
return
function
()
{
return
function
()
{
Bus
.
off
(
dispatch
)
;
Bus
.
off
(
dispatch
)
}
;
}
},
[
dispatch
])
;
},
[
dispatch
])
const
onComponentError
=
React
.
useCallback
(
const
onComponentError
=
React
.
useCallback
(
(
error
:
Error
,
componentStack
:
string
|
null
)
=>
{
(
error
:
Error
,
componentStack
:
string
|
null
)
=>
{
...
@@ -170,25 +189,26 @@ export default function ReactDevOverlay({
...
@@ -170,25 +189,26 @@ export default function ReactDevOverlay({
type
:
Bus
.
TYPE_REACT_ERROR
,
type
:
Bus
.
TYPE_REACT_ERROR
,
error
,
error
,
componentStack
,
componentStack
,
})
;
})
},
},
[]
[]
)
;
)
const
hasBuildError
=
state
.
issues
.
length
>
0
;
const
hasBuildError
=
state
.
issues
.
length
>
0
const
hasRuntimeErrors
=
state
.
errors
.
length
>
0
;
const
hasRuntimeErrors
=
state
.
errors
.
length
>
0
const
errorType
=
hasBuildError
const
errorType
=
hasBuildError
?
"build"
?
'build'
:
hasRuntimeErrors
:
hasRuntimeErrors
?
"runtime"
?
'runtime'
:
null
;
:
null
const
isMounted
=
hasBuildError
||
hasRuntimeErrors
;
const
isMounted
=
hasBuildError
||
hasRuntimeErrors
return
(
return
(
<
React
.
Fragment
>
<
React
.
Fragment
>
<
ErrorBoundary
<
ErrorBoundary
error=
{
state
.
reactError
}
onError=
{
onComponentError
}
onError=
{
onComponentError
}
fallback=
{
fallback=
{
// When the overlay is global for the application and it wraps a component rendering `<html>`
// When the overlay is global for the application and it wraps a component rendering `<html>`
...
@@ -214,5 +234,5 @@ export default function ReactDevOverlay({
...
@@ -214,5 +234,5 @@ export default function ReactDevOverlay({
</
ShadowPortal
>
</
ShadowPortal
>
)
:
null
}
)
:
null
}
</
React
.
Fragment
>
</
React
.
Fragment
>
)
;
)
}
}
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