dynamic routes with server side rendering.
Answered
English Spot posted this in #help-forum
English SpotOP
Hello, if I am creating an app where users can create posts, and then those posts can bee retrived by
app.com/p/[id], how can I fetch the data based on the id and return the rendered page on the server?Answered by joulev
but in this case, i would recommend just rendering everything dynamically yes
11 Replies
@English Spot Hello, if I am creating an app where users can create posts, and then those posts can bee retrived by `app.com/p/[id]`, how can I fetch the data based on the id and return the rendered page on the server?
app router? use this params prop and retrieve the post data server-side https://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional
@joulev app router? use this params prop and retrieve the post data server-side <https://nextjs.org/docs/app/api-reference/file-conventions/page#params-optional>
English SpotOP
Could you give an example of getting the data server-side? is it as easy as literally fetching the data within the file? anythiing not marked
use client is server rendered right? I think I anwsered my own question but lol@English Spot Could you give an example of getting the data server-side? is it as easy as literally fetching the data within the file? anythiing not marked `use client` is server rendered right? I think I anwsered my own question but lol
yeah basically...
export default async function Page({ params }) {
const post = await db.getPost({ id: params.id });
if (!post) notFound(); // render 404 page
return <div>{post.title}</div>;
}you may also want to use
generateStaticParams (https://nextjs.org/docs/app/api-reference/functions/generate-static-params)@joulev you may also want to use `generateStaticParams` (<https://nextjs.org/docs/app/api-reference/functions/generate-static-params>)
English SpotOP
Wouldn't
generateStaticParams only gen the items in the db at the time of building the page?like if users are activitly creating posts, and there are thousands of them?
@English Spot like if users are activitly creating posts, and there are thousands of them?
but in this case, i would recommend just rendering everything dynamically yes
Answer
no need of
generateStaticParams@joulev yeah basically...
tsx
export default async function Page({ params }) {
const post = await db.getPost({ id: params.id });
if (!post) notFound(); // render 404 page
return <div>{post.title}</div>;
}
in your case then just use something like this with
export const revalidate = 0 at the bottomEnglish SpotOP
okay thank you very much 
