@docusaurus/plugin-client-redirects 2.1.0 → 2.3.0

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.
@@ -9,6 +9,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
9
9
  const tslib_1 = require("tslib");
10
10
  const lodash_1 = tslib_1.__importDefault(require("lodash"));
11
11
  const logger_1 = tslib_1.__importDefault(require("@docusaurus/logger"));
12
+ const utils_1 = require("@docusaurus/utils");
12
13
  const utils_common_1 = require("@docusaurus/utils-common");
13
14
  const extensionRedirects_1 = require("./extensionRedirects");
14
15
  const redirectValidation_1 = require("./redirectValidation");
@@ -58,14 +59,52 @@ function validateCollectedRedirects(redirects, pluginContext) {
58
59
  }
59
60
  const allowedToPaths = pluginContext.relativeRoutesPaths;
60
61
  const toPaths = redirects.map((redirect) => redirect.to);
61
- const illegalToPaths = lodash_1.default.difference(toPaths, allowedToPaths);
62
- if (illegalToPaths.length > 0) {
63
- throw new Error(`You are trying to create client-side redirections to paths that do not exist:
64
- - ${illegalToPaths.join('\n- ')}
62
+ const trailingSlashConfig = pluginContext.siteConfig.trailingSlash;
63
+ // Key is the path, value is whether a valid toPath with a different trailing
64
+ // slash exists; if the key doesn't exist it means it's valid
65
+ const differByTrailSlash = new Map(toPaths.map((path) => [path, false]));
66
+ allowedToPaths.forEach((toPath) => {
67
+ if (differByTrailSlash.has(toPath)) {
68
+ differByTrailSlash.delete(toPath);
69
+ }
70
+ else if (differByTrailSlash.has((0, utils_1.removeTrailingSlash)(toPath))) {
71
+ if (trailingSlashConfig === true) {
72
+ differByTrailSlash.set((0, utils_1.removeTrailingSlash)(toPath), true);
73
+ }
74
+ else {
75
+ differByTrailSlash.delete((0, utils_1.removeTrailingSlash)(toPath));
76
+ }
77
+ }
78
+ else if (differByTrailSlash.has((0, utils_1.addTrailingSlash)(toPath))) {
79
+ if (trailingSlashConfig === false) {
80
+ differByTrailSlash.set((0, utils_1.addTrailingSlash)(toPath), true);
81
+ }
82
+ else {
83
+ differByTrailSlash.delete((0, utils_1.addTrailingSlash)(toPath));
84
+ }
85
+ }
86
+ });
87
+ if (differByTrailSlash.size > 0) {
88
+ console.log(differByTrailSlash);
89
+ const errors = Array.from(differByTrailSlash.entries());
90
+ let message = 'You are trying to create client-side redirections to invalid paths.\n';
91
+ const [trailingSlashIssues, invalidPaths] = lodash_1.default.partition(errors, ([, differ]) => differ);
92
+ if (trailingSlashIssues.length) {
93
+ message += `
94
+ These paths do exist, but because you have explicitly set trailingSlash=${trailingSlashConfig}, you need to write the path ${trailingSlashConfig ? 'with trailing slash' : 'without trailing slash'}:
95
+ - ${trailingSlashIssues.map(([p]) => p).join('\n- ')}
96
+ `;
97
+ }
98
+ if (invalidPaths.length) {
99
+ message += `
100
+ These paths are redirected to but do not exist:
101
+ - ${invalidPaths.map(([p]) => p).join('\n- ')}
65
102
 
66
103
  Valid paths you can redirect to:
67
104
  - ${allowedToPaths.join('\n- ')}
68
- `);
105
+ `;
106
+ }
107
+ throw new Error(message);
69
108
  }
70
109
  }
71
110
  function filterUnwantedRedirects(redirects, pluginContext) {
@@ -4,5 +4,5 @@
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
- declare const _default: "\n<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"UTF-8\">\n <meta http-equiv=\"refresh\" content=\"0; url=<%= it.toUrl %>\">\n <link rel=\"canonical\" href=\"<%= it.toUrl %>\" />\n </head>\n <script>\n window.location.href = '<%= it.toUrl %>';\n </script>\n</html>\n";
7
+ declare const _default: "\n<!DOCTYPE html>\n<html>\n <head>\n <meta charset=\"UTF-8\">\n <meta http-equiv=\"refresh\" content=\"0; url=<%= it.toUrl %>\">\n <link rel=\"canonical\" href=\"<%= it.toUrl %>\" />\n </head>\n <script>\n window.location.href = '<%= it.toUrl %>' + window.location.search + window.location.hash;\n </script>\n</html>\n";
8
8
  export default _default;
@@ -15,7 +15,7 @@ exports.default = `
15
15
  <link rel="canonical" href="<%= it.toUrl %>" />
16
16
  </head>
17
17
  <script>
18
- window.location.href = '<%= it.toUrl %>';
18
+ window.location.href = '<%= it.toUrl %>' + window.location.search + window.location.hash;
19
19
  </script>
20
20
  </html>
21
21
  `;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/plugin-client-redirects",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "Client redirects plugin for Docusaurus.",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
@@ -18,18 +18,18 @@
18
18
  },
19
19
  "license": "MIT",
20
20
  "dependencies": {
21
- "@docusaurus/core": "2.1.0",
22
- "@docusaurus/logger": "2.1.0",
23
- "@docusaurus/utils": "2.1.0",
24
- "@docusaurus/utils-common": "2.1.0",
25
- "@docusaurus/utils-validation": "2.1.0",
21
+ "@docusaurus/core": "2.3.0",
22
+ "@docusaurus/logger": "2.3.0",
23
+ "@docusaurus/utils": "2.3.0",
24
+ "@docusaurus/utils-common": "2.3.0",
25
+ "@docusaurus/utils-validation": "2.3.0",
26
26
  "eta": "^1.12.3",
27
27
  "fs-extra": "^10.1.0",
28
28
  "lodash": "^4.17.21",
29
29
  "tslib": "^2.4.0"
30
30
  },
31
31
  "devDependencies": {
32
- "@docusaurus/types": "2.1.0"
32
+ "@docusaurus/types": "2.3.0"
33
33
  },
34
34
  "peerDependencies": {
35
35
  "react": "^16.8.4 || ^17.0.0",
@@ -38,5 +38,5 @@
38
38
  "engines": {
39
39
  "node": ">=16.14"
40
40
  },
41
- "gitHead": "be9b0942641184213485eba7fd75ceb0b328d3f4"
41
+ "gitHead": "ad477781bdca6a11fa9c6daef5048bdcec0ee37e"
42
42
  }
@@ -7,6 +7,7 @@
7
7
 
8
8
  import _ from 'lodash';
9
9
  import logger from '@docusaurus/logger';
10
+ import {addTrailingSlash, removeTrailingSlash} from '@docusaurus/utils';
10
11
  import {applyTrailingSlash} from '@docusaurus/utils-common';
11
12
  import {
12
13
  createFromExtensionsRedirects,
@@ -80,16 +81,59 @@ function validateCollectedRedirects(
80
81
 
81
82
  const allowedToPaths = pluginContext.relativeRoutesPaths;
82
83
  const toPaths = redirects.map((redirect) => redirect.to);
83
- const illegalToPaths = _.difference(toPaths, allowedToPaths);
84
- if (illegalToPaths.length > 0) {
85
- throw new Error(
86
- `You are trying to create client-side redirections to paths that do not exist:
87
- - ${illegalToPaths.join('\n- ')}
84
+ const trailingSlashConfig = pluginContext.siteConfig.trailingSlash;
85
+ // Key is the path, value is whether a valid toPath with a different trailing
86
+ // slash exists; if the key doesn't exist it means it's valid
87
+ const differByTrailSlash = new Map(toPaths.map((path) => [path, false]));
88
+ allowedToPaths.forEach((toPath) => {
89
+ if (differByTrailSlash.has(toPath)) {
90
+ differByTrailSlash.delete(toPath);
91
+ } else if (differByTrailSlash.has(removeTrailingSlash(toPath))) {
92
+ if (trailingSlashConfig === true) {
93
+ differByTrailSlash.set(removeTrailingSlash(toPath), true);
94
+ } else {
95
+ differByTrailSlash.delete(removeTrailingSlash(toPath));
96
+ }
97
+ } else if (differByTrailSlash.has(addTrailingSlash(toPath))) {
98
+ if (trailingSlashConfig === false) {
99
+ differByTrailSlash.set(addTrailingSlash(toPath), true);
100
+ } else {
101
+ differByTrailSlash.delete(addTrailingSlash(toPath));
102
+ }
103
+ }
104
+ });
105
+ if (differByTrailSlash.size > 0) {
106
+ console.log(differByTrailSlash);
107
+ const errors = Array.from(differByTrailSlash.entries());
108
+
109
+ let message =
110
+ 'You are trying to create client-side redirections to invalid paths.\n';
111
+
112
+ const [trailingSlashIssues, invalidPaths] = _.partition(
113
+ errors,
114
+ ([, differ]) => differ,
115
+ );
116
+
117
+ if (trailingSlashIssues.length) {
118
+ message += `
119
+ These paths do exist, but because you have explicitly set trailingSlash=${trailingSlashConfig}, you need to write the path ${
120
+ trailingSlashConfig ? 'with trailing slash' : 'without trailing slash'
121
+ }:
122
+ - ${trailingSlashIssues.map(([p]) => p).join('\n- ')}
123
+ `;
124
+ }
125
+
126
+ if (invalidPaths.length) {
127
+ message += `
128
+ These paths are redirected to but do not exist:
129
+ - ${invalidPaths.map(([p]) => p).join('\n- ')}
88
130
 
89
131
  Valid paths you can redirect to:
90
132
  - ${allowedToPaths.join('\n- ')}
91
- `,
92
- );
133
+ `;
134
+ }
135
+
136
+ throw new Error(message);
93
137
  }
94
138
  }
95
139
 
@@ -14,7 +14,7 @@ export default `
14
14
  <link rel="canonical" href="<%= it.toUrl %>" />
15
15
  </head>
16
16
  <script>
17
- window.location.href = '<%= it.toUrl %>';
17
+ window.location.href = '<%= it.toUrl %>' + window.location.search + window.location.hash;
18
18
  </script>
19
19
  </html>
20
20
  `;