Skip to content

Commit 6e5387e

Browse files
committed
fix: update router
1 parent 3058db7 commit 6e5387e

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

public/index.html

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,38 @@
4444
<body>
4545
<noscript>You need to enable JavaScript to run this app.</noscript>
4646
<div id="root"></div>
47+
<script>
48+
// 处理 GitHub Pages 路由重定向
49+
(function() {
50+
// 只在生产环境且不是首页时执行
51+
if (window.location.hostname === 'web3camp.us' && window.location.pathname !== '/') {
52+
const validRoutes = ['/home', '/tutorial', '/tools', '/tasks', '/news'];
53+
const currentPath = window.location.pathname;
54+
55+
// 检查是否是有效路由
56+
if (validRoutes.includes(currentPath)) {
57+
// 重定向到首页并传递路由参数
58+
window.location.href = '/?route=' + encodeURIComponent(currentPath);
59+
}
60+
}
61+
})();
62+
</script>
63+
<script>
64+
// 处理 GitHub Pages 路由重定向
65+
(function() {
66+
// 只在生产环境且不是首页时执行
67+
if (window.location.hostname === 'web3camp.us' && window.location.pathname !== '/') {
68+
const validRoutes = ['/home', '/tutorial', '/tools', '/tasks', '/news'];
69+
const currentPath = window.location.pathname;
70+
71+
// 检查是否是有效路由
72+
if (validRoutes.includes(currentPath)) {
73+
// 重定向到首页并传递路由参数
74+
window.location.href = '/?route=' + encodeURIComponent(currentPath);
75+
}
76+
}
77+
})();
78+
</script>
4779
<!--
4880
This HTML file is a template.
4981
If you open it directly in the browser, you will see an empty page.

src/App.js

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,45 @@ import GlobalStyle from "./utils/GloablStyle";
22
import { WOW } from 'wowjs';
33
import "animate.css";
44
import {useEffect} from "react";
5-
import { BrowserRouter as Router } from "react-router-dom";
5+
import { BrowserRouter as Router, useNavigate } from "react-router-dom";
66
import RouterLink from "./router/router";
77
import Header from "./components/header";
88
import Footer from "./components/footer";
99

10-
function App() {
10+
function AppContent() {
11+
const navigate = useNavigate();
12+
1113
useEffect(() => {
1214
new WOW().init();
13-
}, []);
14-
return (
15-
<div>
16-
17-
<Router>
15+
16+
// 只在生产环境处理路由参数
17+
if (window.location.hostname === 'web3camp.us') {
18+
const urlParams = new URLSearchParams(window.location.search);
19+
const redirectRoute = urlParams.get('route');
20+
const validRoutes = ['/home', '/tutorial', '/tools', '/tasks', '/news'];
21+
22+
// 如果有路由参数,导航到对应页面并清理URL
23+
if (redirectRoute && validRoutes.includes(redirectRoute)) {
24+
navigate(redirectRoute);
25+
window.history.replaceState({}, '', redirectRoute);
26+
}
27+
}
28+
}, [navigate]);
29+
30+
return (
31+
<>
1832
<Header />
1933
<RouterLink />
2034
<Footer />
35+
</>
36+
);
37+
}
38+
39+
function App() {
40+
return (
41+
<div>
42+
<Router>
43+
<AppContent />
2144
</Router>
2245
<GlobalStyle />
2346
</div>

0 commit comments

Comments
 (0)