diff --git a/docs/.vitepress/theme/index.ts b/docs/.vitepress/theme/index.ts index 7e0aafa..3baf667 100644 --- a/docs/.vitepress/theme/index.ts +++ b/docs/.vitepress/theme/index.ts @@ -1,6 +1,42 @@ import DefaultTheme from 'vitepress/theme' import type { Theme } from 'vitepress' +const EN_BASE = '/en/' +const DOC_PREFIX = '/doc/' +const EN_HTML_SUFFIX = '.en.html' +const HTML_EXT = '.html' + export default { - extends: DefaultTheme + extends: DefaultTheme, + enhanceApp({ siteData }) { + if (typeof window === 'undefined') return + + const { pathname, search, hash } = window.location + const cleanUrls = !!siteData?.value?.cleanUrls + const enLink = siteData?.value?.locales?.en?.link || EN_BASE + const enBase = enLink.endsWith('/') ? enLink : `${enLink}/` + + let target: string | null = null + if (pathname === '/index.en.html') { + target = enBase + } else if (pathname.startsWith(DOC_PREFIX)) { + const rest = pathname.slice(DOC_PREFIX.length) + if (!rest) { + target = '/' + } else if (rest.endsWith(EN_HTML_SUFFIX)) { + target = `${enBase}${rest.slice(0, -EN_HTML_SUFFIX.length)}${cleanUrls ? "" : HTML_EXT}` + } else if (cleanUrls && rest.endsWith(HTML_EXT)) { + target = `/${rest.slice(0, -HTML_EXT.length)}` // Apply clean URLs + } else { + target = `/${rest}` + } + } else { + return + } + + const resolved = new URL(target + search + hash, window.location.origin) + if (resolved.href !== window.location.href) { + window.location.replace(resolved.href) + } + } } satisfies Theme diff --git a/docs/esa.js b/docs/esa.js index 1338d16..2549fe6 100644 --- a/docs/esa.js +++ b/docs/esa.js @@ -193,47 +193,7 @@ async function handleRequest(request) { return handleRelease(version, binaryFile); } - // No match found - return 404.html page (avoid infinite loop by not fetching if already requesting 404.html) - // 未匹配任何URL模式 - 返回404.html页面(避免无限循环,如果已经在请求404.html则不再获取) - if (path !== '/404.html') { - // 缓存key使用HTTP协议 (Cache key uses HTTP protocol) - const notFoundUrl = 'http://ddns.newfuture.cc/404.html'; - - // 尝试从缓存获取 (Try to get from cache first) - const cachedResponse = await cache.get(notFoundUrl); - if (cachedResponse) { - return cachedResponse; - } - - try { - const notFoundResponse = await fetch(notFoundUrl, { redirect: 'follow' }); - - if (notFoundResponse.ok) { - // Return 404.html with 404 status code and appropriate headers - const headers = new Headers(); - // Ensure correct content type for HTML 404 page - headers.set('Content-Type', 'text/html; charset=utf-8'); - headers.set('Cache-Control', 'public, max-age=21600'); // Cache 404 page for 6 hours - - const finalResponse = new Response(notFoundResponse.body, { - status: 404, - statusText: 'Not Found', - headers: headers - }); - - // 先缓存再返回 (Cache first then return) - cache.put(notFoundUrl, finalResponse.clone()).catch(err => { - console.log(`Failed to cache 404 response for ${notFoundUrl}:`, err); - }); - - return finalResponse; - } - } catch (err) { - console.log('Failed to fetch 404.html:', err); - } - } - - // Fallback if 404.html is not available + // No match found - return 404 directly return new Response('Not Found', { status: 404, headers: { 'Content-Type': 'text/plain' } diff --git a/docs/esa.jsonc b/docs/esa.jsonc new file mode 100644 index 0000000..5d4d74f --- /dev/null +++ b/docs/esa.jsonc @@ -0,0 +1,10 @@ +{ + "name": "ddns", + "entry": "esa.js", + "installCommand": "npm install", + "buildCommand": "npm run build", + "assets": { + "directory": ".vitepress/dist", + "notFoundStrategy": "404Page" + } +}