Skip to content

Commit 355cede

Browse files
authored
fix(sveltekit): ignore undefined rejections instead of setting global context (#217)
* fix: ignore undefined rejections instead of setting global req context * changeset * chore: add comment for info
1 parent 9755566 commit 355cede

File tree

2 files changed

+8
-30
lines changed

2 files changed

+8
-30
lines changed

.changeset/heavy-terms-share.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@workflow/sveltekit": patch
3+
---
4+
5+
Replace setting global symbol with catching unhandled rejection in SvelteKit integration

packages/sveltekit/src/builder.ts

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,9 @@ export const POST = async ({request}) => {
160160
// Post-process the generated file to wrap with SvelteKit request converter
161161
let webhookRouteContent = await readFile(webhookRouteFile, 'utf-8');
162162

163-
// For local dev (node), need this since context isn't available to waitUntil()
164-
// Add SYMBOL_FOR_REQ_CONTEXT at the top after imports
165-
webhookRouteContent = webhookRouteContent.replace(
166-
/(import.*?;)/,
167-
`$1\n\nconst SYMBOL_FOR_REQ_CONTEXT = Symbol.for('@vercel/request-context');`
168-
);
163+
// NOTE: This is a workaround to avoid crashing in local dev when context isn't set for waitUntil()
164+
webhookRouteContent = `process.on('unhandledRejection', (reason) => { if (reason !== undefined) console.error('Unhandled rejection detected', reason); });
165+
${webhookRouteContent}`;
169166

170167
// Update handler signature to accept token as parameter
171168
webhookRouteContent = webhookRouteContent.replace(
@@ -184,32 +181,8 @@ export const POST = async ({request}) => {
184181
/export const GET = handler;\nexport const POST = handler;\nexport const PUT = handler;\nexport const PATCH = handler;\nexport const DELETE = handler;\nexport const HEAD = handler;\nexport const OPTIONS = handler;/,
185182
`${SVELTEKIT_REQUEST_CONVERTER}
186183
const createSvelteKitHandler = (method) => async ({ request, params, platform }) => {
187-
// Track background promises for local dev
188-
const backgroundPromises = [];
189-
190-
// Set up context for @vercel/functions waitUntil
191-
const context = {
192-
waitUntil: platform?.waitUntil || ((promise) => {
193-
// Fallback for local dev/tests: collect promises to await them
194-
backgroundPromises.push(promise.catch(err => console.error('Background task error:', err)));
195-
})
196-
};
197-
198-
// Only set context if it doesn't already exist (Vercel sets it and makes it read-only)
199-
if (!globalThis[SYMBOL_FOR_REQ_CONTEXT]) {
200-
globalThis[SYMBOL_FOR_REQ_CONTEXT] = {
201-
get: () => context
202-
};
203-
}
204-
205184
const normalRequest = await convertSvelteKitRequest(request);
206185
const response = await handler(normalRequest, params.token);
207-
208-
// In local dev (no platform.waitUntil), await background tasks before returning
209-
if (!platform?.waitUntil && backgroundPromises.length > 0) {
210-
await Promise.all(backgroundPromises);
211-
}
212-
213186
return response;
214187
};
215188

0 commit comments

Comments
 (0)