Skip to content

Commit 9c569d8

Browse files
committed
fix: maybe fix more react 19 issues
1 parent 2a48f0e commit 9c569d8

File tree

5 files changed

+67
-8
lines changed

5 files changed

+67
-8
lines changed

e2e-tests/development-runtime/cypress/integration/hot-reloading/error-handling/page-query-result-runtime-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ after(() => {
2727
const errorPlaceholder = `false`
2828
const errorReplacement = `true`
2929

30-
// XXX FIXME(serhalp)
31-
describe.skip(
30+
describe(
3231
`testing error overlay and ability to automatically recover runtime errors cause by content changes (page queries variant)`,
3332
{ testIsolation: false },
3433
() => {

e2e-tests/development-runtime/cypress/integration/hot-reloading/error-handling/runtime-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ describe(
3939
cy.findByTestId(`hot`).should(`contain.text`, `Working`)
4040
})
4141

42-
// XXX FIXME(serhalp)
43-
it.skip(`displays error with overlay on runtime errors`, () => {
42+
it(`displays error with overlay on runtime errors`, () => {
4443
cy.exec(
4544
`npm run update -- --file src/pages/error-handling/runtime-error.js --replacements "${errorPlaceholder}:${errorReplacement}" --exact`
4645
)

e2e-tests/development-runtime/cypress/integration/hot-reloading/error-handling/static-query-result-runtime-error.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,7 @@ describe(
4646
cy.findByTestId(`results`).should(`contain.text`, `"hasError": false`)
4747
})
4848

49-
// XXX FIXME(serhalp)
50-
it.skip(`displays error with overlay on runtime errors`, () => {
49+
it(`displays error with overlay on runtime errors`, () => {
5150
cy.exec(
5251
`npm run update -- --file content/error-recovery/static-query.json --replacements "${errorPlaceholder}:${errorReplacement}" --exact`
5352
)

packages/gatsby/cache-dir/app.js

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ module.hot.accept(
3333
}
3434
)
3535

36+
// Initialize Gatsby events system early for error overlay
37+
window._gatsbyEvents = window._gatsbyEvents || []
38+
3639
window.___emitter = emitter
3740

3841
const loader = new DevLoader(asyncRequires, matchPaths)
@@ -41,16 +44,50 @@ loader.setApiRunner(apiRunner)
4144

4245
window.___loader = publicLoader
4346

47+
// React 19 error handlers for development error overlay
48+
const handleUncaughtError = (error, errorInfo) => {
49+
console.error(`Uncaught error:`, error, errorInfo)
50+
window._gatsbyEvents.push([
51+
`FAST_REFRESH`,
52+
{
53+
action: `SHOW_RUNTIME_ERRORS`,
54+
payload: [error], // Pass the actual Error object
55+
},
56+
])
57+
apiRunner(`onUncaughtError`, { error, errorInfo })
58+
}
59+
60+
const handleCaughtError = (error, errorInfo) => {
61+
// Also forward caught errors to the overlay system
62+
window._gatsbyEvents.push([
63+
`FAST_REFRESH`,
64+
{
65+
action: `SHOW_RUNTIME_ERRORS`,
66+
payload: [error],
67+
},
68+
])
69+
apiRunner(`onCaughtError`, { error, errorInfo })
70+
}
71+
4472
const reactDomClient = require(`react-dom/client`)
4573
const reactFirstRenderOrHydrate = (Component, el) => {
4674
// we will use hydrate if mount element has any content inside
4775
const useHydrate = el && el.children.length
4876

77+
// Only pass options if React 19 error handling options are available
78+
const errorHandlerOptions =
79+
handleUncaughtError || handleCaughtError
80+
? {
81+
onUncaughtError: handleUncaughtError,
82+
onCaughtError: handleCaughtError,
83+
}
84+
: undefined
85+
4986
if (useHydrate) {
50-
const root = reactDomClient.hydrateRoot(el, Component)
87+
const root = reactDomClient.hydrateRoot(el, Component, errorHandlerOptions)
5188
return () => root.unmount()
5289
} else {
53-
const root = reactDomClient.createRoot(el)
90+
const root = reactDomClient.createRoot(el, errorHandlerOptions)
5491
root.render(Component)
5592
return () => root.unmount()
5693
}

packages/gatsby/cache-dir/fast-refresh-overlay/components/error-boundary.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ export class ErrorBoundary extends React.Component {
55

66
componentDidCatch(error) {
77
this.setState({ error })
8+
9+
// Forward component errors to Fast Refresh overlay system
10+
if (
11+
window._gatsbyEvents &&
12+
Array.isArray(window._gatsbyEvents.push ? [] : window._gatsbyEvents)
13+
) {
14+
window._gatsbyEvents.push([
15+
`FAST_REFRESH`,
16+
{
17+
action: `SHOW_RUNTIME_ERRORS`,
18+
payload: [error], // Pass the actual Error object
19+
},
20+
])
21+
} else if (
22+
window._gatsbyEvents &&
23+
typeof window._gatsbyEvents.push === `function`
24+
) {
25+
window._gatsbyEvents.push([
26+
`FAST_REFRESH`,
27+
{
28+
action: `SHOW_RUNTIME_ERRORS`,
29+
payload: [error], // Pass the actual Error object
30+
},
31+
])
32+
}
833
}
934

1035
render() {

0 commit comments

Comments
 (0)