Skip to content

Commit 60c16fa

Browse files
committed
add onError and onRecoverableError
1 parent 63d9476 commit 60c16fa

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

hydrate.tsx

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { hydrateRoot } from "react-dom/client";
1+
import { hydrateRoot, type ErrorInfo } from "react-dom/client";
22
import { RouterHost } from "./router";
33
import { getRouteMatcher } from "./router/utils/get-route-matcher";
44
import { ServerSideProps } from "./types";
@@ -16,7 +16,12 @@ export async function hydrate(
1616
Shell: React.ComponentType<
1717
{ children: React.ReactElement } & ServerSideProps
1818
>,
19-
options?: Omit<React.PropsWithoutRef<typeof RouterHost>, "children" | "Shell">
19+
{
20+
onRecoverableError = () => void 8,
21+
...options
22+
}: Omit<React.PropsWithoutRef<typeof RouterHost>, "children" | "Shell"> & {
23+
onRecoverableError?: (error: unknown, errorInfo: ErrorInfo) => void;
24+
} = {}
2025
) {
2126
const matched = match(globalX.__INITIAL_ROUTE__.split("?")[0])!;
2227
const Initial = await import(matched.value);
@@ -26,6 +31,7 @@ export async function hydrate(
2631
<Shell {...globalX.__SERVERSIDE_PROPS__}>
2732
<Initial.default {...globalX.__SERVERSIDE_PROPS__?.props} />
2833
</Shell>
29-
</RouterHost>
34+
</RouterHost>,
35+
{ onRecoverableError }
3036
);
3137
}

index.tsx

+5
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,15 @@ export class StaticRouters {
4040
preloadScript,
4141
bootstrapModules,
4242
context,
43+
onError = (error, errorInfo) => {
44+
console.error(error, errorInfo);
45+
},
4346
}: {
4447
Shell: React.ComponentType<{ children: React.ReactElement }>;
4548
preloadScript?: string;
4649
bootstrapModules?: string[];
4750
context?: T;
51+
onError(error: unknown, errorInfo: React.ErrorInfo): string | void;
4852
}
4953
): Promise<Response | null> {
5054
const { pathname, search } = new URL(request.url);
@@ -98,6 +102,7 @@ export class StaticRouters {
98102
.filter(Boolean)
99103
.join(";"),
100104
bootstrapModules,
105+
onError,
101106
}
102107
);
103108
return new Response(stream, {

0 commit comments

Comments
 (0)