1

So I'm finding it difficult to see the benefits of doing SSR for dynamic paths in NextJs when I can just just pre-render a few static paths, and use fallback=true to cover my bases on most pages.

Say I have an eCommerce site with 1 million product detail pages, but I only want to pre-render featured products on the home page(most clicked). If I set fallback to true in getStaticPaths, then the getStaticProps function runs every time a non featured product page is requested.

So what's the advantage of using SSR when I can just have a fallback that queries the database every time a non pre-rendered page is called?

Note: I saw a similar question on Stack Overflow, and the answer was that web-crawlers see only the fallback state of your react Component that you set for non pre-rendered pages (so the source code would only read <p>Loading...</p> or something like that, vs the SSR page which would load all your data for the product directly as the source code. But this doesn't seem to be true in my app.

Thanks for any help.

TLDR: [In NextJs..] Why can't I just use SSG for dynamic paths, with fallback=true in getStaticPaths, instead of SSR?

THANKS ALL

I tried reading the NextJs docs and couldn't find an explanation for the cons of using fallback=true in getStaticPaths

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • What you mention about the crawlers "seeing" the fallback HTML is false. Next.js won't expose this to the crawlers, instead the page will be rendered in blocking mode. Instead of "advantages" you need to think about use cases and page's needs. Is the page supposed to handle dynamic data? Do you need to handle things like sessions server-side? Go for SSR, otherwise, always choose SSG/ISR. – ivanatias Feb 08 '23 at 02:36

1 Answers1

1

From next.js docs:

By default, Next.js pre-renders every page. This means that Next.js generates HTML for each page in advance, instead of having it all done by client-side JavaScript.

Two Forms of Pre-rendering

Next.js has two forms of pre-rendering: Static Generation and Server-side Rendering. The difference is in when it generates the HTML for a page.

  • Static Generation is the pre-rendering method that generates the HTML at build time. The pre-rendered HTML is then reused on each request.

  • Server-side Rendering is the pre-rendering method that generates the HTML on each request.

I put those definitions to clarify the terms in next.js. I believe your question is regarding fallback:true versus generating HTML on each request (or building page runtime vs build time). I think this note you shared is not correct

Note: I saw a similar question on Stack Overflow, and the answer was that web-crawlers see only the fallback state of your react Component that you set for non pre-rendered pages (so the source code would only read

Loading...

or something like that, vs the SSR page which would load all your data for the product directly as the source code. But this doesn't seem to be true in my app.

In each case the populated page is seen by the crawlers.

Using getStaticPath in your e-commerce example is the usage of caching. those pages for popular products are already built and inside next build folder you can see them if you build your app locally. But in large applications, those static assets are stored in CDN, and whenever the server gets a request response will be in no time. so customer will have a better user experience so which will eventually affect the profit of the ecommerce site.

I think the clearest example would be thinking about a blogging website like Medium. The most popular blogs will be pre-generated since the content of the blogs do not change that often. Medium will use CDN's from different parts of the world, so user all around the world will have faster access to blogs.

Hitting databases is a very expensive operation. The more load you have on the database harder to maintain the availability, scalability, and reliability of your application.

Also, you might have a better internet connection, you use high end clients so you might access any data faster but you have to think about all people around the world who try to access data with low-quality clients or internet connections.

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
  • Thank you Yilmaz. This makes a lot of sense. So it seems useful to pre-render as many pages as possible, maybe with ISR to keep things updated, instead of having every user query the database via CSR or SSR. – Evan O'Shea Feb 08 '23 at 18:21