跳到主要内容
版本:3.8.1

📦 客户端重定向插件

用于生成客户端重定向的 Docusaurus 插件。

此插件将向您的静态网站写入额外的 HTML 页面,这些页面会使用 JavaScript 将用户重定向到您现有的 Docusaurus 页面。

仅限生产环境

此插件在开发环境中始终处于非活动状态,且仅在生产环境中激活,因为它作用于构建输出。

警告

在可能的情况下,最好使用服务器端重定向。

在使用此插件之前,您应该查看您的托管服务提供商是否提供了此功能。

安装

npm install --save @docusaurus/plugin-client-redirects

配置

接受的字段

选项类型默认值描述
fromExtensionsstring[][]重定向后要从路由中删除的扩展名。
toExtensionsstring[][]重定向后要附加到路由的扩展名。
redirectsRedirectRule[][]重定向规则列表。
createRedirectsCreateRedirectsFnundefined用于创建重定向规则的回调函数。Docusaurus 会对它创建的每个路径查询此回调,并使用其返回值输出更多路径。
注意

当多个文件将被发送到同一位置时,此插件还将读取siteConfig.onDuplicateRoutes配置来调整其日志级别。

类型

RedirectRule

type RedirectRule = {
to: string;
from: string | string[];
};
注意

此插件的核心思想是“从(from)”和“到(to)”。“从”指您希望创建的路径,即要写入的额外 HTML 文件;“到”指您希望重定向的路径,通常是 Docusaurus 已知的路由。

这就是为什么您可以为同一个“到”路径设置多个“从”路径:我们将创建多个 HTML 文件,它们都重定向到同一个目标。另一方面,一个“从”路径永远不能有多个“到”路径:写入的 HTML 文件需要有一个确定的目标。

CreateRedirectsFn

// The parameter `path` is a route that Docusaurus has already created. It can
// be seen as the "to", and your return value is the "from". Returning a falsy
// value will not create any redirect pages for this particular path.
type CreateRedirectsFn = (path: string) => string[] | string | null | undefined;

示例配置

这是一个配置示例

docusaurus.config.js
export default {
plugins: [
[
'@docusaurus/plugin-client-redirects',
{
fromExtensions: ['html', 'htm'], // /myPage.html -> /myPage
toExtensions: ['exe', 'zip'], // /myAsset -> /myAsset.zip (if latter exists)
redirects: [
// /docs/oldDoc -> /docs/newDoc
{
to: '/docs/newDoc',
from: '/docs/oldDoc',
},
// Redirect from multiple old paths to the new path
{
to: '/docs/newDoc2',
from: ['/docs/oldDocFrom2019', '/docs/legacyDocFrom2016'],
},
],
createRedirects(existingPath) {
if (existingPath.includes('/community')) {
// Redirect from /docs/team/X to /community/X and /docs/support/X to /community/X
return [
existingPath.replace('/community', '/docs/team'),
existingPath.replace('/community', '/docs/support'),
];
}
return undefined; // Return a falsy value: no redirect created
},
},
],
],
};