跳到主要内容
版本:2.4.3

搜索引擎优化 (SEO)

Docusaurus支持以多种方式进行搜索引擎优化。

Global metadata

Provide global meta attributes for the entire site through the site configuration. The metadata will all be rendered in the HTML <head> using the key-value pairs as the prop name and value.

docusaurus.config.js
module.exports = {
themeConfig: {
metadata: [{name: 'keywords', content: 'cooking, blog'}],
// This would become <meta name="keywords" content="cooking, blog"/> in the generated HTML
},
};

Docusaurus 添加了一些开箱即用的元数据。 For example, if you have configured i18n, you will get a hreflang alternate link.

To read more about types of meta tags, visit the MDN docs.

Single page metadata

Similar to global metadata, Docusaurus also allows for the addition of meta-information to individual pages. Follow this guide for configuring the <head> tag. 一言以蔽:

my-markdown-page.md
# A cooking guide

<head>
<meta name="keywords" content="cooking, blog"/>
</head>

Some content...

Docusaurus automatically adds description, title, canonical URL links, and other useful metadata to each Markdown page. 可以用前言进行配置:

---
title: 给搜索引擎看的标题;可能和正文标题不一样
description: 关于此页的简短描述
image: 在社交媒体卡片中显示的缩略图
keywords: [描述, 中心的, 关键词]
---

When creating your React page, adding these fields in Layout would also improve SEO.

提示

Prefer to use front matter for fields like description and keywords: Docusaurus will automatically apply this to both description and og:description, while you would have to manually declare two metadata tags when using the <head> tag.

For JSX pages, you can use the Docusaurus <Head> component.

my-react-page.jsx
import React from 'react';
import Layout from '@theme/Layout';
import Head from '@docusaurus/Head';

export default function page() {
return (
<Layout title="Page" description="A React page demo">
<Head>
<meta property="og:image" content="image.png" />
</Head>
{/* ... */}
</Layout>
);
}
提示

For convenience, the default theme <Layout> component accept title and description as props.

Static HTML generation

Docusaurus 是一个静态网站生成器——每个 URL 路径都会静态生成对应的 HTML 文件,这有助于搜索引擎更容易地找到你的内容。

Image meta description

图片的 alt 标签会告诉搜索引擎图片的内容,同时它也用在图片无法通过视觉被观看时,比如使用屏幕阅读器,或者图片损坏时。 Markdown 广泛地支持 alt 标签。

你也可以给图片添加一个 title——这不太会影响 SEO,不过会在鼠标悬停在图片上的时候显示浮窗,通常用于提供提示信息。

![Docusaurus banner](./assets/docusaurus-asset-example-banner.png '图片标题')
http://localhost:3000

Docusaurus banner

Rich search information

Docusaurus blogs support rich search results out-of-the-box to get maximum search engine experience. 这些信息是基于你的博客和全局配置中的元数据创建的。 为了能享受丰富搜索信息,你需要填写每篇帖子的发布日期、作者和缩略图等信息。 Read more about the meta-information here.

Robots file

A robots.txt file regulates search engines' behavior about which should be displayed and which shouldn't. You can provide it as static asset. 下面的例子将会允许对所有子页面的所有请求:

static/robots.txt
User-agent: *
Disallow:

Read more about the robots file in the Google documentation.

注意

Important: the robots.txt file does not prevent HTML pages from being indexed.

To prevent your whole Docusaurus site from being indexed, use the noIndex site config. Some hosting providers may also let you configure a X-Robots-Tag: noindex HTTP header (GitHub Pages does not support this).

To prevent a single page from being indexed, use <meta name="robots" content="noindex"> as page metadata. Read more about the robots meta tag.

Sitemap file

Docusaurus provides the @docusaurus/plugin-sitemap plugin, which is shipped with preset-classic by default. It autogenerates a sitemap.xml file which will be available at https://example.com/[baseUrl]/sitemap.xml after the production build. 站点地图信息可以帮助搜索引擎爬虫更准确地爬取你的网站。

提示

The sitemap plugin automatically filters pages containing a noindex robots meta directive.

For example, /examples/noIndex is not included in the Docusaurus sitemap.xml file because it contains the following page metadata:

<head>
<meta name="robots" content="noindex, nofollow" />
</head>

Docusaurus uses your file names as links, but you can always change that using slugs, see this tutorial for more details.

Structured content

Search engines rely on the HTML markup such as <h2>, <table>, etc., to understand the structure of your webpage. When Docusaurus renders your pages, semantic markup, e.g. <aside>, <nav>, <main>, are used to divide the different sections of the page, helping the search engine to locate parts like sidebar, navbar, and the main page content.

Most CommonMark syntaxes have their corresponding HTML tags. 在项目中始终使用 Markdown 可以帮助搜索引擎更容易理解页面的内容。