refactor(worker): 迁移静态资源处理至Workers Sites配置

更新wrangler.toml配置,将Assets配置改为Workers Sites格式。同时优化[[path]].ts中的静态资源处理逻辑,统一使用getAssetFromKV方法并简化错误处理流程。
This commit is contained in:
2025-10-08 16:41:39 +08:00
parent 360d55ce55
commit 6a401aea98
3 changed files with 67 additions and 39 deletions

46
deployed_final.html Normal file
View File

@@ -0,0 +1,46 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>PixivNow</title>
<!-- Google tag (gtag.js) -->
<!-- <script
async
src="https://www.googletagmanager.com/gtag/js?id=%VITE_GOOGLE_ANALYTICS_ID%"
></script>
<script>
window.dataLayer = window.dataLayer || []
function gtag() {
dataLayer.push(arguments)
}
gtag('js', new Date())
gtag('config', '%VITE_GOOGLE_ANALYTICS_ID%')
</script> -->
<!-- Umami Analytics -->
<script defer src="https://cloud.umami.is/script.js" data-website-id="842d980c-5e11-4834-a2a8-5daaa285ce66"></script>
<!-- Google Search Console -->
<meta
name="google-site-verification"
content="%VITE_GOOGLE_SEARCH_CONSOLE_VERIFICATION%"
/>
<!-- Google AdSense -->
<script
async
src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=%VITE_ADSENSE_PUB_ID%"
crossorigin="anonymous"
></script>
<!-- jQuery -->
<!-- <script src="https://unpkg.com/jquery@3.7.1/dist/jquery.js"></script> -->
<script type="module" crossorigin src="/assets/index-DzjFVrBY.js"></script>
<link rel="stylesheet" crossorigin href="/assets/index-QezOOX5q.css">
</head>
<body>
<noscript>
This site requires JavaScript enabled. Please check your browser settings.
</noscript>
<div id="app"></div>
</body>
</html>

View File

@@ -59,28 +59,25 @@ export default {
} }
// 静态资源和前端页面处理 // 静态资源和前端页面处理
// 使用新的 Assets API // 使用 getAssetFromKV (Workers Sites)
if (env.ASSETS) { try {
try { return await getAssetFromKV(
return await env.ASSETS.fetch(request) {
} catch (e) { request,
console.log('Asset not found, falling back to index.html') waitUntil: ctx.waitUntil.bind(ctx),
// 如果静态资源不存在,返回 index.htmlSPA 路由处理) },
try { {
const indexRequest = new Request(new URL('/index.html', request.url), request) ASSET_NAMESPACE: env.__STATIC_CONTENT,
return await env.ASSETS.fetch(indexRequest) ASSET_MANIFEST: env.__STATIC_CONTENT_MANIFEST,
} catch (indexError) {
console.log('Index.html not found, using fallback')
// 如果 index.html 也不存在,返回内置的前端页面
return await handleFrontendPage(request, env)
} }
} )
} else { } catch (e) {
// 如果没有 ASSETS使用 getAssetFromKV // 如果静态资源不存在,返回 index.htmlSPA 路由处理)
try { try {
const indexRequest = new Request(new URL('/index.html', request.url), request)
return await getAssetFromKV( return await getAssetFromKV(
{ {
request, request: indexRequest,
waitUntil: ctx.waitUntil.bind(ctx), waitUntil: ctx.waitUntil.bind(ctx),
}, },
{ {
@@ -88,24 +85,9 @@ export default {
ASSET_MANIFEST: env.__STATIC_CONTENT_MANIFEST, ASSET_MANIFEST: env.__STATIC_CONTENT_MANIFEST,
} }
) )
} catch (e) { } catch (indexError) {
// 如果静态资源不存在,返回 index.htmlSPA 路由处理) // 如果 index.html 也不存在,返回内置的前端页面
try { return await handleFrontendPage(request, env)
const indexRequest = new Request(new URL('/index.html', request.url), request)
return await getAssetFromKV(
{
request: indexRequest,
waitUntil: ctx.waitUntil.bind(ctx),
},
{
ASSET_NAMESPACE: env.__STATIC_CONTENT,
ASSET_MANIFEST: env.__STATIC_CONTENT_MANIFEST,
}
)
} catch (indexError) {
// 如果 index.html 也不存在,返回内置的前端页面
return await handleFrontendPage(request, env)
}
} }
} }

View File

@@ -2,9 +2,9 @@ name = "pixivnow-worker"
main = "functions/[[path]].ts" main = "functions/[[path]].ts"
compatibility_date = "2023-10-30" compatibility_date = "2023-10-30"
# Assets 配置 - 用于部署静态资源 # Workers Sites 配置 - 用于部署静态资源
[assets] [site]
directory = "./dist" bucket = "./dist"
# 环境变量配置 # 环境变量配置
# 这些变量都有默认值,可以根据需要自定义 # 这些变量都有默认值,可以根据需要自定义