Catch errors at runtime

Jeasx streams markup by default. Errors that happen before the first chunk is sent are handled by Fastify's error handler, which answers with the right status code.

By default, it responds with JSON; register your own via setErrorHandler() in FASTIFY_SERVER of jeasx.config.js to render a user-friendly error page. Once streaming has started, status and headers are already sent: wrap risky components in an error boundary to render a fallback instead. Pick a scenario below and have a look at the source to see how things are wired up.

Error before streaming

The route throws before it returns any markup. Nothing is sent yet, so Fastify's error handler answers with status 500.

Try it→ Fastify's error handler
Error while streaming, caught

A component fails after the page has started streaming. An error boundary catches it and renders a fallback instead.

Error while streaming, uncaught

The same component without a boundary. Status and headers are already sent, so the page simply ends where the error happened.

How the error boundary works

ErrorBoundary.jsx renders its children to a string first with jsxToString, so it can catch what goes wrong and render a fallback instead. Since JSX elements are only descriptions, it can also render them again to retry, and give up on slow ones after a timeout. It passes this.signal to its children, which aborts on timeout, so components stop waiting for data nobody needs anymore.

A dashboard which never breaks

Async components render one after another, so four boundaries would wait for each other. <Parallel> starts all its children at once and streams them in order. Reload to run it again.

Weatherok

21 °C, sunny

Rendered on the first try.
Stocksfallback

Unavailable right now

The stock API answered with status 503.
Recommendationsfallback

Unavailable right now

Took longer than 800 ms.
Newsretried

Jeasx ships another release

Succeeded on attempt 3 after two failures.

0.80 s for all four widgets: they loaded in parallel, and the slow one was cut off after 800 ms.

Nested boundaries

The inner boundary catches the failing comments, so the article around them still shows. A failure the inner boundary doesn't catch would end up in the outer one.

Why HTML first?

Because the fastest JavaScript is the one you don't send.

Comments are unavailable right now.