fix next/pages server-only components from being imported in the client#3533
Open
brandonhines-mialabs wants to merge 2 commits intoPostHog:mainfrom
Open
fix next/pages server-only components from being imported in the client#3533brandonhines-mialabs wants to merge 2 commits intoPostHog:mainfrom
brandonhines-mialabs wants to merge 2 commits intoPostHog:mainfrom
Conversation
|
@brandonhines-mialabs is attempting to deploy a commit to the PostHog Team on Vercel. A member of the Team first needs to authorize it. |
Contributor
Prompt To Fix All With AIFix the following 2 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 2
packages/next/src/pages.client.ts:1-7
`DEFAULT_INGEST_PATH` is a plain string constant (`'/ingest'`) that carries no `server-only` or `posthog-node` dependency, but it is omitted from this client barrel while still being declared in the shared `pages.d.ts` (generated from `pages.ts`). Because `types` is not conditioned per-runtime, TypeScript always resolves against the full type surface — so `import { DEFAULT_INGEST_PATH } from '@posthog/next/pages'` type-checks cleanly but evaluates to `undefined` at runtime in any browser bundle. A `pages/_app.tsx` that passes `DEFAULT_INGEST_PATH` as `api_host` would silently break PostHog initialisation on the client.
```suggestion
// Client-runtime barrel for the `./pages` subpath. Resolved by Next.js's
// `browser` exports condition. Excludes `getPostHog`, `getServerSidePostHog`,
// and `postHogMiddleware`, which import `server-only` or `posthog-node` and
// must not be reachable from a client bundle.
export { PostHogProvider } from './pages/PostHogProvider.js'
export { PostHogPageView } from './pages/PostHogPageView.js'
export { DEFAULT_INGEST_PATH } from './shared/constants.js'
export type { PagesPostHogProviderProps } from './pages/PostHogProvider.js'
```
### Issue 2 of 2
packages/next/tests/barrelExports.client.test.ts:41-46
**Prefer parameterised tests**
The "omits" and "exposes" checks across both `barrelExports.*.test.ts` files repeat the same `expect(typeof m.X).toBe(...)` / `expect(m.X).toBeUndefined()` pattern once per symbol. Per the project's preference for parameterised tests, these could use `it.each` with `[symbolName, expectedType]` rows — both for conciseness and so that a future export addition shows up as a new row rather than a new `expect` buried in a block. The same pattern applies to the equivalent blocks in `barrelExports.server.test.ts`.
Reviews (1): Last reviewed commit: "fix next/pages server-only components fr..." | Re-trigger Greptile |
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.
Problem
@posthog/next/pagesdoesn't build in a Next.js Pages Router app following the documented setup. The./pagessubpath inpackage.json#exportshad no per-runtime conditions, so animportfrompages/_app.tsxtransitively pulled inposthog-nodeand'server-only'. Next.js rejects'server-only'in client modules before tree-shaking can drop the unused re-exports, sonext buildfails with:Pages Router users hit this on every Next version that ships the
server-onlyenforcement (Next 13+).Changes
Split the
./pagesbarrel per runtime, mirroring the pattern already used for the.entry:src/pages.client.ts—browsercondition. Re-exportsPostHogProviderandPostHogPageViewonly.src/pages.edge.ts—edge-light/edge/workerconditions. Re-exportspostHogMiddleware,PostHogPageView,DEFAULT_INGEST_PATH.src/pages.ts—react-server/defaultconditions. Unchanged; keeps the full Pages Router surface (getServerSidePostHog,getPostHog, etc.).Wired into
package.json#exports:Existing consumer imports (
from '@posthog/next/pages') keep working unchanged; the resolver picks the right file per runtime.Libraries affected
Checklist
If releasing new changes
pnpm changesetto generate a changeset file