fix: deliver client-side templates to Pages Router clients#37
Merged
Conversation
Pages Router's getNextlyticsProps runs in getInitialProps without access to config, so it can't collect client-side templates the way App Router's NextlyticsServer does. Include templates in the /api/event response and merge them on the client from whichever source supplies them, so script insertions compile on both routers. Also stop getNextlyticsProps from throwing on client-side navigations, where _app's getInitialProps re-runs with no req. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
The client now sends the template ids it already holds in a header, and the server diffs against the templates resolved for the request, returning only the new ones. App Router clients hold the full set from the ctx prop, so they get an empty response; a Pages Router client receives each template once instead of on every event. The diff is per-request, so it stays correct when backend resolution (and thus the template set) varies by request. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
getNextlyticsProps now tolerates a missing req, so the _app example no longer needs to guard ctx.req itself — call it directly, which doubles as the documented usage. Add an initial-load test (both routers) asserting the client-side script templates compile and run. On Pages Router this only works once templates arrive via /api/event, so it guards the fix; the prior script test was App-Router-only. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 <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
Client-side templates (used to compile script insertions) were only available to App Router clients. App Router's
NextlyticsServercollects them from config and passes them via the ctx prop; Pages Router'sgetNextlyticsPropsruns insidegetInitialPropswith no access to config, so its clients never received them.This includes
templatesin every/api/eventresponse and merges them on the client from whichever source supplies them (ctx prop for App Router, response for Pages Router), so script insertions compile on both routers and survive client-side navigations.It also fixes
getNextlyticsPropsthrowing on client-side navigations, where_app'sgetInitialPropsre-runs without areq. It now returns an empty context in that case.Changes
collectTemplatescallback through the handler; includetemplatesin all/api/eventresponses./api/eventresponse.reqis absent instead of throwing.collectTemplatescallback.templatestoClientRequestResult.Testing
bun run typecheckpasses.🤖 Generated with Claude Code