feat: support analytics().sendEvent() from Pages Router API routes#39
Merged
Conversation
analytics() now accepts an optional request (NextRequest | NextApiRequest). Pages Router API routes pass req, since next/headers throws there; App Router keeps calling it with no args. Standalone routes with no page render (email pixels, redirects) send without a parent page-view. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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()now accepts an optional request soanalytics().sendEvent()works in both routers:analytics()with no args — unchanged, context comes fromnext/headers.analytics(req).next/headersthrows there, so context is built fromreq.headers/req.cookies/req.url.NextRequest(App Router route handler) is also accepted.Why
The server API was App-Router-only because
analytics()reads fromnext/headers, which throws in Pages Router API routes (req/res). There was no nextlytics-native way to emit a server event from those routes — e.g. email pixel / redirect endpoints that genuinely need to be Pages Router.How
The three context sources are unified behind a
NormalizedRequestshape, thenbuildServerContext()consumes it (replacing the App-Router-onlycreateServerContextFromHeaders). Standalone routes with no preceding page render (email pixels, redirects) now send without a parent page-view —parentEventIdis omitted rather than failing. The "middleware not configured" hard error is preserved only for the no-arg App Router path.Server context
methodis now the real request method instead of a hardcoded"GET".Notes
analytics(), no new anon cookie is written (existing cookies are read; GDPR-hash mode works).typecheck, lint, build, tests, and format check all pass.
🤖 Generated with Claude Code