Skip to content

Commit

Permalink
Add auto reload if lazy component fails to load (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleJune authored Aug 3, 2024
1 parent 3b82a7f commit e8e2119
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 206 deletions.
31 changes: 19 additions & 12 deletions client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,18 +180,25 @@ export function lazy<
: undefined;
if (typeof boundaryOrFactory !== "string") factory = boundaryOrFactory;
return reactLazy(async () => {
const { default: Component, ErrorFallback, boundary: boundaryOverride } =
await factory!();
const errorBoundaryProps = {
FallbackComponent: ErrorFallback,
} as ErrorBoundaryProps;
if (boundaryOverride) boundary = boundaryOverride;
if (boundary) errorBoundaryProps.boundary = boundaryOverride ?? boundary;
try {
const { default: Component, ErrorFallback, boundary: boundaryOverride } =
await factory!();
const errorBoundaryProps = {
FallbackComponent: ErrorFallback,
} as ErrorBoundaryProps;
if (boundaryOverride) boundary = boundaryOverride;
if (boundary) errorBoundaryProps.boundary = boundaryOverride ?? boundary;

return {
default: errorBoundaryProps.FallbackComponent
? withErrorBoundary(Component, errorBoundaryProps)
: Component,
};
return {
default: errorBoundaryProps.FallbackComponent
? withErrorBoundary(Component, errorBoundaryProps)
: Component,
};
} catch (error) {
const log = getLogger();
log.error("Error loading component", error, { boundary });
window.location.reload();
throw error;
}
});
}
2 changes: 1 addition & 1 deletion deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
"@std/fs": "jsr:@std/fs@0",
"@std/log": "jsr:@std/log@0",
"@std/path": "jsr:@std/path@1",
"@std/testing": "jsr:@std/testing@0",
"@std/testing": "jsr:@std/testing@1",
"esbuild": "npm:[email protected]",
"react": "npm:react@18",
"@types/react": "npm:@types/react@18",
Expand Down
54 changes: 33 additions & 21 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion example/deno.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
"@std/fs": "jsr:@std/fs@0",
"@std/log": "jsr:@std/log@0",
"@std/path": "jsr:@std/path@1",
"@std/testing": "jsr:@std/testing@0",
"@std/testing": "jsr:@std/testing@1",
"esbuild": "npm:[email protected]",
"react": "npm:react@18",
"@types/react": "npm:@types/react@18",
Expand Down
Loading

0 comments on commit e8e2119

Please sign in to comment.