From 6a401aea98babbbb508775e9235df9f56cf33079 Mon Sep 17 00:00:00 2001 From: JuLuogo <1576586736@qq.com> Date: Wed, 8 Oct 2025 16:41:39 +0800 Subject: [PATCH] =?UTF-8?q?refactor(worker):=20=E8=BF=81=E7=A7=BB=E9=9D=99?= =?UTF-8?q?=E6=80=81=E8=B5=84=E6=BA=90=E5=A4=84=E7=90=86=E8=87=B3Workers?= =?UTF-8?q?=20Sites=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 更新wrangler.toml配置,将Assets配置改为Workers Sites格式。同时优化[[path]].ts中的静态资源处理逻辑,统一使用getAssetFromKV方法并简化错误处理流程。 --- deployed_final.html | 46 ++++++++++++++++++++++++++++++++++++ functions/[[path]].ts | 54 +++++++++++++++---------------------------- wrangler.toml | 6 ++--- 3 files changed, 67 insertions(+), 39 deletions(-) create mode 100644 deployed_final.html diff --git a/deployed_final.html b/deployed_final.html new file mode 100644 index 0000000..58c284d --- /dev/null +++ b/deployed_final.html @@ -0,0 +1,46 @@ + + + + + + + PixivNow + + + + + + + + + + + + + + + + +
+ + diff --git a/functions/[[path]].ts b/functions/[[path]].ts index bf44bed..a5a1b5b 100644 --- a/functions/[[path]].ts +++ b/functions/[[path]].ts @@ -59,28 +59,25 @@ export default { } // 静态资源和前端页面处理 - // 使用新的 Assets API - 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) + // 使用 getAssetFromKV (Workers Sites) + try { + return await getAssetFromKV( + { + request, + waitUntil: ctx.waitUntil.bind(ctx), + }, + { + ASSET_NAMESPACE: env.__STATIC_CONTENT, + ASSET_MANIFEST: env.__STATIC_CONTENT_MANIFEST, } - } - } else { - // 如果没有 ASSETS,使用 getAssetFromKV + ) + } catch (e) { + // 如果静态资源不存在,返回 index.html(SPA 路由处理) try { + const indexRequest = new Request(new URL('/index.html', request.url), request) return await getAssetFromKV( { - request, + request: indexRequest, waitUntil: ctx.waitUntil.bind(ctx), }, { @@ -88,24 +85,9 @@ export default { ASSET_MANIFEST: env.__STATIC_CONTENT_MANIFEST, } ) - } catch (e) { - // 如果静态资源不存在,返回 index.html(SPA 路由处理) - try { - 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) - } + } catch (indexError) { + // 如果 index.html 也不存在,返回内置的前端页面 + return await handleFrontendPage(request, env) } } diff --git a/wrangler.toml b/wrangler.toml index e06e70c..a11b537 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -2,9 +2,9 @@ name = "pixivnow-worker" main = "functions/[[path]].ts" compatibility_date = "2023-10-30" -# Assets 配置 - 用于部署静态资源 -[assets] -directory = "./dist" +# Workers Sites 配置 - 用于部署静态资源 +[site] +bucket = "./dist" # 环境变量配置 # 这些变量都有默认值,可以根据需要自定义