How can i trigger suspense when search param change in NexJS 16 with cache component #86034
Replies: 2 comments 2 replies
-
|
I think you can do this trick: // src/app/blog/page.tsx
async function SearchResultsContainer({
searchParams,
}: {
searchParams: Promise<{category?: string}>;
}) {
const {category} = await searchParams;
return (
<Suspense key={category} fallback={<BlogPostsSkeleton />}>
<SearchResults category={category} />
</Suspense>
);
}
export default function BlogPage({searchParams}: {searchParams: Promise<{category?: string}>}) {
return (
<div>
<CategoryFilter />
<Suspense fallback={<BlogPostsSkeleton />}>
<SearchResultsContainer searchParams={searchParams} />
</Suspense>
</div>
);
}It's like you want to show some fallback for the searchParam access and then also for when you do work within the SearchResults - that one has to be keyed. This is a client component version of it: #85278 (reply in thread) Also I think it is worth exploring past the workshop: // /blog/[[...category]]/page.tsx
import {getBlogPosts} from "@/api";
import BlogPosts from "@/components/blog-posts";
export default async function BlogPage(props: PageProps<"/blog/[[...category]]">) {
const {category} = await props.params;
const posts = await getBlogPosts(typeof category === "string" ? category : undefined);
return <BlogPosts posts={posts} />;
}This is really just defining the fallback UI for Suspense boundary that wraps your page (if you don't define it then nothing is wrapped IIRC) // /blog/[[...category]]/loading.tsx
import {BlogPostsSkeleton} from "@/components/blog-posts";
export default function Loading() {
return <BlogPostsSkeleton />;
}And // /blog/layout.tsx
import {getCategories} from "@/api";
import CategoryFilter from "@/components/category-filter";
export default async function Layout({children}: LayoutProps<"/blog">) {
"use cache";
const categories = await getCategories();
return (
<div className="container mx-auto flex flex-col gap-8 px-4 py-8">
<header>
<h1 className="mb-4 text-4xl font-bold">Blog Posts</h1>
<p className="text-muted-foreground">Discover our latest articles and insights</p>
</header>
<CategoryFilter categories={categories} />
{children}
</div>
);
}Screen.Recording.2025-11-12.at.21.03.51.mov |
Beta Was this translation helpful? Give feedback.
-
|
once you add a Cache Components only work as expected when the first Suspense boundary is the one you place in the page/layout. If So in your case:
So the rule of thumb is: Cache Components + route-level loading.tsx conflict unless the cached UI lives in a parent layout. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am following the Next conf project (https://github.com/vercel-solutions/nextjs16-conf-workshop) to learn cache component. i want re-trigger suspense when my searchPrams change for loading UI
And i try to add key to suspense by await the searchParams but i have error Uncached data was accessed outside of Suspense
This is my code:
I already try to use "use cache: private" for using search params but it not re-trigger the suspense
Thanks for help me.
Beta Was this translation helpful? Give feedback.
All reactions