静态网站生成 (SSG)
In architecture, we mentioned that the theme is run in Webpack. 但是要注意,这并不代表它总是可以访问到浏览器的全局变量! 主题会被构建两次:
- During server-side rendering, the theme is compiled in a sandbox called React DOM Server. You can see this as a "headless browser", where there is no
window
ordocument
, only React. 服务端渲染 (SSR) 会生成静态 HTML 页面。 - During client-side rendering, the theme is compiled to JavaScript that gets eventually executed in the browser, so it has access to browser variables.
SSR or SSG?
Server-side rendering and static site generation can be different concepts, but we use them interchangeably.
严格来说,Docusaurus 是一个静态站点生成器,因为我们没有服务器端的运行时——我们静态渲染 HTML 文件,然后部署在 CDN 上,而不是针对每个请求动态预渲染。 This differs from the working model of Next.js.
Therefore, while you probably know not to access Node globals like process
(or can we?) or the 'fs'
module, you can't freely access browser globals either.
import React from 'react';
export default function WhereAmI() {
return <span>{window.location.href}</span>;
}
This looks like idiomatic React, but if you run docusaurus build
, you will get an error:
ReferenceError: window is not defined