fix: await delivery in analytics().sendEvent()#41
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
analytics().sendEvent()awaited the dispatch kickoff but not thecompletionpromise:dispatchEventInternalreturns immediately, so backend writes (Neon insert, Jitsu POST, …) were effectively fire-and-forget.Why it matters
The middleware page-view path defers
completionwithafter()so it survives past the response. An explicitsendEventfrom 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. Soawait sendEvent()did not guarantee delivery (it works locally only because the dev server stays alive).Fix
Await
completion:Now
await sendEvent()means "delivered". The middleware page-view path is unchanged (it never callssendEvent).Testing
typecheck + all tests pass.