Unverified Commit 83b04c89 authored by Antoine Apollis's avatar Antoine Apollis Committed by GitHub

Feat/14701 full reload notification (#28866)

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issue: #14701
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [x] Errors have helpful link attached, see `contributing.md`

<details>

<summary>Screenshot</summary>

<img width="1388" alt="Screen Shot 2021-12-17 at 2 07 04 PM" src="https://user-images.githubusercontent.com/22380829/146602057-9675455d-cf10-435a-b121-bc462a87fbf3.png">


</details>

Closes: https://github.com/vercel/next.js/issues/14701Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 59c1f039
......@@ -31,6 +31,7 @@ import {
onBuildError,
onBuildOk,
onRefresh,
onFullRefreshNeeded,
} from '@next/react-dev-overlay/lib/client'
import stripAnsi from 'next/dist/compiled/strip-ansi'
import { addMessageListener } from './websocket'
......@@ -282,23 +283,15 @@ function tryApplyUpdates(onHotUpdateSuccess) {
function handleApplyUpdates(err, updatedModules) {
if (err || hadRuntimeError || !updatedModules) {
if (err) {
console.warn(
'[Fast Refresh] performing full reload\n\n' +
"Fast Refresh will perform a full reload when you edit a file that's imported by modules outside of the React rendering tree.\n" +
'You might have a file which exports a React component but also exports a value that is imported by a non-React component file.\n' +
'Consider migrating the non-React component export to a separate file and importing it into both files.\n\n' +
'It is also possible the parent component of the component you edited is a class component, which disables Fast Refresh.\n' +
'Fast Refresh requires at least one parent function component in your React tree.'
)
performFullRefresh(err)
} else if (hadRuntimeError) {
console.warn(
'[Fast Refresh] performing full reload because your application had an unrecoverable error'
)
performFullRefresh()
}
window.location.reload()
return
}
clearFullRefreshStorage()
const hasUpdates = Boolean(updatedModules.length)
if (typeof onHotUpdateSuccess === 'function') {
// Maybe we want to do something.
......@@ -331,3 +324,26 @@ function tryApplyUpdates(onHotUpdateSuccess) {
}
)
}
const FULL_REFRESH_STORAGE_KEY = '_has_warned_about_full_refresh'
function performFullRefresh(err) {
if (shouldWarnAboutFullRefresh()) {
sessionStorage.setItem(FULL_REFRESH_STORAGE_KEY, 'true')
onFullRefreshNeeded(err.message)
} else {
window.location.reload()
}
}
function shouldWarnAboutFullRefresh() {
return !process.env.__NEXT_TEST_MODE && !hasAlreadyWarnedAboutFullRefresh()
}
function hasAlreadyWarnedAboutFullRefresh() {
return sessionStorage.getItem(FULL_REFRESH_STORAGE_KEY) !== null
}
function clearFullRefreshStorage() {
sessionStorage.removeItem(FULL_REFRESH_STORAGE_KEY)
}
......@@ -79,10 +79,21 @@ function onBuildError(message: string) {
Bus.emit({ type: Bus.TYPE_BUILD_ERROR, message })
}
function onFullRefreshNeeded(reason?: string) {
Bus.emit({ type: Bus.TYPE_FULL_REFRESH_NEEDED, reason: reason ?? null })
}
function onRefresh() {
Bus.emit({ type: Bus.TYPE_REFRESH })
}
export { getNodeError } from './internal/helpers/nodeStackFrames'
export { default as ReactDevOverlay } from './internal/ReactDevOverlay'
export { onBuildOk, onBuildError, register, unregister, onRefresh }
export {
onBuildOk,
onBuildError,
onFullRefreshNeeded,
register,
unregister,
onRefresh,
}
import * as React from 'react'
import * as Bus from './bus'
import { ShadowPortal } from './components/ShadowPortal'
import { Errors, SupportedErrorEvent } from './container/Errors'
import { BuildError } from './container/BuildError'
import { Errors, SupportedErrorEvent } from './container/Errors'
import { FullRefreshWarning } from './container/FullRefreshWarning'
import { ErrorBoundary } from './ErrorBoundary'
import { Base } from './styles/Base'
import { ComponentStyles } from './styles/ComponentStyles'
......@@ -12,6 +14,8 @@ type OverlayState = {
nextId: number
buildError: string | null
errors: SupportedErrorEvent[]
fullRefreshReason: string | null
isAboutToFullRefresh: boolean
}
function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
......@@ -22,6 +26,19 @@ function reducer(state: OverlayState, ev: Bus.BusEvent): OverlayState {
case Bus.TYPE_BUILD_ERROR: {
return { ...state, buildError: ev.message }
}
case Bus.TYPE_FULL_REFRESH_NEEDED: {
const aboutToRefreshNewState: OverlayState = {
...state,
fullRefreshReason: null,
isAboutToFullRefresh: true,
}
if (ev.reason === null) {
return aboutToRefreshNewState
}
return { ...aboutToRefreshNewState, fullRefreshReason: ev.reason }
}
case Bus.TYPE_REFRESH: {
return { ...state, buildError: null, errors: [] }
}
......@@ -46,7 +63,13 @@ const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({
}) {
const [state, dispatch] = React.useReducer<
React.Reducer<OverlayState, Bus.BusEvent>
>(reducer, { nextId: 1, buildError: null, errors: [] })
>(reducer, {
nextId: 1,
buildError: null,
errors: [],
fullRefreshReason: null,
isAboutToFullRefresh: false,
})
React.useEffect(() => {
Bus.on(dispatch)
......@@ -64,8 +87,9 @@ const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({
const hasBuildError = state.buildError != null
const hasRuntimeErrors = Boolean(state.errors.length)
const isAboutToFullRefresh = state.isAboutToFullRefresh
const isMounted = hasBuildError || hasRuntimeErrors
const isMounted = hasBuildError || hasRuntimeErrors || isAboutToFullRefresh
return (
<React.Fragment>
<ErrorBoundary onError={onComponentError}>
......@@ -81,6 +105,8 @@ const ReactDevOverlay: React.FunctionComponent = function ReactDevOverlay({
<BuildError message={state.buildError!} />
) : hasRuntimeErrors ? (
<Errors errors={state.errors} />
) : isAboutToFullRefresh ? (
<FullRefreshWarning reason={state.fullRefreshReason} />
) : undefined}
</ShadowPortal>
) : undefined}
......
......@@ -2,6 +2,7 @@ import { StackFrame } from 'stacktrace-parser'
export const TYPE_BUILD_OK = 'build-ok'
export const TYPE_BUILD_ERROR = 'build-error'
export const TYPE_FULL_REFRESH_NEEDED = 'full-refresh-needed'
export const TYPE_REFRESH = 'fast-refresh'
export const TYPE_UNHANDLED_ERROR = 'unhandled-error'
export const TYPE_UNHANDLED_REJECTION = 'unhandled-rejection'
......@@ -12,6 +13,10 @@ export type BuildError = {
message: string
}
export type FastRefresh = { type: typeof TYPE_REFRESH }
export type FullRefreshNeeded = {
type: typeof TYPE_FULL_REFRESH_NEEDED
reason: string | null
}
export type UnhandledError = {
type: typeof TYPE_UNHANDLED_ERROR
reason: Error
......@@ -26,6 +31,7 @@ export type BusEvent =
| BuildOk
| BuildError
| FastRefresh
| FullRefreshNeeded
| UnhandledError
| UnhandledRejection
......
import * as React from 'react'
import {
Dialog,
DialogBody,
DialogContent,
DialogHeader,
} from '../components/Dialog'
import { Overlay } from '../components/Overlay'
import { Terminal } from '../components/Terminal'
import { noop as css } from '../helpers/noop-template'
export type FullRefreshWarningProps = { reason: string | null }
export const FullRefreshWarning: React.FC<FullRefreshWarningProps> =
function FullRefreshWarning({ reason }) {
const reload = React.useCallback(() => {
window.location.reload()
}, [])
return (
<Overlay fixed>
<Dialog
type="warning"
aria-labelledby="nextjs__container_refresh_warning_label"
aria-describedby="nextjs__container_refresh_warning_desc"
onClose={reload}
>
<DialogContent>
<DialogHeader className="nextjs-container-refresh-warning-header">
<h4 id="nextjs__container_refresh_warning_label">
About to perform a full refresh
</h4>
</DialogHeader>
<DialogBody className="nextjs-container-refresh-warning-body">
<FullRefreshWarningReason reason={reason} />
<footer>
<p>
You can read more about Fast Refresh in{' '}
<a href="https://nextjs.org/docs/basic-features/fast-refresh#how-it-works">
our documentation
</a>
.
</p>
<button onClick={reload}>Reload</button>
</footer>
</DialogBody>
</DialogContent>
</Dialog>
</Overlay>
)
}
export const styles = css`
.nextjs-container-refresh-warning-header > h4 {
line-height: 1.5;
margin: 0;
padding: 0;
}
.nextjs-container-refresh-warning-body footer {
margin-top: var(--size-gap-double);
}
.nextjs-container-build-error-body p {
color: #757575;
}
.nextjs-container-refresh-warning-body button {
background-color: var(--color-ansi-yellow);
border: 0;
border-radius: var(--size-gap-half);
color: var(--color-ansi-black);
cursor: pointer;
display: block;
margin-left: auto;
padding: calc(var(--size-gap) + var(--size-gap-half))
calc(var(--size-gap-double) + var(--size-gap-half));
transition: background-color 0.25s ease;
}
.nextjs-container-refresh-warning-body button:hover {
background-color: var(--color-ansi-bright-yellow);
}
`
type FullRefreshBodyProps = {
reason: string | null
}
const FullRefreshWarningReason = ({
reason,
}: FullRefreshBodyProps): JSX.Element => {
if (reason === null) {
return (
<p>
Fast Refresh will perform a full reload because your application had an
unrecoverable error.
</p>
)
}
return (
<>
<p>
Fast Refresh will perform a full reload when you edit a file that is
imported by modules outside of the React rendering tree. It is also
possible the parent component of the component you edited is a class
component, which disables Fast Refresh. Fast Refresh requires at least
one parent function component in your React tree.
</p>
<p>You can find more information in the related Webpack error below:</p>
<Terminal content={reason} />
</>
)
}
import * as React from 'react'
import { styles as codeFrame } from '../components/CodeFrame/styles'
import { styles as dialog } from '../components/Dialog'
import { styles as leftRightDialogHeader } from '../components/LeftRightDialogHeader/styles'
......@@ -7,6 +8,7 @@ import { styles as terminal } from '../components/Terminal/styles'
import { styles as toast } from '../components/Toast'
import { styles as buildErrorStyles } from '../container/BuildError'
import { styles as containerErrorStyles } from '../container/Errors'
import { styles as fullRefreshWarningStyles } from '../container/FullRefreshWarning'
import { styles as containerRuntimeErrorStyles } from '../container/RuntimeError'
import { noop as css } from '../helpers/noop-template'
......@@ -25,6 +27,7 @@ export function ComponentStyles() {
${buildErrorStyles}
${containerErrorStyles}
${containerRuntimeErrorStyles}
${fullRefreshWarningStyles}
`,
}}
/>
......
......@@ -29,7 +29,7 @@ const startServer = async (optEnv = {}, opts) => {
const env = Object.assign(
{},
clone(process.env),
{ PORT: `${appPort}` },
{ PORT: `${appPort}`, __NEXT_TEST_MODE: 'true' },
optEnv
)
......
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