Interactive Learning Platform Nuxt layer

Isolated learning atmosphere · v4.x

← Back to writing
Step 4 of 7ssrErrorBoundary

Server-Side Error Boundaries That Actually Hold

Enable experimental.ssrErrorBoundary, catch a component that throws while server rendering, retry on the client, and use clearBoundaryError without colliding with the global clearError.

<NuxtErrorBoundary> used to catch errors on the client only. A component that threw while server rendering went straight past it to your error page. In 4.7, the boundary holds on the server too.

What's new#

Enable experimental.ssrErrorBoundary (it is on with future.compatibilityVersion: 5):

nuxt.config.ts
export default defineNuxtConfig({
  experimental: {
    ssrErrorBoundary: true,
  },
});

If a child throws on the server, the response is a 200 with the rest of the page intact and the #error slot in its place. The client hydrates into the same state.

app/pages/index.vue
<template>
  <NuxtErrorBoundary>
    <BrokenWidget />
    <template #error="{ error, clearBoundaryError }">
      <p>Widget unavailable</p>
      <button @click="clearBoundaryError">Retry</button>
    </template>
  </NuxtErrorBoundary>
</template>

There is also a new retry-on-client prop. It serves the #error slot in the HTML, then tries the real content again in the browser. That is the replacement for <NuxtClientFallback>, which is now deprecated.

And the slot exposes clearBoundaryError instead of clearError. That rename matters: if you forgot to destructure the old name, your template called the global clearError and the boundary stayed broken. The new name cannot collide.

Why you'd care#

A marketing page with one recommendation widget should not become a 500 because the widget's data source is down. A boundary is a local failure domain. Before 4.7, that domain stopped at the client, which is exactly where server rendering is supposed to help you.

Now you can ship a page where one section fails, the rest renders, and the user can retry the broken part.

Before#

A server-side throw in <BrokenWidget> reaches your global error page. The whole route is gone.

After#

The boundary catches it, renders the slot, and keeps the page:

txt
HTTP/1.1 200 OK

<header>...</header>
<section>Widget unavailable <button>Retry</button></section>
<footer>...</footer>

In production, errors that are not HTTP errors reach the slot as a plain 500, so server error messages stay out of your HTML.

The client-only case#

Some widgets only work in the browser: a canvas, a map, a library that touches window at import time. retry-on-client is for those:

vue
<NuxtErrorBoundary retry-on-client>
  <ClientOnlyMap />
  <template #error>
    <p>Map loading…</p>
  </template>
</NuxtErrorBoundary>

The server sends the fallback, then the browser tries the real component. No blank page, no hydration mismatch, no <NuxtClientFallback>.

Do it yourself#

  1. Enable experimental.ssrErrorBoundary.
  2. Create a component that throws during setup on the server.
  3. Wrap it in <NuxtErrorBoundary> with an #error slot.
  4. Load the page with JavaScript disabled. The slot should be in the HTML.
  5. Click Retry with JavaScript on. The boundary should clear.
  6. Try retry-on-client on a component that only works in the browser.

Gotchas#

  • A boundary is not a try/catch for data fetching. It catches errors thrown while rendering its subtree. Handle expected API failures with useAsyncData's error ref.
  • Production hides the message. A non-HTTP error becomes a plain 500 in the slot. Do not render error.message to users.
  • clearBoundaryError is the slot name. Destructure it. Do not call the global clearError by accident.
  • <NuxtClientFallback> is deprecated. Replace it with <NuxtErrorBoundary retry-on-client>.
  • Nested boundaries are a design choice. The nearest boundary catches. If a parent and a child both have one, the child wins.
  • 200 is deliberate. The page rendered; one section failed. If the whole page should fail, throw outside the boundary.

Recap#

<NuxtErrorBoundary> now holds on the server, retry-on-client replaces <NuxtClientFallback>, and clearBoundaryError removes the footgun. One section can fail without taking the page with it.

Next: Validated Runtime Config.