Reputation: 55
I'm learning nextjs and read in some places that nextjs only prerenders the first page and in some other places they say nextjs prerenders all pages by default so I cant understand which one is true
Upvotes: 4
Views: 2102
Reputation: 1843
There are different rendering stategies. You can select which one NextJs will choose for each page by adding a method in your page file.
getStaticProps will prerender an HTML file ate the build time, you can still have a useEffect to hydrate the content of the page.
getStaticPath is kinda same but you can prerender differents pages for different routes, or on demand on non existing paths if fallback: 'blocking'
is provided
You can also go form ISR (Incremental Static Regeneration), an inbetween Static/SSR, where pages are generated on the demand and cached for the amount of time specified by adding revalidate
.
getServerSideProps will render the page on demand
Every one of this rendering strategies have strength and drawbacks, static pages are useful for first render but can need another round trip to hydrate content. Server render is usefull for SEO but can yield to higher server CPU usage.
What is nice is that you can choose which strategy to employ depending on the page you render.
Edit: I did not add it but the principe of these methods is that you fetch data/do stuff in them, and their return statement will be consumed by the page they live in as parameters.
Upvotes: 2