Avoiding aditional requests on dynamic path pages that are client components #80077
-
ProblemI have a page like this: // /example/[id]/page.tsx
"use client";
export default function Page() {
return <>{/* a static component that reads from a context passed from the layout */}</>
} Event thought the page represents a static component, the request is made to each id, like So for each id the user navigates, a new request would be made to fetch the RSC payload. This is inefficient compared with pages dir, where the request would be made once to Is there a way to get this pages dir behavior on the app dir? Example and relevant codeNote the speed on navigation of pages dir vs app dir, since there aren't any additional request after the page loads, vs the app dir that does a request on navigation. Use caseI have multiple small pages that don't load a lot of data. It's “cheaper” to load the entire dataset on the layout, and consume the corresponding data with React Context. Something like this, where the layout is a server component, and both pages are client components. layout.tsx # <Provider value={await db.collect()} />
page.tsx # data = useContext(Provider)
[id]/page.tsx # data = useContext(Provider).filter(v.id = useParams().id) ?? notfound() The idea is to make navigation between page.tsx and all [id]/page.tsx instant. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, Yeah, as far as I know, the Next.js is working on some improvements, but you can also do this: In short, it is not ideal because |
Beta Was this translation helpful? Give feedback.
Hi,
Yeah, as far as I know, the Next.js is working on some improvements, but you can also do this:
In short, it is not ideal because
useParams
misbehaves, butusePathname
works fine.