From 5cea4d514b122556dbfd3a3ded470f88728e1e78 Mon Sep 17 00:00:00 2001 From: shuaiplus <2327005759@qq.com> Date: Tue, 27 May 2025 22:13:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BB=8E=E6=9C=AC=E5=9C=B0deploy=E5=88=B0worke?= =?UTF-8?q?rs=E6=88=90=E5=8A=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- worker/index.js | 67 +++++++------------------------------------------ wrangler.toml | 7 ++++-- 2 files changed, 14 insertions(+), 60 deletions(-) diff --git a/worker/index.js b/worker/index.js index 89301d4..ec8e1ba 100644 --- a/worker/index.js +++ b/worker/index.js @@ -1,74 +1,25 @@ import { generateClientId, encryptMessage, decryptMessage, logEvent, isString, isObject, getTime } from './utils.js'; -// 定义Content-Type映射 -const contentTypeMap = { - 'html': 'text/html', - 'css': 'text/css', - 'js': 'text/javascript', - 'json': 'application/json', - 'png': 'image/png', - 'jpg': 'image/jpeg', - 'jpeg': 'image/jpeg', - 'gif': 'image/gif', - 'svg': 'image/svg+xml', - 'ico': 'image/x-icon' -}; - -// 获取文件的Content-Type -function getContentType(filename) { - const ext = filename.split('.').pop().toLowerCase(); - return contentTypeMap[ext] || 'text/plain'; -} - export default { async fetch(request, env, ctx) { const url = new URL(request.url); - const path = url.pathname; - + // 处理WebSocket请求 const upgradeHeader = request.headers.get('Upgrade'); if (upgradeHeader && upgradeHeader === 'websocket') { - // Get or create a Durable Object instance const id = env.CHAT_ROOM.idFromName('chat-room'); const stub = env.CHAT_ROOM.get(id); - return stub.fetch(request); } - - // 处理静态文件请求 - try { - // 根路径重定向到index.html - if (path === '/' || path === '') { - return new Response('', { - status: 302, - headers: { - 'Location': '/index.html' - } - }); - } - - // 从KV存储或Assets中获取静态文件 - let contentPath = path; - if (contentPath.startsWith('/')) { - contentPath = contentPath.substring(1); - } - - // 这里使用了Cloudflare Workers的__STATIC_CONTENT变量来获取静态资源 - // 在实际部署时,您需要将静态文件上传到Cloudflare Workers的Assets中 - const staticContent = await env.__STATIC_CONTENT.get(contentPath, 'stream'); - - if (staticContent === null) { - return new Response('Not Found', { status: 404 }); - } - - return new Response(staticContent, { - headers: { - 'Content-Type': getContentType(contentPath) - } - }); - } catch (error) { - return new Response('Server Error: ' + error.message, { status: 500 }); + + // 处理API请求 + if (url.pathname.startsWith('/api/')) { + // ...API 逻辑... + return new Response(JSON.stringify({ ok: true }), { headers: { "Content-Type": "application/json" } }); } + + // 其余全部交给 ASSETS 处理(自动支持 hash 文件名和 SPA fallback) + return env.ASSETS.fetch(request); } }; diff --git a/wrangler.toml b/wrangler.toml index 365399d..632f37c 100644 --- a/wrangler.toml +++ b/wrangler.toml @@ -4,8 +4,11 @@ compatibility_date = "2024-09-23" compatibility_flags = ["nodejs_compat"] # 配置静态资源 -[site] -bucket = "./dist" +[assets] +directory = "./dist" +not_found_handling = "single-page-application" +run_worker_first = true +binding = "ASSETS" [durable_objects] bindings = [