英文:
Is it possible to build a static-only (SSG) site with Gatsby, disabling SSR?
问题
在Gatsby文档中提到,默认的构建模式是SSG:
SSG是Gatsby的默认渲染模式。虽然名称中包含"static"一词,但这并不意味着它无聊或无生气。它只是意味着整个站点在构建时被预渲染为HTML、CSS和JavaScript,然后作为静态资源提供给浏览器。
但似乎在构建时,组件和库必须支持SSR,并且在使用仅客户端的库时,需要使用解决方法。
从文档中看,渲染有三个选项:
如果我不想使用SSR,只想提供Gatsby站点的静态SSG版本,是否有选项来构建纯静态站点,像Vite或Create React App一样的客户端站点,而不会出现类似于服务器端渲染错误的投诉?
错误#95312 HTML编译
"window"在服务器端渲染期间不可用。在"gatsby develop"期间启用"DEV_SSR"以进行调试。
英文:
In the Gatsby documentation it says that the default build mode is SSG:
> SSG is the default rendering mode in Gatsby. While the name has the word “static” in it, it doesn’t at all mean boring or lifeless. It simply means the entire site is pre-rendered into HTML, CSS, and JavaScript at build time, which then get served as static assets to the browser.
But it seems that when you build it, the compontents and libraries have to be SSR friendly, and you need to use workarounds when using client-only libraries.
From the documentation it seems like there are three options for rendering:
What if I am not interested in using SSR and just want to serve the static SSG version of a Gatsby site. Is there an option to build a purely static site, client-side site like Vite or Create React App and not have it complain about server-side rendering errors like this?
failed Building static HTML for pages - 1.639s
ERROR #95312 HTML.COMPILATION
"window" is not available during server-side rendering. Enable "DEV_SSR" to debug this during "gatsby develop".
答案1
得分: 1
构建静态网站和进行服务器端渲染几乎是相同的事情。主要区别在于何时执行它(在构建时而不是按需执行)。
生成要交付给客户端的HTML的代码仍然必须执行,它仍然必须在window
不可用的环境中运行。
所以不,你仍然需要进行一些变通,以便只在客户端上运行的代码仅在客户端上运行。
英文:
Building a static site and doing server-side rendering are very nearly the same thing. The primary difference is when it is done (at build time instead of on demand).
The code to generate the HTML to be delivered to the client still has to be executed, and it still has to run in an environment where window
is not available.
So no. You still need to do the workaround so that the code which can only run on the client is only run on the client.
答案2
得分: 0
我使用的是修改过的 gatsby-plugin-no-javascript 版本,用于构建一个去除了 gatsby react 脚本的静态网站。
英文:
I use a modified version of gatsby-plugin-no-javascript to build to a static site with gatsby react scripts removed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论