跳到主要内容

📦 plugin-client-redirects

Docusaurus Plugin to generate client-side redirects.

This plugin will write additional HTML pages to your static site that redirect the user to your existing Docusaurus pages with JavaScript.

production only

This plugin is always inactive in development and only active in production because it works on the build output.

警告

最好是使用服务器端的重定向功能。

在使用此插件前,你应该查看一下服务提供商是否未提供重定向功能。

安装

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

配置

Accepted fields:

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.
备注

This plugin will also read the siteConfig.onDuplicateRoutes config to adjust its logging level when multiple files will be emitted to the same location.

Types

RedirectRule

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

The idea of "from" and "to" is central in this plugin. "From" means a path that you want to create, i.e. an extra HTML file that will be written; "to" means a path to want to redirect to, usually a route that Docusaurus already knows about.

This is why you can have multiple "from" for the same "to": we will create multiple HTML files that all redirect to the same destination. On the other hand, one "from" can never have more than one "to": the written HTML file needs to have a determinate destination.

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;

Example configuration

Here's an example configuration:

docusaurus.config.js
module.exports = {
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
},
},
],
],
};