Skip to content

fix: await delivery in analytics().sendEvent()#41

Merged
vklimontovich merged 1 commit into
mainfrom
fix/sendevent-await-completion
Jun 5, 2026
Merged

fix: await delivery in analytics().sendEvent()#41
vklimontovich merged 1 commit into
mainfrom
fix/sendevent-await-completion

Conversation

@vklimontovich

Copy link
Copy Markdown
Collaborator

What

analytics().sendEvent() awaited the dispatch kickoff but not the completion promise:

await dispatchEventInternal(event, ctx); // returns { clientActions, completion } synchronously
return { ok: true };

dispatchEventInternal returns immediately, so backend writes (Neon insert, Jitsu POST, …) were effectively fire-and-forget.

Why it matters

The middleware page-view path defers completion with after() so it survives past the response. An explicit sendEvent from an API route / server action has no such hook — in a serverless function the process can freeze right after the response returns, dropping the in-flight writes. So await sendEvent() did not guarantee delivery (it works locally only because the dev server stays alive).

Fix

Await completion:

const { completion } = dispatchEventInternal(event, ctx);
await completion;
return { ok: true };

Now await sendEvent() means "delivered". The middleware page-view path is unchanged (it never calls sendEvent).

Testing

typecheck + all tests pass.

sendEvent dispatched the event but only awaited the dispatch kickoff, not the
`completion` promise, so backend writes were effectively fire-and-forget. In a
serverless function the process can freeze right after the response, dropping the
in-flight writes — so `await sendEvent()` did not actually guarantee delivery.

Await `completion` instead. The middleware page-view path is unaffected: it still
defers completion with `after()` and never calls sendEvent.
@vklimontovich vklimontovich merged commit 70030dd into main Jun 5, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant