@@ -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- / ( i m p o r t .* ?; ) / ,
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 / e x p o r t c o n s t G E T = h a n d l e r ; \n e x p o r t c o n s t P O S T = h a n d l e r ; \n e x p o r t c o n s t P U T = h a n d l e r ; \n e x p o r t c o n s t P A T C H = h a n d l e r ; \n e x p o r t c o n s t D E L E T E = h a n d l e r ; \n e x p o r t c o n s t H E A D = h a n d l e r ; \n e x p o r t c o n s t O P T I O N S = h a n d l e r ; / ,
185182 `${ SVELTEKIT_REQUEST_CONVERTER }
186183const 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