Next.js Discord

Discord Forum

dynamic routes with server side rendering.

Answered
English Spot posted this in #help-forum
Open in Discord
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
View full answer

11 Replies

@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>;
}
@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
English SpotOP
okay thank you very much :ty: