ComponentErrorBoundary
Catches an error thrown while a home block renders, logs it, and shows nothing in production so the rest of the page survives.
import { ComponentErrorBoundary } from '@salla.sa/twilight-theme-engine/components/home';In plain words
Normally, when one component crashes while drawing, React removes the whole page. An error boundary is a component that stops that at its edge: whatever is inside it can crash, and only that part disappears.
HomeComponentRenderer already puts every home block inside this one. Use it yourself only where you render blocks without the renderer.
Signature
class ComponentErrorBoundary extends React.Component<{
children: ReactNode;
data: HomeComponentData; // used for the log and the development card
position: number;
}> {}Try it live
Banner block
Features block
import { ComponentErrorBoundary } from '@salla.sa/twilight-theme-engine/components/home';
import type { HomeComponentData } from '@salla.sa/twilight-theme-engine/types';
import { Banner } from './Banner';
// Only needed where you render blocks yourself: HomeComponentRenderer already wraps each one.
export function SafeBanner({ block, position }: { block: HomeComponentData; position: number }) {
return (
<ComponentErrorBoundary data={block} position={position}>
<Banner data={block} />
</ComponentErrorBoundary>
);
}
Example
import { ComponentErrorBoundary } from '@salla.sa/twilight-theme-engine/components/home';
import type { HomeComponentData } from '@salla.sa/twilight-theme-engine/types';
import { Brands } from './Brands';
export function SafeBrands({ block }: { block: HomeComponentData }) {
return (
<ComponentErrorBoundary data={block} position={1}>
<Brands data={block} />
</ComponentErrorBoundary>
);
}
How it behaves
In a development build (
NODE_ENVnotproduction) it shows a red card with the path, the message, the stack and the block data. In production it rendersnull.It logs a collapsed group through
useTwilight().log(component, position, URL, time, both stacks, data). It reads the Twilight context withstatic contextType; outsideTwilightProviderit only logs that it must be used inside it.
Gotchas
Like every React error boundary, it catches errors thrown while rendering, not in event handlers or in promises your effects start.
Error boundaries do not catch during the server render: there it only passes its children through. A block that throws there makes React fall back to the nearest
<Suspense>(the renderer gives each visible block one) and render it again in the browser, where this boundary catches.Once it has caught an error it keeps showing its fallback until it remounts. Give it a new
keyto try again.