SvelteKit部署Vercel后随机函数崩溃问题求助
SvelteKit电商应用部署到Vercel后随机函数崩溃问题
问题描述
我开发的SvelteKit电商应用在本地运行完全正常,但部署到Vercel后会随机出现函数崩溃,所有URL都受影响,这严重影响了电商转化率。已经尝试更换svelte.config.js中的适配器为@sveltejs/adapter-vercel,但问题仍未解决。
错误日志
[GET] /proizvodi/9?fbclid=PAAaY2n3yQfX-Wzxs5pTMWeP9nMUkdIv2V4qg8kObIDQgUAdPnwLtWEr4ICYg_aem_AT5iXQ0y2WTFTVjFAkqIfOf-vx44GuIUXoJKxT5IunSH48Cf1QVjlfC8Q1rKapxuuR9daAL70GWwpLZ_rINriwy815oEYfwfXFvOuh3HvHBvhWRFkVdfeQom7PK6UZvTp0s 02:39:09:44 2023-01-26T01:39:09.507Z f0d6641c-416c-4c0a-ba67-c59890f6f936 ERROR [2023-01-26T01:39:09.507Z] @firebase/firestore: Firestore (9.15.0): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds. This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend. 2023-01-26T01:39:09.539Z efb98c51-3cde-4791-981f-5d22145fe3c0 ERROR Unhandled Promise Rejection {"errorType":"Runtime.UnhandledPromiseRejection","errorMessage":"TypeError: fetch failed","reason":{"errorType":"TypeError","errorMessage":"fetch failed","cause":{"errorType":"Error","errorMessage":"Client network socket disconnected before secure TLS connection was established","code":"ECONNRESET","host":"translate.googleapis.com","port":443,"localAddress":null,"stack":["Error: Client network socket disconnected before secure TLS connection was established"," at connResetException (node:internal/errors:711:14)"," at TLSSocket.onConnectEnd (node:_tls_wrap:1593:19)"," at TLSSocket.emit (node:events:525:35)"," at endReadableNT (node:internal/streams/readable:1359:12)"," at process.processTicksAndRejections (node:internal/process/task_queues:82:21)"]},"stack":["TypeError: fetch failed"," at fetch (/var/task/node_modules/undici/index.js:105:13)"," at process.processTicksAndRejections (node:internal/process/task_queues:95:5)"]},"promise":{},"stack":["Runtime.UnhandledPromiseRejection: TypeError: fetch failed"," at process.<anonymous> (file:///var/runtime/index.mjs:1194:17)"," at process.emit (node:events:525:35)"," at emit (node:internal/process/promises:149:20)"," at processPromiseRejections (node:internal/process/promises:283:27)"," at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]} Unknown application error occurred Runtime.Unknown
解决方案
1. 处理Firestore连接超时问题
- 给Firestore请求添加重试逻辑,避免单次网络波动导致超时。可以封装Firestore调用函数,在超时或连接失败时自动重试2-3次:
import { getFirestore } from 'firebase/firestore'; async function firestoreRequestWithRetry(fn, retries = 3) { try { return await fn(); } catch (error) { if (retries > 0 && error.message.includes('Could not reach Cloud Firestore backend')) { await new Promise(resolve => setTimeout(resolve, 1500)); return firestoreRequestWithRetry(fn, retries - 1); } throw error; } } // 使用示例 const db = getFirestore(); const doc = await firestoreRequestWithRetry(() => getDoc(doc(db, 'proizvodi', '9'))); - 检查Vercel的区域配置,选择距离Firestore实例最近的部署区域,减少网络延迟。
2. 修复Google翻译API的TLS连接重置问题
- 为调用
translate.googleapis.com的fetch请求添加重试机制,针对ECONNRESET错误进行重试:async function fetchTranslateWithRetry(url, options = {}, retries = 3) { try { return await fetch(url, options); } catch (error) { if (retries > 0 && error.cause?.code === 'ECONNRESET') { await new Promise(resolve => setTimeout(resolve, 1000)); return fetchTranslateWithRetry(url, options, retries - 1); } throw error; } } - 确认项目使用的Node.js版本与Vercel配置一致,在
package.json中指定版本:"engines": { "node": ">=18.x" }
3. 捕获未处理的Promise拒绝
在SvelteKit的hooks.server.js中添加全局捕获逻辑,避免未处理的Promise直接导致函数崩溃:
process.on('unhandledRejection', (reason, promise) => { console.error('未处理的Promise拒绝:', reason); // 可添加日志上报等兜底处理 });
内容的提问来源于stack exchange,提问作者Jaksa
相关产品推荐
相关产品推荐

