Skip to content

Commit 2598b0e

Browse files
committed
Fix URL parameter parsing to handle different URL formats
- Update URL parsing logic to support both query parameter (?id=...) and path parameter (/example-detail/...) formats - Add fallback mechanism to extract example ID from path segments when query parameter is missing - This addresses the issue where manually changing URL format would cause unexpected redirects - The application now properly handles various URL formats without强制重定向 to a specific format
1 parent b8f0abc commit 2598b0e

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

docs/src/pages/example-detail.tsx

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,31 @@ export default function ExampleDetail() {
8181
return;
8282
}
8383

84+
// 支持两种URL参数格式:查询参数 ?id=... 或路径参数 /example-detail/...
85+
// 首先尝试从查询参数获取
8486
const urlParams = new URLSearchParams(location.search);
85-
const urlExampleId = urlParams.get("id") || "hello-world";
87+
let urlExampleId = urlParams.get("id");
88+
89+
// 如果查询参数中没有找到id,尝试从路径中解析(处理类似 /example-detail/xxx 的格式)
90+
if (!urlExampleId) {
91+
const pathSegments = location.pathname.split('/');
92+
// 查找可能包含id的路径段
93+
for (let i = 0; i < pathSegments.length; i++) {
94+
if (pathSegments[i] === 'example-detail' && i + 1 < pathSegments.length) {
95+
const potentialId = pathSegments[i + 1];
96+
// 检查是否是有效的示例ID(存在于EXAMPLES_CONFIG中)
97+
if (EXAMPLES_CONFIG.some(ex => ex.id === potentialId)) {
98+
urlExampleId = potentialId;
99+
break;
100+
}
101+
}
102+
}
103+
}
104+
105+
// 如果仍然没有找到,使用默认值
106+
if (!urlExampleId) {
107+
urlExampleId = "hello-world";
108+
}
86109

87110
// 确保示例存在
88111
const targetExample = EXAMPLES_CONFIG.find(ex => ex.id === urlExampleId) || EXAMPLES_CONFIG[0];
@@ -109,13 +132,34 @@ export default function ExampleDetail() {
109132
const timer = setTimeout(initExample, 100);
110133
return () => clearTimeout(timer);
111134
}
112-
}, [location.search, EXAMPLES_CONFIG.length]); // 监听 EXAMPLES_CONFIG 长度变化
135+
}, [location.pathname, location.search, EXAMPLES_CONFIG.length]); // 监听 EXAMPLES_CONFIG 长度变化
113136

114137
// 监听路由变化,处理语言切换时的URL问题
115138
useEffect(() => {
116139
const handleLocationChange = () => {
140+
// 支持两种URL参数格式:查询参数 ?id=... 或路径参数 /example-detail/...
117141
const urlParams = new URLSearchParams(location.search);
118-
const urlExampleId = urlParams.get("id") || "hello-world";
142+
let urlExampleId = urlParams.get("id");
143+
144+
// 如果查询参数中没有找到id,尝试从路径中解析
145+
if (!urlExampleId) {
146+
const pathSegments = location.pathname.split('/');
147+
// 查找可能包含id的路径段
148+
for (let i = 0; i < pathSegments.length; i++) {
149+
if (pathSegments[i] === 'example-detail' && i + 1 < pathSegments.length) {
150+
const potentialId = pathSegments[i + 1];
151+
// 检查是否是有效的示例ID(存在于EXAMPLES_CONFIG中)
152+
if (EXAMPLES_CONFIG.some(ex => ex.id === potentialId)) {
153+
urlExampleId = potentialId;
154+
break;
155+
}
156+
}
157+
}
158+
}
159+
160+
if (!urlExampleId) {
161+
urlExampleId = "hello-world";
162+
}
119163

120164
// 确保示例存在
121165
const targetExample = EXAMPLES_CONFIG.find(ex => ex.id === urlExampleId) || EXAMPLES_CONFIG[0];

0 commit comments

Comments
 (0)