Deployment
Para construir os arquivos estáticos do seu site para produção, execute:
- npm
- Yarn
- pnpm
npm run build
yarn build
pnpm run build
Quando terminar, os arquivos estáticos serão gerados dentro do diretório build
.
A única responsabilidade do Docusaurus é construir seu site e emitir arquivos estáticos na build
.
Agora cabe a você escolher como hospedar esses arquivos estáticos.
Você pode fazer o deploy do seu site para serviços estáticos de hospedagem como Vercel, GitHub Pages, Netlify, Render, Surge...
Um site Docusaurus é renderizado estaticamente e geralmente pode funcionar sem JavaScript!
Configuração
The following parameters are required in docusaurus.config.js
to optimize routing and serve files from the correct location:
Nome | Descrição |
---|---|
url | URL for your site. For a site deployed at https://my-org.com/my-project/ , url is https://my-org.com/ . |
baseUrl | Base URL for your project, with a trailing slash. For a site deployed at https://my-org.com/my-project/ , baseUrl is /my-project/ . |
Testando localmente sua Construção
It is important to test your build locally before deploying it for production. Docusaurus provides a docusaurus serve
command for that:
- npm
- Yarn
- pnpm
npm run serve
yarn serve
pnpm run serve
By default, this will load your site at http://localhost:3000/
.
Configuração de barra
Docusaurus has a trailingSlash
config, to allow customizing URLs/links and emitted filename patterns.
O valor padrão geralmente funciona bem. Infelizmente, cada provedor de hospedagem estática tem um comportamento diferente e implantar exatamente o mesmo site em vários hosts pode levar a resultados distintos. Dependendo do seu host, pode ser útil alterar essa configuração.
Use slorber/trailing-slash-guide para entender melhor o comportamento do seu host e configurar trailingSlash
adequadamente.
Using environment variables
Putting potentially sensitive information in the environment is common practice. 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... are client side and do not have direct access to the process
global. In this case, you can consider using customFields
to pass environment variables to the client side.
// If you are using dotenv (https://www.npmjs.com/package/dotenv)
require('dotenv').config();
module.exports = {
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>Contact us through {customFields.teamEmail}!</div>;
}
Choosing a hosting provider
There are a few common hosting options:
- Self hosting with an HTTP server like Apache2 or Nginx;
- Jamstack providers, e.g. Netlify and Vercel. We will use them as references, but the same reasoning can apply to other providers.
- GitHub Pages. (By definition, it is also Jamstack, but we compare it separately.)
If you are unsure of which one to choose, ask the following questions:
How much resource (person-hours, money) am I willing to invest in this?
- 🔴 Self-hosting is the hardest to set up—you would usually need an experienced person to manage this. Cloud services are almost never free, and setting up an on-site server and connecting it to the WAN can be even more costly.
- 🟢 Jamstack providers can help you set up a working website in almost no time and offers features like server-side redirects that are easily configurable. Many providers offer generous build time quotas even for free plans that you would almost never exceed. However, it's still ultimately limited—you would need to pay once you hit the limit. Check the pricing page of your provider for details.
- 🟡 The GitHub Pages deployment workflow can be tedious to set up. (Evidence: see the length of Deploying to GitHub Pages!) However, this service (including build and deployment) is always free for public repositories, and we have detailed instructions to help you make it work.
How much server-side configuration would I need?
- 🟢 With self-hosting, you have access to the entire server's configuration. You can configure the virtual host to serve different content based on the request URL; you can do complicated server-side redirects; you can put part of the site behind authentication... If you need a lot of server-side features, self-host your website.
- 🟡 Jamstack usually offers some server-side configurations, e.g. URLs formatting (trailing slashes), server-side redirects...
- 🔴 GitHub Pages doesn't expose server-side configurations besides enforcing HTTPS and setting CNAME.
Do I have needs to cooperate?
- 🟡 Self-hosted services can achieve the same effect as Netlify, but with much more heavy-lifting. Usually, you would have a specific person who looks after the deployment, and the workflow won't be very git-based as opposed to the other two options.