refactor(worker): 迁移静态资源处理至Workers Sites配置
更新wrangler.toml配置,将Assets配置改为Workers Sites格式。同时优化[[path]].ts中的静态资源处理逻辑,统一使用getAssetFromKV方法并简化错误处理流程。
This commit is contained in:
46
deployed_final.html
Normal file
46
deployed_final.html
Normal 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>
|
||||||
@@ -59,24 +59,7 @@ export default {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 静态资源和前端页面处理
|
// 静态资源和前端页面处理
|
||||||
// 使用新的 Assets API
|
// 使用 getAssetFromKV (Workers Sites)
|
||||||
if (env.ASSETS) {
|
|
||||||
try {
|
|
||||||
return await env.ASSETS.fetch(request)
|
|
||||||
} catch (e) {
|
|
||||||
console.log('Asset not found, falling back to index.html')
|
|
||||||
// 如果静态资源不存在,返回 index.html(SPA 路由处理)
|
|
||||||
try {
|
|
||||||
const indexRequest = new Request(new URL('/index.html', request.url), request)
|
|
||||||
return await env.ASSETS.fetch(indexRequest)
|
|
||||||
} catch (indexError) {
|
|
||||||
console.log('Index.html not found, using fallback')
|
|
||||||
// 如果 index.html 也不存在,返回内置的前端页面
|
|
||||||
return await handleFrontendPage(request, env)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// 如果没有 ASSETS,使用 getAssetFromKV
|
|
||||||
try {
|
try {
|
||||||
return await getAssetFromKV(
|
return await getAssetFromKV(
|
||||||
{
|
{
|
||||||
@@ -107,7 +90,6 @@ export default {
|
|||||||
return await handleFrontendPage(request, env)
|
return await handleFrontendPage(request, env)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Worker error:', error)
|
console.error('Worker error:', error)
|
||||||
|
|||||||
@@ -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"
|
||||||
|
|
||||||
# 环境变量配置
|
# 环境变量配置
|
||||||
# 这些变量都有默认值,可以根据需要自定义
|
# 这些变量都有默认值,可以根据需要自定义
|
||||||
|
|||||||
Reference in New Issue
Block a user