Skip to main content
Version: 3.1.1

📦 插件客户端重定向

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

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

production only

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

warning

尽可能使用服务器端重定向更好。

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

安装

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

配置

接受的字段:

OptionTypeDefaultDescription
fromExtensionsstring[][]The extensions to be removed from the route after redirecting.
toExtensionsstring[][]The extensions to be appended to the route after redirecting.
redirectsRedirectRule[][]The list of redirect rules.
createRedirectsCreateRedirectsFnundefinedA callback to create a redirect rule. Docusaurus query this callback against every path it has created, and use its return value to output more paths.
note

此插件还将读取siteConfig.onDuplicateRoutes配置,以在多个文件将输出到同一位置时调整其日志记录级别。

类型

RedirectRule

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

“从”和“到”的概念在这个插件中是核心。“从”意味着你想要创建的路径,即一个将被写入的额外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
},
},
],
],
};