简体   繁体   English

Next.js static 服务器端渲染和 Gatsby.js

[英]Next.js static server side rendering and Gatsby.js

I want to build a website with Next.js and am trying to better understand their Automatic Static Optimization , and the different ways that you can use it.我想用 Next.js 建立一个网站,并试图更好地了解他们的Automatic Static Optimization以及您可以使用它的不同方式。
So to start with, there's Gatsby.js which is a static site generator.首先, Gatsby.js是一个 static 站点生成器。 When you run Gatsby's build command you get a /public folder which is completely static and can be deployed without any need for some kind of a back-end.当您运行 Gatsby 的build命令时,您会得到一个完全是 static 的/public文件夹,并且可以在不需要某种后端的情况下进行部署。 If I understand correctly, this means that the entire static folder is sent to the client on the first request, and from then on everything, including routing, happens client-side.如果我理解正确,这意味着整个 static 文件夹在第一次请求时被发送到客户端,从那时起,包括路由在内的一切都发生在客户端。
With Next.js on the other hand, you have static generation, which means that all pages are pre-rendered on the server at build time (like Gatsby), but the application still depends on a back-end (either a full-blown server or a serverless function) for routing.另一方面,使用 Next.js 生成 static,这意味着所有页面在构建时都在服务器上预渲染(如 Gatsby),但应用程序仍然依赖于后端(成熟的服务器或无服务器功能)用于路由。 Ie, the pages are pre-rendered but, unlike Gatsby, they're sent to the client per request, ie on navigation.即,页面是预渲染的,但与 Gatsby 不同的是,它们是按请求发送给客户端的,即在导航时。 (I found this answer which says there's only the initial request with Next, but then, what's the difference from Gatsby?) (我发现这个答案说只有 Next 的初始请求,但是,与 Gatsby 有什么区别?)
What I find confusing in all this, are things like Next's docs for Static HTML Export .我在这一切中感到困惑的是,像 Next 的Static HTML Export的文档。 They start by stating他们首先说明

next export allows you to export your app to static HTML, which can be run standalone without the need of a Node.js server. next export允许您将应用程序导出到 static HTML,它可以独立运行,无需 Node.js 服务器。

So, sounds like this option gives us the ability to use Next just like Gatsby, ie as a completely static folder.所以,听起来这个选项让我们能够像 Gatsby 一样使用 Next,即完全是 static 文件夹。
But then they go on remarking that:但后来他们 go 评论说:

If your pages don't have getInitialProps you may not need next export at all;如果您的页面没有getInitialProps您可能根本不需要next export next build is already enough thanks to Automatic Static Optimization .由于Automatic Static Optimizationnext build已经足够了。

But Automatic Static Optimization refers only to server side static pre-rendering, and next build does not produce a Gatsby like static folder that can be deployed as a standalone.但是自动 Static 优化仅指服务器端 static 预渲染,并且next build不会生成像 static 一样的 Gatsby 文件夹,可以部署为独立的文件夹。
So what am I missing here?那么我在这里错过了什么? What's the difference between Gatsby.js and Next.js? Gatsby.js 和 Next.js 有什么区别? Does Gatsby can do something Next can't?盖茨比能做 Next 做不到的事情吗? Can I build a completely static site with Next without using the export command?我可以在不使用export命令的情况下使用 Next 构建一个完整的 static 站点吗?
Most importantly, can I build and deploy a Next.js application with some pages completely static (like Gatsby), some pages only pre-rendered ( getStaticProps and getStaticPaths ), and some pages rendered server-side ( getServerSideProps )?最重要的是,我能否构建和部署一个 Next.js 应用程序,其中一些页面完全为 static(如 Gatsby),一些页面仅预渲染( getStaticPropsgetStaticPaths ),以及一些页面呈现在服务器端( getServerSideProps )?

Thanks a lot in advance!提前非常感谢!

The first request is for <url>/index.html so no the entire public is not sent to the client.第一个请求是针对<url>/index.html的,因此不会将整个public发送给客户端。

Gatsby optimises the loading process to ensure critical resources (HTML, CSS, JS) are loaded first, ensuring best possible user experience. Gatsby 优化了加载过程,以确保首先加载关键资源(HTML、CSS、JS),从而确保最佳的用户体验。 From there it will load remaining resources required to render the full page and will also prefetch linked pages from the main page.从那里它将加载呈现整个页面所需的剩余资源,并且还将从主页预取链接页面。 Of course if you have requested a route to a different page, then the client would fetch the HTML initially for that page, but the process followed will be similar.当然,如果您已请求到不同页面的路由,则客户端最初会为该页面获取 HTML,但随后的过程将类似。

Gatsby is still better at this than Next.js (SSG is a very new feature for Next and this the core of what Gatsby does) - see https://dev.to/notsidney/gatsby-won-against-next-js-in-this-head-to-head-37ka . Gatsby 在这方面仍然比 Next.js 更好(SSG 是 Next 的一个非常新的功能,这是 Gatsby 所做的核心) - 请参阅https://dev.to/notsidney/gatsby-won-against-next-js-in-这头对头 37ka

In answer to your questions, yes you can do full SSG, partial SSR/SSG and full SSR with Next.在回答您的问题时,是的,您可以使用 Next 进行完整 SSG、部分 SSR/SSG 和完整 SSR。 You need to do next export if you want full SSG, otherwise for the other modes you are in standard Next territory and Next will take care of both SSG/SSR given you have a traditional web server running that can serve both static content and perform dynamic SSR.如果您想要完整的 SSG,则需要进行next export ,否则对于其他模式,您处于标准的 Next 领域,并且 Next 将同时处理 SSG/SSR,因为您运行的是传统的 web 服务器,可以同时提供 static 内容并执行动态固态硬盘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM