@docusaurus/plugin-client-redirects 2.0.0-beta.15 → 2.0.0-beta.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/collectRedirects.js +12 -9
- package/lib/createRedirectPageContent.js +2 -2
- package/lib/extensionRedirects.js +5 -11
- package/lib/writeRedirectFiles.js +14 -11
- package/package.json +10 -10
- package/src/collectRedirects.ts +12 -9
- package/src/createRedirectPageContent.ts +2 -2
- package/src/extensionRedirects.ts +5 -12
- package/src/writeRedirectFiles.ts +14 -11
package/lib/collectRedirects.js
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
|
-
const lodash_1 = require("lodash");
|
|
10
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
11
11
|
const extensionRedirects_1 = require("./extensionRedirects");
|
|
12
12
|
const redirectValidation_1 = require("./redirectValidation");
|
|
13
13
|
const utils_common_1 = require("@docusaurus/utils-common");
|
|
@@ -23,9 +23,12 @@ function collectRedirects(pluginContext, trailingSlash) {
|
|
|
23
23
|
}
|
|
24
24
|
exports.default = collectRedirects;
|
|
25
25
|
// If users wants to redirect to=/abc and they enable trailingSlash=true then
|
|
26
|
-
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
27
|
-
//
|
|
28
|
-
//
|
|
26
|
+
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
27
|
+
// path now)
|
|
28
|
+
// => we want to redirect to=/abc/ without the user having to change all its
|
|
29
|
+
// redirect plugin options
|
|
30
|
+
// It should be easy to toggle siteConfig.trailingSlash option without having to
|
|
31
|
+
// change other configs
|
|
29
32
|
function applyRedirectsTrailingSlash(redirects, params) {
|
|
30
33
|
return redirects.map((redirect) => ({
|
|
31
34
|
...redirect,
|
|
@@ -39,8 +42,8 @@ function validateCollectedRedirects(redirects, pluginContext) {
|
|
|
39
42
|
(0, redirectValidation_1.validateRedirect)(redirect);
|
|
40
43
|
return undefined;
|
|
41
44
|
}
|
|
42
|
-
catch (
|
|
43
|
-
return
|
|
45
|
+
catch (err) {
|
|
46
|
+
return err.message;
|
|
44
47
|
}
|
|
45
48
|
})
|
|
46
49
|
.filter(Boolean);
|
|
@@ -51,7 +54,7 @@ function validateCollectedRedirects(redirects, pluginContext) {
|
|
|
51
54
|
}
|
|
52
55
|
const allowedToPaths = pluginContext.relativeRoutesPaths;
|
|
53
56
|
const toPaths = redirects.map((redirect) => redirect.to);
|
|
54
|
-
const illegalToPaths =
|
|
57
|
+
const illegalToPaths = lodash_1.default.difference(toPaths, allowedToPaths);
|
|
55
58
|
if (illegalToPaths.length > 0) {
|
|
56
59
|
throw new Error(`You are trying to create client-side redirections to paths that do not exist:
|
|
57
60
|
- ${illegalToPaths.join('\n- ')}
|
|
@@ -64,13 +67,13 @@ Valid paths you can redirect to:
|
|
|
64
67
|
function filterUnwantedRedirects(redirects, pluginContext) {
|
|
65
68
|
// we don't want to create twice the same redirect
|
|
66
69
|
// that would lead to writing twice the same html redirection file
|
|
67
|
-
Object.entries(
|
|
70
|
+
Object.entries(lodash_1.default.groupBy(redirects, (redirect) => redirect.from)).forEach(([from, groupedFromRedirects]) => {
|
|
68
71
|
if (groupedFromRedirects.length > 1) {
|
|
69
72
|
logger_1.default.error `name=${'@docusaurus/plugin-client-redirects'}: multiple redirects are created with the same "from" pathname: path=${from}
|
|
70
73
|
It is not possible to redirect the same pathname to multiple destinations: ${groupedFromRedirects.map((r) => JSON.stringify(r))}`;
|
|
71
74
|
}
|
|
72
75
|
});
|
|
73
|
-
const collectedRedirects =
|
|
76
|
+
const collectedRedirects = lodash_1.default.uniqBy(redirects, (redirect) => redirect.from);
|
|
74
77
|
// We don't want to override an already existing route with a redirect file!
|
|
75
78
|
const redirectsOverridingExistingPath = collectedRedirects.filter((redirect) => pluginContext.relativeRoutesPaths.includes(redirect.from));
|
|
76
79
|
if (redirectsOverridingExistingPath.length > 0) {
|
|
@@ -9,8 +9,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
9
9
|
const tslib_1 = require("tslib");
|
|
10
10
|
const eta = (0, tslib_1.__importStar)(require("eta"));
|
|
11
11
|
const redirectPage_template_html_1 = (0, tslib_1.__importDefault)(require("./templates/redirectPage.template.html"));
|
|
12
|
-
const lodash_1 = require("lodash");
|
|
13
|
-
const getCompiledRedirectPageTemplate =
|
|
12
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
13
|
+
const getCompiledRedirectPageTemplate = lodash_1.default.memoize(() => eta.compile(redirectPage_template_html_1.default.trim()));
|
|
14
14
|
function renderRedirectPageTemplate(data) {
|
|
15
15
|
const compiled = getCompiledRedirectPageTemplate();
|
|
16
16
|
return compiled(data, eta.defaultConfig);
|
|
@@ -52,18 +52,12 @@ function createFromExtensionsRedirects(paths, extensions) {
|
|
|
52
52
|
if (path === '' || path === '/' || alreadyEndsWithAnExtension(path)) {
|
|
53
53
|
return [];
|
|
54
54
|
}
|
|
55
|
-
// /path => /path.html
|
|
56
|
-
// /path/ => /path.html/
|
|
57
|
-
function getFrom(ext) {
|
|
58
|
-
if (path.endsWith('/')) {
|
|
59
|
-
return (0, utils_1.addTrailingSlash)(`${(0, utils_1.removeTrailingSlash)(path)}.${ext}`);
|
|
60
|
-
}
|
|
61
|
-
else {
|
|
62
|
-
return `${path}.${ext}`;
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
55
|
return extensions.map((ext) => ({
|
|
66
|
-
|
|
56
|
+
// /path => /path.html
|
|
57
|
+
// /path/ => /path.html/
|
|
58
|
+
from: path.endsWith('/')
|
|
59
|
+
? (0, utils_1.addTrailingSlash)(`${(0, utils_1.removeTrailingSlash)(path)}.${ext}`)
|
|
60
|
+
: `${path}.${ext}`,
|
|
67
61
|
to: path,
|
|
68
62
|
}));
|
|
69
63
|
};
|
|
@@ -10,7 +10,7 @@ exports.writeRedirectFile = exports.toRedirectFilesMetadata = exports.createToUr
|
|
|
10
10
|
const tslib_1 = require("tslib");
|
|
11
11
|
const fs_extra_1 = (0, tslib_1.__importDefault)(require("fs-extra"));
|
|
12
12
|
const path_1 = (0, tslib_1.__importDefault)(require("path"));
|
|
13
|
-
const lodash_1 = require("lodash");
|
|
13
|
+
const lodash_1 = (0, tslib_1.__importDefault)(require("lodash"));
|
|
14
14
|
const createRedirectPageContent_1 = (0, tslib_1.__importDefault)(require("./createRedirectPageContent"));
|
|
15
15
|
const utils_1 = require("@docusaurus/utils");
|
|
16
16
|
function createToUrl(baseUrl, to) {
|
|
@@ -18,7 +18,8 @@ function createToUrl(baseUrl, to) {
|
|
|
18
18
|
}
|
|
19
19
|
exports.createToUrl = createToUrl;
|
|
20
20
|
// Create redirect file path
|
|
21
|
-
// Make sure this path has lower precedence over the original file path when
|
|
21
|
+
// Make sure this path has lower precedence over the original file path when
|
|
22
|
+
// served by host providers!
|
|
22
23
|
// Otherwise it can produce infinite redirect loops!
|
|
23
24
|
//
|
|
24
25
|
// See https://github.com/facebook/docusaurus/issues/5055
|
|
@@ -29,23 +30,25 @@ function getRedirectFilePath(fromPath, trailingSlash) {
|
|
|
29
30
|
const filePath = path_1.default.dirname(fromPath);
|
|
30
31
|
// Edge case for https://github.com/facebook/docusaurus/pull/5102
|
|
31
32
|
// If the redirect source path is /xyz, with file /xyz.html
|
|
32
|
-
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
33
|
-
//
|
|
33
|
+
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
34
|
+
// FS, a file/folder can't have the same name "xyz.html"
|
|
35
|
+
// The only possible solution for a redirect file is thus /xyz.html.html (I
|
|
36
|
+
// know, looks suspicious)
|
|
34
37
|
if (trailingSlash === false && fileName.endsWith('.html')) {
|
|
35
38
|
return path_1.default.join(filePath, `${fileName}.html`);
|
|
36
39
|
}
|
|
37
|
-
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
38
|
-
// otherwise it would be picked in priority and
|
|
39
|
-
//
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
}
|
|
40
|
+
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
41
|
+
// redirect file to be /xyz.html, otherwise it would be picked in priority and
|
|
42
|
+
// the redirect file would redirect to itself. We prefer the redirect file to
|
|
43
|
+
// be /xyz.html/index.html, served with lower priority for most static hosting
|
|
44
|
+
// tools
|
|
45
|
+
return path_1.default.join(filePath, `${fileName}/index.html`);
|
|
43
46
|
}
|
|
44
47
|
function toRedirectFilesMetadata(redirects, pluginContext, trailingSlash) {
|
|
45
48
|
// Perf: avoid rendering the template twice with the exact same "props"
|
|
46
49
|
// We might create multiple redirect pages for the same destination url
|
|
47
50
|
// note: the first fn arg is the cache key!
|
|
48
|
-
const createPageContentMemoized =
|
|
51
|
+
const createPageContentMemoized = lodash_1.default.memoize((toUrl) => (0, createRedirectPageContent_1.default)({ toUrl }));
|
|
49
52
|
const createFileMetadata = (redirect) => {
|
|
50
53
|
const fileRelativePath = getRedirectFilePath(redirect.from, trailingSlash);
|
|
51
54
|
const fileAbsolutePath = path_1.default.join(pluginContext.outDir, fileRelativePath);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@docusaurus/plugin-client-redirects",
|
|
3
|
-
"version": "2.0.0-beta.
|
|
3
|
+
"version": "2.0.0-beta.16",
|
|
4
4
|
"description": "Client redirects plugin for Docusaurus.",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "src/plugin-client-redirects.d.ts",
|
|
@@ -18,18 +18,18 @@
|
|
|
18
18
|
},
|
|
19
19
|
"license": "MIT",
|
|
20
20
|
"dependencies": {
|
|
21
|
-
"@docusaurus/core": "2.0.0-beta.
|
|
22
|
-
"@docusaurus/logger": "2.0.0-beta.
|
|
23
|
-
"@docusaurus/utils": "2.0.0-beta.
|
|
24
|
-
"@docusaurus/utils-common": "2.0.0-beta.
|
|
25
|
-
"@docusaurus/utils-validation": "2.0.0-beta.
|
|
21
|
+
"@docusaurus/core": "2.0.0-beta.16",
|
|
22
|
+
"@docusaurus/logger": "2.0.0-beta.16",
|
|
23
|
+
"@docusaurus/utils": "2.0.0-beta.16",
|
|
24
|
+
"@docusaurus/utils-common": "2.0.0-beta.16",
|
|
25
|
+
"@docusaurus/utils-validation": "2.0.0-beta.16",
|
|
26
26
|
"eta": "^1.12.3",
|
|
27
|
-
"fs-extra": "^10.0.
|
|
28
|
-
"lodash": "^4.17.
|
|
27
|
+
"fs-extra": "^10.0.1",
|
|
28
|
+
"lodash": "^4.17.21",
|
|
29
29
|
"tslib": "^2.3.1"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
|
-
"@docusaurus/types": "2.0.0-beta.
|
|
32
|
+
"@docusaurus/types": "2.0.0-beta.16"
|
|
33
33
|
},
|
|
34
34
|
"peerDependencies": {
|
|
35
35
|
"react": "^16.8.4 || ^17.0.0",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": ">=14"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "eb43c4d4f95a4fb97dc9bb9dc615413e0dc2e1e7"
|
|
42
42
|
}
|
package/src/collectRedirects.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import
|
|
8
|
+
import _ from 'lodash';
|
|
9
9
|
import type {
|
|
10
10
|
PluginOptions,
|
|
11
11
|
RedirectOption,
|
|
@@ -39,9 +39,12 @@ export default function collectRedirects(
|
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
// If users wants to redirect to=/abc and they enable trailingSlash=true then
|
|
42
|
-
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
43
|
-
//
|
|
44
|
-
//
|
|
42
|
+
// => we don't want to reject the to=/abc (as only /abc/ is an existing/valid
|
|
43
|
+
// path now)
|
|
44
|
+
// => we want to redirect to=/abc/ without the user having to change all its
|
|
45
|
+
// redirect plugin options
|
|
46
|
+
// It should be easy to toggle siteConfig.trailingSlash option without having to
|
|
47
|
+
// change other configs
|
|
45
48
|
function applyRedirectsTrailingSlash(
|
|
46
49
|
redirects: RedirectMetadata[],
|
|
47
50
|
params: ApplyTrailingSlashParams,
|
|
@@ -61,8 +64,8 @@ function validateCollectedRedirects(
|
|
|
61
64
|
try {
|
|
62
65
|
validateRedirect(redirect);
|
|
63
66
|
return undefined;
|
|
64
|
-
} catch (
|
|
65
|
-
return (
|
|
67
|
+
} catch (err) {
|
|
68
|
+
return (err as Error).message;
|
|
66
69
|
}
|
|
67
70
|
})
|
|
68
71
|
.filter(Boolean);
|
|
@@ -76,7 +79,7 @@ function validateCollectedRedirects(
|
|
|
76
79
|
|
|
77
80
|
const allowedToPaths = pluginContext.relativeRoutesPaths;
|
|
78
81
|
const toPaths = redirects.map((redirect) => redirect.to);
|
|
79
|
-
const illegalToPaths = difference(toPaths, allowedToPaths);
|
|
82
|
+
const illegalToPaths = _.difference(toPaths, allowedToPaths);
|
|
80
83
|
if (illegalToPaths.length > 0) {
|
|
81
84
|
throw new Error(
|
|
82
85
|
`You are trying to create client-side redirections to paths that do not exist:
|
|
@@ -95,7 +98,7 @@ function filterUnwantedRedirects(
|
|
|
95
98
|
): RedirectMetadata[] {
|
|
96
99
|
// we don't want to create twice the same redirect
|
|
97
100
|
// that would lead to writing twice the same html redirection file
|
|
98
|
-
Object.entries(groupBy(redirects, (redirect) => redirect.from)).forEach(
|
|
101
|
+
Object.entries(_.groupBy(redirects, (redirect) => redirect.from)).forEach(
|
|
99
102
|
([from, groupedFromRedirects]) => {
|
|
100
103
|
if (groupedFromRedirects.length > 1) {
|
|
101
104
|
logger.error`name=${'@docusaurus/plugin-client-redirects'}: multiple redirects are created with the same "from" pathname: path=${from}
|
|
@@ -105,7 +108,7 @@ It is not possible to redirect the same pathname to multiple destinations: ${gro
|
|
|
105
108
|
}
|
|
106
109
|
},
|
|
107
110
|
);
|
|
108
|
-
const collectedRedirects = uniqBy(redirects, (redirect) => redirect.from);
|
|
111
|
+
const collectedRedirects = _.uniqBy(redirects, (redirect) => redirect.from);
|
|
109
112
|
|
|
110
113
|
// We don't want to override an already existing route with a redirect file!
|
|
111
114
|
const redirectsOverridingExistingPath = collectedRedirects.filter(
|
|
@@ -7,13 +7,13 @@
|
|
|
7
7
|
|
|
8
8
|
import * as eta from 'eta';
|
|
9
9
|
import redirectPageTemplate from './templates/redirectPage.template.html';
|
|
10
|
-
import
|
|
10
|
+
import _ from 'lodash';
|
|
11
11
|
|
|
12
12
|
type CreateRedirectPageOptions = {
|
|
13
13
|
toUrl: string;
|
|
14
14
|
};
|
|
15
15
|
|
|
16
|
-
const getCompiledRedirectPageTemplate = memoize(() =>
|
|
16
|
+
const getCompiledRedirectPageTemplate = _.memoize(() =>
|
|
17
17
|
eta.compile(redirectPageTemplate.trim()),
|
|
18
18
|
);
|
|
19
19
|
|
|
@@ -81,19 +81,12 @@ export function createFromExtensionsRedirects(
|
|
|
81
81
|
if (path === '' || path === '/' || alreadyEndsWithAnExtension(path)) {
|
|
82
82
|
return [];
|
|
83
83
|
}
|
|
84
|
-
|
|
85
|
-
// /path => /path.html
|
|
86
|
-
// /path/ => /path.html/
|
|
87
|
-
function getFrom(ext: string) {
|
|
88
|
-
if (path.endsWith('/')) {
|
|
89
|
-
return addTrailingSlash(`${removeTrailingSlash(path)}.${ext}`);
|
|
90
|
-
} else {
|
|
91
|
-
return `${path}.${ext}`;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
84
|
return extensions.map((ext) => ({
|
|
96
|
-
|
|
85
|
+
// /path => /path.html
|
|
86
|
+
// /path/ => /path.html/
|
|
87
|
+
from: path.endsWith('/')
|
|
88
|
+
? addTrailingSlash(`${removeTrailingSlash(path)}.${ext}`)
|
|
89
|
+
: `${path}.${ext}`,
|
|
97
90
|
to: path,
|
|
98
91
|
}));
|
|
99
92
|
};
|
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import fs from 'fs-extra';
|
|
9
9
|
import path from 'path';
|
|
10
|
-
import
|
|
10
|
+
import _ from 'lodash';
|
|
11
11
|
|
|
12
12
|
import type {PluginContext, RedirectMetadata} from './types';
|
|
13
13
|
import createRedirectPageContent from './createRedirectPageContent';
|
|
@@ -25,7 +25,8 @@ export function createToUrl(baseUrl: string, to: string): string {
|
|
|
25
25
|
}
|
|
26
26
|
|
|
27
27
|
// Create redirect file path
|
|
28
|
-
// Make sure this path has lower precedence over the original file path when
|
|
28
|
+
// Make sure this path has lower precedence over the original file path when
|
|
29
|
+
// served by host providers!
|
|
29
30
|
// Otherwise it can produce infinite redirect loops!
|
|
30
31
|
//
|
|
31
32
|
// See https://github.com/facebook/docusaurus/issues/5055
|
|
@@ -39,17 +40,19 @@ function getRedirectFilePath(
|
|
|
39
40
|
const filePath = path.dirname(fromPath);
|
|
40
41
|
// Edge case for https://github.com/facebook/docusaurus/pull/5102
|
|
41
42
|
// If the redirect source path is /xyz, with file /xyz.html
|
|
42
|
-
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
43
|
-
//
|
|
43
|
+
// We can't write the redirect file at /xyz.html/index.html because for Unix
|
|
44
|
+
// FS, a file/folder can't have the same name "xyz.html"
|
|
45
|
+
// The only possible solution for a redirect file is thus /xyz.html.html (I
|
|
46
|
+
// know, looks suspicious)
|
|
44
47
|
if (trailingSlash === false && fileName.endsWith('.html')) {
|
|
45
48
|
return path.join(filePath, `${fileName}.html`);
|
|
46
49
|
}
|
|
47
|
-
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
48
|
-
// otherwise it would be picked in priority and
|
|
49
|
-
//
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
}
|
|
50
|
+
// If the target path is /xyz, with file /xyz/index.html, we don't want the
|
|
51
|
+
// redirect file to be /xyz.html, otherwise it would be picked in priority and
|
|
52
|
+
// the redirect file would redirect to itself. We prefer the redirect file to
|
|
53
|
+
// be /xyz.html/index.html, served with lower priority for most static hosting
|
|
54
|
+
// tools
|
|
55
|
+
return path.join(filePath, `${fileName}/index.html`);
|
|
53
56
|
}
|
|
54
57
|
|
|
55
58
|
export function toRedirectFilesMetadata(
|
|
@@ -60,7 +63,7 @@ export function toRedirectFilesMetadata(
|
|
|
60
63
|
// Perf: avoid rendering the template twice with the exact same "props"
|
|
61
64
|
// We might create multiple redirect pages for the same destination url
|
|
62
65
|
// note: the first fn arg is the cache key!
|
|
63
|
-
const createPageContentMemoized = memoize((toUrl: string) =>
|
|
66
|
+
const createPageContentMemoized = _.memoize((toUrl: string) =>
|
|
64
67
|
createRedirectPageContent({toUrl}),
|
|
65
68
|
);
|
|
66
69
|
|