英文:
What's the difference between SSR and fallback = true for dynamic paths in NextJS
问题
所以我发现在NextJs中为动态路径使用SSR时很难看到好处,当我可以只预渲染一些静态路径,并使用fallback=true
来覆盖大多数页面的基础。
假设我有一个包含100万个产品详细页面的电商网站,但我只想在主页(最常点击的页面)上预渲染一些特色产品。如果我在getStaticPaths
中将fallback设置为true,那么每次请求非特色产品页面时,getStaticProps
函数都会运行。
那么,使用SSR的优势是什么,当我可以使用fallback,在每次调用未预渲染页面时查询数据库呢?
注意:我在Stack Overflow上看到一个类似的问题,答案是网络爬虫只看到你为未预渲染页面设置的React组件的fallback状态(因此源代码只会读取<p>Loading...</p>
之类的内容,而不是SSR页面,它会直接加载产品的所有数据作为源代码。但这在我的应用程序中似乎并不成立。
感谢任何帮助。
TLDR:[在NextJs中...] 为什么我不能只是在动态路径中使用SSG,同时在getStaticPaths
中设置fallback=true,而不是使用SSR?
谢谢大家
我尝试阅读NextJs文档,但找不到关于在getStaticPaths
中使用fallback=true的缺点的解释。
英文:
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
答案1
得分: 1
来自Next.js文档:
默认情况下,Next.js对每个页面进行预渲染。这意味着Next.js提前为每个页面生成HTML,而不是由客户端JavaScript完成所有操作。
两种预渲染形式
Next.js有两种预渲染形式:静态生成和服务器端渲染。区别在于生成页面的HTML的时机。
静态生成是在构建时生成HTML的预渲染方法。预渲染的HTML然后在每个请求上重复使用。
服务器端渲染是在每个请求上生成HTML的预渲染方法。
我将这些定义放在这里以澄清Next.js中的术语。我认为您的问题涉及fallback:true
与在每个请求上生成HTML(或在运行时与构建时生成页面)之间的区别。我认为您分享的这个注释是不正确的:
注意:我在Stack Overflow上看到类似的问题,答案是Web爬虫仅看到您为非预渲染页面设置的回退状态(因此源代码只会读取
<p>Loading...</p>
之类的内容,与SSR页面不同,后者将直接加载产品的所有数据作为源代码。但在我的应用中似乎并非如此。
在每种情况下,被爬虫检测到的都是已填充的页面。
在您的电子商务示例中使用getStaticPath
是缓存的用法。那些热门产品的页面已经在本地构建应用程序时创建,并存储在next构建文件夹中。但在大型应用程序中,这些静态资源存储在CDN中,每当服务器收到请求时,响应将迅速返回。因此,客户将获得更好的用户体验,最终会影响电子商务网站的利润。
我认为最清晰的例子是考虑一个类似Medium的博客网站。最受欢迎的博客将被预先生成,因为博客的内容不经常更改。Medium将使用来自世界各地的CDN,以便全球各地的用户能够更快地访问博客。
访问数据库是一项非常昂贵的操作。数据库负载越重,越难以维护应用程序的可用性、可扩展性和可靠性。
此外,您可能拥有更好的互联网连接,使用高端客户端,因此可能更快地访问任何数据,但您必须考虑世界各地的尝试使用低质量客户端或互联网连接访问数据的所有人。
英文:
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 <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.
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论