Nextjs Error boundary with SSR (page router)

Currently when an error occurs while nextjs is rendering a page on the server side I can see in the browser network tab a document request that is getting a status 500 response and a white page with the message Application error: a client-side exception has occurred (see the browser console for more information)..

I am using nextjs 14 and the page router.

For better understanding here is an example of my page/component structure:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>export default function page(props: pageProps) {
return (
<div>
{/* MyComponent1 and Mycomponent2 do not
have any errors in it or its child components */}
<MyComponent1 />
<MyComponent2 />
{/* MyComponent3 has an error in it or its
child components */}
<MyComponent3 />
{/* Since in MyComponent3 is an error I want to
show an error component instead of MyComponent3
while this is rendered on the server */}
</div>
);
}
</code>
<code>export default function page(props: pageProps) { return ( <div> {/* MyComponent1 and Mycomponent2 do not have any errors in it or its child components */} <MyComponent1 /> <MyComponent2 /> {/* MyComponent3 has an error in it or its child components */} <MyComponent3 /> {/* Since in MyComponent3 is an error I want to show an error component instead of MyComponent3 while this is rendered on the server */} </div> ); } </code>
export default function page(props: pageProps) {
  return (
    <div>
      {/* MyComponent1 and Mycomponent2 do not 
        have any errors in it or its child components */}
      <MyComponent1 />
      <MyComponent2 />
      {/* MyComponent3 has an error in it or its
        child components */}
      <MyComponent3 /> 
      {/* Since in MyComponent3 is an error I want to 
        show an error component instead of MyComponent3 
        while this is rendered on the server */}
    </div>
  );
}

With my example above while nextjs renders a page with this component on it on the server side the whole page will result in the nextjs error page even though the error only happened in MyComponent3. All other parts are working and do not contain any error.

My goal is to still get the HTML document from nextjs with the working parts of <MyComponent1> and <MyComponent2> but an error component rendered instead of <MyComponent3>.

I am aware that react has Error boundaries, but these error boundaries are not working on the serverside while SSR. I tried to use try/catch where I wrap the error prone code into the try and render a fallback component in the catch block. This seems to work within a single component but I want to wrap multiple components and its children at once. If I try to make a HOC like for example withSsrErrorBoundary and the try/catch block in it the thrown errors in the wrapped component/s are not reaching the catch block of my HOC. I am trying to prevent writing try/catch blocks manually everywhere into my codebase to reach my goal of rendering fallback components on the server side.

Is there any way to get error boundaries or an alternative working like I described above with SSR?

From the nextjs docs the App router error handling documentation page does exactly what i am looking for. But I need that for the page router.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật