@rspress/plugin-client-redirects 2.0.0-alpha.6 → 2.0.0-alpha.7

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.
Files changed (2) hide show
  1. package/package.json +3 -3
  2. 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.6",
3
+ "version": "2.0.0-alpha.7",
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,7 +24,7 @@
24
24
  "static"
25
25
  ],
26
26
  "dependencies": {
27
- "@rspress/shared": "2.0.0-alpha.6"
27
+ "@rspress/shared": "2.0.0-alpha.7"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@microsoft/api-extractor": "^7.52.1",
@@ -37,7 +37,7 @@
37
37
  "@rspress/config": "1.0.0"
38
38
  },
39
39
  "peerDependencies": {
40
- "@rspress/runtime": "^2.0.0-alpha.6"
40
+ "@rspress/runtime": "^2.0.0-alpha.7"
41
41
  },
42
42
  "engines": {
43
43
  "node": ">=14.17.6"
@@ -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 (redirects) {
21
- for (const redirect of redirects) {
22
- const { from, to } = redirect;
23
- const fromPaths = Array.isArray(from) ? from : [from];
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
- fromPaths.forEach(item => {
26
- const regex = new RegExp(item);
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) && typeof window !== 'undefined') {
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
  }