mirror of
https://github.com/NewFuture/DDNS.git
synced 2026-10-19 16:15:45 +00:00
doc(esa): Add esa config (#650)
* Update esa.jsonc for new project configuration * Add legacy doc redirects in VitePress and simplify ESA 404 handling (#649) * Initial plan * docs: add legacy redirects and simplify 404 handling Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): simplify legacy redirect logic Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): clarify doc redirects Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): align clean url handling Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): tidy url constants Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): reuse en base constant Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): use constants for en base Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): respect dynamic en base Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): reuse html extension constant Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): streamline doc redirect logic Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): further simplify legacy redirects Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): inline clean-url handling Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): avoid redundant html suffix Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * chore(docs): coalesce clean-url handling Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> * Improve URL target resolution logic Refactor URL target resolution to handle clean URLs. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> Co-authored-by: New Future <NewFuture@users.noreply.github.com> * Apply suggestion from @Copilot Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com> Co-authored-by: NewFuture <6290356+NewFuture@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
co-authored by
NewFuture
copilot-swe-agent[bot]
Copilot
parent
0018154e59
commit
aa54605379
@@ -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
|
||||
|
||||
+1
-41
@@ -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' }
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"name": "ddns",
|
||||
"entry": "esa.js",
|
||||
"installCommand": "npm install",
|
||||
"buildCommand": "npm run build",
|
||||
"assets": {
|
||||
"directory": ".vitepress/dist",
|
||||
"notFoundStrategy": "404Page"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user