I am trying to wrap my head around how to conveniently work with data fetching in Next.js 15 (app router) using Suspense and ErrorBoundary.
Below please find my current approach. This works fine. But it seems like a lot of boilerplate to write. In particular, the data fetching has to occur in a child component of the suspense boundary.
Is this how people normally do this? I'm assuming it's not intended to fetch the data in page.tsx itself?
page.tsx
export default async function ArticleListPage() {
return (
<main>
<h1>Articles</h1>
<ErrorBoundary fallback= {<> Error: Unable to load articles.</>}>
<Suspense fallback={<>Loading...</>}>
<ArticleList />
</Suspense>
</ErrorBoundary>
</main>
);
}
ArticleList.tsx
export async function ArticleList() {
const articles = await fetchAllArticles();
return (
<>
{ articles.map((article: any) => (
<Link
key={article.slug}
href={`/articles/${article.slug}`}
>
<img src={article.cover} />
<h2>{article.title}</h2>
<p>{article.description}</p>
</Link>
))}
</>
);
}
ErrorBoundarycomponent look like? I found one example w/ a class component and this doesn't work with server components :(