Skip to content

Commit 55770dc

Browse files
committed
Revert problematic changes and restore basic example switching functionality
- Revert complex URL normalization logic that was interfering with example switching - Restore basic URL update functionality in handleExampleSelect - Remove URL normalization useEffect that was causing issues with example switching - Prioritize basic functionality over URL formatting fixes
1 parent 6724c8d commit 55770dc

1 file changed

Lines changed: 6 additions & 34 deletions

File tree

docs/src/pages/example-detail.tsx

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -134,27 +134,7 @@ export default function ExampleDetail() {
134134
}
135135
}, [location.pathname, location.search, EXAMPLES_CONFIG.length]); // 监听 EXAMPLES_CONFIG 长度变化
136136

137-
// 监听URL变化并规范化URL格式,确保不带尾随斜杠
138-
useEffect(() => {
139-
// 检查当前URL是否包含尾随斜杠,并规范化为不带斜杠的格式
140-
// 使用setTimeout确保在所有状态更新完成后再执行规范化
141-
const timer = setTimeout(() => {
142-
if (location.pathname.includes('/example-detail/') && location.search.includes('id=')) {
143-
// 如果路径是 /example-detail/ 形式(带斜杠),将其规范化为 /example-detail 形式(不带斜杠)
144-
const normalizedPath = location.pathname.replace(/\/example-detail\/$/, '/example-detail');
145-
if (normalizedPath !== location.pathname) {
146-
// URL包含尾随斜杠,需要规范化,但不触发页面重新加载
147-
const normalizedUrl = `${normalizedPath}${location.search}`;
148-
// 仅在URL实际发生变化时进行替换
149-
if (normalizedUrl !== `${location.pathname}${location.search}`) {
150-
window.history.replaceState({}, document.title, normalizedUrl);
151-
}
152-
}
153-
}
154-
}, 100); // 延迟执行,确保状态更新完成
155-
156-
return () => clearTimeout(timer);
157-
}, [location.pathname, location.search]);
137+
158138

159139
// 监听路由变化,处理语言切换时的URL问题
160140
useEffect(() => {
@@ -335,24 +315,16 @@ export default function ExampleDetail() {
335315
setExampleId(selectedExample.id);
336316
setCode(selectedExample.code);
337317

338-
// 更新URL参数,保持当前语言路径前缀,但去除路径末尾的斜杠
318+
// 更新URL参数,保持当前语言路径前缀
339319
const currentPath = window.location.pathname;
340320
const currentLangPrefix = currentPath.startsWith('/zh/') ? '/zh' :
341321
currentPath.startsWith('/en/') ? '/en' : '';
342322

343-
// 构建正确的URL路径,处理语言前缀,但不包含末尾斜杠
323+
// 构建正确的URL,处理语言前缀
344324
let newUrlPath = currentLangPrefix ? `${currentLangPrefix}/example-detail` : '/example-detail';
345-
346-
// 确保路径不以斜杠结尾,使查询参数直接连接
347-
if (newUrlPath.endsWith('/')) {
348-
newUrlPath = newUrlPath.slice(0, -1);
349-
}
350-
351-
// 构建完整的URL,使用window.location.origin确保正确的协议和主机
352-
const newUrlString = `${window.location.origin}${newUrlPath}?id=${selectedExample.id}`;
353-
354-
// 使用 replaceState 更新URL但不触发页面重载
355-
window.history.replaceState({}, '', newUrlString);
325+
const newUrl = new URL(window.location.origin + newUrlPath);
326+
newUrl.searchParams.set('id', selectedExample.id);
327+
window.history.replaceState({}, '', newUrl.toString());
356328

357329
// 立即运行新代码,使用新的代码内容,避免依赖可能未更新的状态
358330
setTimeout(() => {

0 commit comments

Comments
 (0)