@rspress/plugin-client-redirects 2.0.0-alpha.6 → 2.0.0-alpha.8
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/package.json +6 -6
- package/static/Redirect.tsx +27 -14
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@rspress/plugin-client-redirects",
|
3
|
-
"version": "2.0.0-alpha.
|
3
|
+
"version": "2.0.0-alpha.8",
|
4
4
|
"description": "A plugin for rspress to client redirect in docs.",
|
5
5
|
"bugs": "https://github.com/web-infra-dev/rspress/issues",
|
6
6
|
"repository": {
|
@@ -24,20 +24,20 @@
|
|
24
24
|
"static"
|
25
25
|
],
|
26
26
|
"dependencies": {
|
27
|
-
"@rspress/shared": "2.0.0-alpha.
|
27
|
+
"@rspress/shared": "2.0.0-alpha.8"
|
28
28
|
},
|
29
29
|
"devDependencies": {
|
30
|
-
"@microsoft/api-extractor": "^7.52.
|
31
|
-
"@rslib/core": "0.
|
30
|
+
"@microsoft/api-extractor": "^7.52.2",
|
31
|
+
"@rslib/core": "0.6.0",
|
32
32
|
"@types/node": "^18.11.17",
|
33
|
-
"@types/react": "^18.3.
|
33
|
+
"@types/react": "^18.3.20",
|
34
34
|
"@types/react-dom": "^18.3.5",
|
35
35
|
"react": "^18.3.1",
|
36
36
|
"typescript": "^5.8.2",
|
37
37
|
"@rspress/config": "1.0.0"
|
38
38
|
},
|
39
39
|
"peerDependencies": {
|
40
|
-
"@rspress/runtime": "^2.0.0-alpha.
|
40
|
+
"@rspress/runtime": "^2.0.0-alpha.8"
|
41
41
|
},
|
42
42
|
"engines": {
|
43
43
|
"node": ">=14.17.6"
|
package/static/Redirect.tsx
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import { useLocation } from '@rspress/runtime';
|
2
2
|
import { isExternalUrl } from '@rspress/shared';
|
3
|
-
import { useEffect } from 'react';
|
3
|
+
import { useEffect, useMemo } from 'react';
|
4
4
|
|
5
5
|
// these are types copied from src/types.ts
|
6
6
|
type RedirectRule = {
|
@@ -13,31 +13,44 @@ type RedirectsOptions = {
|
|
13
13
|
};
|
14
14
|
|
15
15
|
export default function Redirect(props: RedirectsOptions = {}) {
|
16
|
-
const { pathname } = useLocation();
|
16
|
+
const { pathname, hash } = useLocation();
|
17
17
|
const { redirects } = props;
|
18
18
|
|
19
|
+
// Use useMemo to preprocess redirect rules to avoid recreating RegExp objects every time you render
|
20
|
+
const processedRedirects = useMemo(() => {
|
21
|
+
if (!redirects?.length) return [];
|
22
|
+
|
23
|
+
return redirects.map(({ from, to }) => ({
|
24
|
+
to,
|
25
|
+
patterns: Array.isArray(from) ? from : [from],
|
26
|
+
}));
|
27
|
+
}, [redirects]);
|
28
|
+
|
19
29
|
useEffect(() => {
|
20
|
-
if
|
21
|
-
|
22
|
-
|
23
|
-
|
30
|
+
// If there is no redirect rule or if it is not in the browser environment, it will be returned
|
31
|
+
if (!processedRedirects.length || typeof window === 'undefined') {
|
32
|
+
return;
|
33
|
+
}
|
24
34
|
|
25
|
-
|
26
|
-
|
35
|
+
for (const { patterns, to } of processedRedirects) {
|
36
|
+
for (const pattern of patterns) {
|
37
|
+
try {
|
38
|
+
const regex = new RegExp(pattern);
|
27
39
|
|
28
|
-
if (regex.test(pathname)
|
40
|
+
if (regex.test(pathname)) {
|
29
41
|
if (isExternalUrl(to)) {
|
30
42
|
window.location.replace(to);
|
31
43
|
} else {
|
32
|
-
window.location.replace(
|
33
|
-
pathname.replace(regex, to) + location.hash,
|
34
|
-
);
|
44
|
+
window.location.replace(pathname.replace(regex, to) + hash);
|
35
45
|
}
|
46
|
+
return;
|
36
47
|
}
|
37
|
-
})
|
48
|
+
} catch (error) {
|
49
|
+
console.warn(`Invalid redirect pattern: ${pattern}`, error);
|
50
|
+
}
|
38
51
|
}
|
39
52
|
}
|
40
|
-
}, [pathname]);
|
53
|
+
}, [pathname, hash, processedRedirects]);
|
41
54
|
|
42
55
|
return null;
|
43
56
|
}
|