-
Notifications
You must be signed in to change notification settings - Fork 27k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: group react dev overlay entry (#69223)
- Loading branch information
Showing
2 changed files
with
54 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/next/src/client/components/react-dev-overlay/client-entry.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react' | ||
import ReactDevOverlay from './app/ReactDevOverlay' | ||
import { getSocketUrl } from './internal/helpers/get-socket-url' | ||
import { INITIAL_OVERLAY_STATE } from './shared' | ||
import { HMR_ACTIONS_SENT_TO_BROWSER } from '../../../server/dev/hot-reloader-types' | ||
|
||
// if an error is thrown while rendering an RSC stream, this will catch it in dev | ||
// and show the error overlay | ||
export function createDevOverlayElement(reactEl: React.ReactElement) { | ||
const rootLayoutMissingTags = window.__next_root_layout_missing_tags | ||
const hasMissingTags = !!rootLayoutMissingTags?.length | ||
const socketUrl = getSocketUrl(process.env.__NEXT_ASSET_PREFIX || '') | ||
const socket = new window.WebSocket(`${socketUrl}/_next/webpack-hmr`) | ||
|
||
// add minimal "hot reload" support for RSC errors | ||
const handler = (event: MessageEvent) => { | ||
let obj | ||
try { | ||
obj = JSON.parse(event.data) | ||
} catch {} | ||
|
||
if (!obj || !('action' in obj)) { | ||
return | ||
} | ||
|
||
if (obj.action === HMR_ACTIONS_SENT_TO_BROWSER.SERVER_COMPONENT_CHANGES) { | ||
window.location.reload() | ||
} | ||
} | ||
|
||
socket.addEventListener('message', handler) | ||
|
||
const FallbackLayout = hasMissingTags | ||
? ({ children }: { children: React.ReactNode }) => ( | ||
<html id="__next_error__"> | ||
<body>{children}</body> | ||
</html> | ||
) | ||
: React.Fragment | ||
|
||
return ( | ||
<FallbackLayout> | ||
<ReactDevOverlay | ||
state={{ ...INITIAL_OVERLAY_STATE, rootLayoutMissingTags }} | ||
onReactError={() => {}} | ||
> | ||
{reactEl} | ||
</ReactDevOverlay> | ||
</FallbackLayout> | ||
) | ||
} |