/** * Cloudflare Workers 静态站点服务 (Wrangler 4.x 兼容) * * @param {Request} request * @param {Object} env - 环境变量,包含 ASSETS 绑定 * @param {ExecutionContext} ctx * @returns {Promise} */ export default { async fetch(request, env, ctx) { const url = new URL(request.url); const pathname = url.pathname; // 🏠 根路径处理 if (pathname === '/' || pathname === '/index') { url.pathname = '/index.html'; } try { // 🔍 检查 ASSETS 是否存在 if (!env.ASSETS) { throw new Error('ASSETS binding not configured. Check wrangler.toml configuration.'); } // 🔍 获取静态资源 let response = await env.ASSETS.fetch(url); // 🚫 404 处理 if (response.status === 404) { try { const notFoundResponse = await env.ASSETS.fetch('/404.html'); if (notFoundResponse.ok) { response = new Response(notFoundResponse.body, { ...notFoundResponse, status: 404, statusText: 'Not Found' }); } } catch (error) { console.warn('Custom 404 page not available, using default'); response = new Response(` 404 - 页面未找到

404

抱歉,您访问的页面不存在

返回首页

`.trim(), { status: 404, headers: { 'Content-Type': 'text/html; charset=utf-8' } }); } } // ⚙️ 优化响应 return this.optimizeResponse(response, pathname); } catch (error) { console.error('Worker error:', error); return new Response(`服务暂时不可用: ${error.message}`, { status: 503, headers: { 'Content-Type': 'text/plain; charset=utf-8' } }); } }, optimizeResponse(response, pathname) { const newResponse = new Response(response.body, response); const headers = newResponse.headers; const contentType = headers.get('Content-Type') || ''; // 🔒 安全头 headers.set('X-Content-Type-Options', 'nosniff'); headers.set('X-Frame-Options', 'DENY'); headers.set('X-XSS-Protection', '1; mode=block'); headers.set('Referrer-Policy', 'strict-origin-when-cross-origin'); // 📦 智能缓存 if (/\.(js|css|png|jpg|jpeg|gif|svg|webp|woff2|woff|ttf|eot|ico|json)$/.test(pathname)) { headers.set('Cache-Control', 'public, max-age=31536000, immutable'); } else if (pathname.endsWith('.html')) { headers.set('Cache-Control', 'public, max-age=3600, stale-while-revalidate=86400'); } else { headers.set('Cache-Control', 'public, max-age=86400'); } // 🌐 CORS headers.set('Access-Control-Allow-Origin', '*'); return newResponse; } };