I'm learning Next js and I try to figure out the difference between static generation and ssr. Then I saw this sentence in their doc but I can't understand: "On the other hand, Static Generation is not a good idea if you cannot pre-render a page ahead of a user's request"
CodePudding user response:
Static site generation is used when you want to get data on build time and use it in your site. On the other side, server side rendering, retrieves data from server on each user request. For clarification, Imagine these scenarios:
You have a profile page in your website, which holds user id as URL parameter, for example this is profile URL of user with id = 1:
/profile/1
. You may want to show a 404 page if user with this id does not exist. So you need to query your database on each request to check whether the user exist. This is when SSR (Server-side rendering) is a good idea. You can do it by exporting a function calledgetServersideProps
You may have a collection in your database which holds information about your site, for example footer typography and etc. which is not changed frequently, You can use static generation in this situation. But be careful, When you use SSG, if you change data that's in databse, You MUST rebuild your application to update data in your site