部署
要在生产环境中构建网站的静态文件,请运行:
- npm
- Yarn
- pnpm
npm run build
yarn build
pnpm run build
完成后,静态文件会被生成在 build
目录中。
Docusaurus 只负责构建站点,然后把静态文件输出到 build
文件夹。
现在,该由你来决定怎么托管这些静态文件了。
You can deploy your site to static site hosting services such as Vercel, GitHub Pages, Netlify, Render, and Surge.
Docusaurus 网站是静态渲染的,而且一般不需要 JavaScript 也能运行!
配置
在 docusaurus.config.js
中,下面这些参数是必填的,让 Docusaurus 能够优化路由,并从正确的位置加载文件:
参数 | 描述 |
---|---|
url | 站点 URL。 如果网站部署在 https://my-org.com/my-project/ , url 就是 https://my-org.com/ |
baseUrl | 站点的 base URL,带有末尾斜杠。 如果网站部署在 https://my-org.com/my-project/ , baseUrl 就是 /my-project/ |
本地测试构建
在部署到生产环境前,事先进行本地测试尤为重要。 Docusaurus 提供了 docusaurus serve
命令来测试:
- npm
- Yarn
- pnpm
npm run serve
yarn serve
pnpm run serve
By default, this will load your site at http://localhost:3000/
.
末尾斜杠配置
Docusaurus has a trailingSlash
config to allow customizing URLs/links and emitted filename patterns.
你一般不需要修改默认值。 遗憾的是,每家静态托管商的行为都不一样,而把同一网站部署到不同服务商的结果可能大相径庭。 根据你的托管商的不同,你可能需要修改此配置。
要更好地了解你的托管商的行为,可以参见 slorber/trailing-slash-guide,并依此配置 trailingSlash
选项。
使用环境变量
把可能敏感的信息放在环境变量中的做法很常见。 However, in a typical Docusaurus website, the docusaurus.config.js
file is the only interface to the Node.js environment (see our architecture overview), while everything else (MDX pages, React components, etc.) are client side and do not have direct access to the process
global variable. 在这种情况下,你可以考虑使用 customFields
将环境变量传递给客户端。
// If you are using dotenv (https://www.npmjs.com/package/dotenv)
import 'dotenv/config';
export default {
title: '...',
url: process.env.URL, // You can use environment variables to control site specifics as well
customFields: {
// Put your custom environment here
teamEmail: process.env.EMAIL,
},
};
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
export default function Home() {
const {
siteConfig: {customFields},
} = useDocusaurusContext();
return <div>通过 {customFields.teamEmail} 联系我们!</div>;
}
选择托管服务商
有几种常见的托管选择:
- Self hosting with an HTTP server like Apache2 or Nginx.
- Jamstack providers (e.g. Netlify and Vercel). 我们会以它们为参考,但同样的道理也可以适用于其他提供商。
- GitHub Pages (by definition, it is also Jamstack, but we compare it separately).
如果你不清楚选择哪一个,问自己下面几个问题:
How many resources (money, person-hours, etc.) am I willing to invest in this?
- 🔴 Self-hosting requires experience in networking as well as Linux and web server administration. It's the most difficult option, and would require the most time to manage successfully. Expense-wise, cloud services are almost never free, and purchasing/deploying an onsite server can be even more costly.
- 🟢 Jamstack 提供商可以帮助你建立一个运转良好的网站,几乎不需要时间,并且很容易配置功能,比如服务端重定向。 Many providers offer generous build-time quotas even for free plans that you would almost never exceed. However, free plans have limits, and you would need to pay once you hit those limits. 要了解详情,请查看你的提供商的定价页面。
- 🟡 GitHub Pages 部署的工作流程设置起来可能很麻烦。 (不信的话,可以看看部署到 GitHub Pages 部分的长度!) 但是,这项服务(包括构建和部署)对所有公共仓库都永久免费,并且我们也有详细教程,帮助你正确运行它。
How much server-side customization do I need?
- 🟢 自行托管时,你可以控制整个服务器的配置。 You can configure the virtual host to serve different content based on the request URL, you can do complicated server-side redirects, you can implement authentication, and so on. 如果你需要很多服务器端功能,请选择自行托管网站。
- 🟡 Jamstack usually offers some server-side configuration (e.g. URL formatting (trailing slashes), server-side redirects, etc.).
- 🔴 GitHub Pages doesn't expose server-side configuration besides enforcing HTTPS and setting CNAME records.
Do I need collaboration-friendly deployment workflows?
- 🟡 Self-hosted services can leverage continuous deployment functionality like Netlify, but more heavy-lifting is involved. Usually, you would designate a specific person to manage the deployment, and the workflow wouldn't be very git-based as opposed to the other two options.
- 🟢 Netlify 和 Vercel 对每个 Pull Request 都会生成部署预览,这对于在合并到生产环境之前的团队审核工作非常有用。 你也可以做团队管理,不同成员拥有不同的部署访问权限。
- 🟡 GitHub 页面不能做部署预览,至少方法非常复杂。 每个仓库只能和一个站点部署相关联。 另一方面,你还是可以控制哪些人有站点部署的写权限。
不存在通用方案。 你需要权衡你的需求和资源,然后再做决定。