Handling top-level data fetching in Next.js 16 with cacheComponents and “Uncached data accessed outside of <Suspense>” error #85608
-
SummaryI'm using Next.js 16 with cacheComponents enabled. I have a page where I need to fetch data at the top level, for example: export default async function Profile({ params }) {
const { id } = await params;
const user = await fetchUser(id);
if (!user) {
notFound();
}
// ...
}In this case, since data fetching happens directly at the top level of the component, I can’t wrap it in a Next.js then throws the following error: So, should this error be avoided in cases like this? Or is it safe to ignore when top-level data fetching is required? I know that with this data fetching pattern, the route is considered a What’s the recommended approach for handling this scenario in Next.js 16, especially when component caching is active? Additional informationNo response ExampleNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
|
You could use a async function Profile({ userId }) {
// if you pass params keep it as you had it
const id = await userId;
const user = await fetchUser(id);
if (!user) {
notFound();
}
// ...
}
export default async function Page({ params }) {
return (
<>
<h1>Profile page</h1>
<Suspense fallback={<p>Loading data...</p>}>
{/* or just pass params */}
<Profile userId={params.then(p => p.id)} />
</Suspense>
</>
)
} |
Beta Was this translation helpful? Give feedback.
I think you can use the placeholder trick: https://nextjs.org/docs/messages/empty-generate-static-params#option-2-use-a-placeholder-param-for-validation
So
ifthe userId is the placeholder, orno user, thennotFound()- I tried a bit locally, and I see 404 Not Found w/ cURL.