Skip to content

feat(nextjs): create @posthog/nextjs package #515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open

Conversation

hpouillot
Copy link
Contributor

@hpouillot hpouillot commented May 30, 2025

Changes

  • Configure sourcemaps generation for nextjs
  • Upload sourcemaps during build process

Release info Sub-libraries affected

Bump level

  • Major
  • Minor
  • Patch

Libraries affected

  • All of them
  • posthog-web
  • posthog-node
  • posthog-ai
  • posthog-react-native
  • posthog-nextjs

Changelog notes

  • Configure sourcemaps generation for nextjs
  • Upload sourcemaps during build process

@hpouillot hpouillot marked this pull request as draft May 30, 2025 14:15
@hpouillot hpouillot requested review from a team, daibhin and oliverb123 May 30, 2025 14:15
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Introduces a new @posthog/nextjs package that provides sourcemap generation and upload capabilities during Next.js build process, with configurable options for both server and client builds.

  • Added webpack plugin in posthog-nextjs/src/webpack-plugin.ts for automated sourcemap injection using posthog-cli
  • Implemented flexible configuration system in posthog-nextjs/src/config.ts with support for async/sync Next.js configs
  • Missing critical documentation in README.md and CHANGELOG.md that needs to be added
  • Package configuration in posthog-nextjs/package.json needs review for test dependencies and peer dependency specifications
  • PR template sections for bump level and affected libraries are incomplete and should be filled out

14 files reviewed, 15 comments
Edit PR Review Bot Settings | Greptile

Copy link

github-actions bot commented May 30, 2025

Size Change: 0 B

Total Size: 323 kB

ℹ️ View Unchanged
Filename Size
posthog-ai/lib/anthropic/index.cjs 2.81 kB
posthog-ai/lib/anthropic/index.mjs 2.66 kB
posthog-ai/lib/index.cjs 13.6 kB
posthog-ai/lib/index.mjs 13.3 kB
posthog-ai/lib/langchain/index.cjs 7.72 kB
posthog-ai/lib/langchain/index.mjs 7.5 kB
posthog-ai/lib/openai/index.cjs 2.71 kB
posthog-ai/lib/openai/index.mjs 2.56 kB
posthog-ai/lib/vercel/index.cjs 3.95 kB
posthog-ai/lib/vercel/index.mjs 3.9 kB
posthog-node/lib/edge/index.cjs 35.4 kB
posthog-node/lib/edge/index.mjs 35.1 kB
posthog-node/lib/node/index.cjs 39.8 kB
posthog-node/lib/node/index.mjs 39.5 kB
posthog-react-native/lib/posthog-core/src/eventemitter.js 1.08 kB
posthog-react-native/lib/posthog-core/src/featureFlagUtils.js 1.51 kB
posthog-react-native/lib/posthog-core/src/index.js 14.9 kB
posthog-react-native/lib/posthog-core/src/lz-string.js 1.42 kB
posthog-react-native/lib/posthog-core/src/types.js 1.08 kB
posthog-react-native/lib/posthog-core/src/utils.js 1.95 kB
posthog-react-native/lib/posthog-core/src/vendor/uuidv7.js 2.04 kB
posthog-react-native/lib/posthog-react-native/index.js 477 B
posthog-react-native/lib/posthog-react-native/src/autocapture.js 1.8 kB
posthog-react-native/lib/posthog-react-native/src/frameworks/wix-navigation.js 505 B
posthog-react-native/lib/posthog-react-native/src/hooks/useFeatureFlag.js 437 B
posthog-react-native/lib/posthog-react-native/src/hooks/useFeatureFlags.js 362 B
posthog-react-native/lib/posthog-react-native/src/hooks/useNavigationTracker.js 707 B
posthog-react-native/lib/posthog-react-native/src/hooks/usePostHog.js 249 B
posthog-react-native/lib/posthog-react-native/src/legacy.js 810 B
posthog-react-native/lib/posthog-react-native/src/native-deps.js 1.34 kB
posthog-react-native/lib/posthog-react-native/src/optional/OptionalAsyncStorage.js 183 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalExpoApplication.js 215 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalExpoDevice.js 211 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalExpoFileSystem.js 224 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalExpoLocalization.js 216 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeDeviceInfo.js 220 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeLocalize.js 169 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeNavigation.js 218 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeNavigationWix.js 222 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalReactNativeSafeArea.js 312 B
posthog-react-native/lib/posthog-react-native/src/optional/OptionalSessionReplay.js 231 B
posthog-react-native/lib/posthog-react-native/src/posthog-rn.js 5.09 kB
posthog-react-native/lib/posthog-react-native/src/PostHogContext.js 210 B
posthog-react-native/lib/posthog-react-native/src/PostHogProvider.js 1.75 kB
posthog-react-native/lib/posthog-react-native/src/storage.js 1.09 kB
posthog-react-native/lib/posthog-react-native/src/surveys/components/BottomSection.js 630 B
posthog-react-native/lib/posthog-react-native/src/surveys/components/Cancel.js 527 B
posthog-react-native/lib/posthog-react-native/src/surveys/components/ConfirmationMessage.js 738 B
posthog-react-native/lib/posthog-react-native/src/surveys/components/QuestionHeader.js 541 B
posthog-react-native/lib/posthog-react-native/src/surveys/components/QuestionTypes.js 2.81 kB
posthog-react-native/lib/posthog-react-native/src/surveys/components/SurveyModal.js 1.6 kB
posthog-react-native/lib/posthog-react-native/src/surveys/components/Surveys.js 1.85 kB
posthog-react-native/lib/posthog-react-native/src/surveys/getActiveMatchingSurveys.js 902 B
posthog-react-native/lib/posthog-react-native/src/surveys/icons.js 1.86 kB
posthog-react-native/lib/posthog-react-native/src/surveys/index.js 222 B
posthog-react-native/lib/posthog-react-native/src/surveys/PostHogSurveyProvider.js 1.82 kB
posthog-react-native/lib/posthog-react-native/src/surveys/surveys-utils.js 2.33 kB
posthog-react-native/lib/posthog-react-native/src/surveys/useActivatedSurveys.js 1.41 kB
posthog-react-native/lib/posthog-react-native/src/surveys/useSurveyStorage.js 690 B
posthog-react-native/lib/posthog-react-native/src/types.js 90 B
posthog-react-native/lib/posthog-react-native/src/version.js 124 B
posthog-web/lib/index.cjs 26.4 kB
posthog-web/lib/index.mjs 26.4 kB

compressed-size-action

@hpouillot hpouillot marked this pull request as ready for review June 5, 2025 10:09
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

Significant changes made to the build and release infrastructure to support the new @posthog/nextjs package.

  • Added posthog-nextjs to release workflow matrix in .github/workflows/release.yml for npm publishing
  • Rollup configuration in rollup.config.js expanded to build NextJS package with ESM bundle and TypeScript declarations
  • Package template files (PR, bug report, feature request) updated to include posthog-nextjs references
  • Root package.json needs updating - test scripts and version bump not properly configured
  • Edge runtime support marked as TODO in webpack-plugin.ts, needs implementation before release

13 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines +25 to +28
"dependencies": {
"@posthog/cli": "^0.1.0",
"rimraf": "5.0.9"
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should these be peer dependencies?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For me they are real deps. Users should not worry about how we implemented it, it could be using cli or our own js implementation.

],
"devDependencies": {
"@types/node": "^22.15.23",
"next": "^12.1.0"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this need in both?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, peerDependencies are not installed when running 'yarn install' and you need them to dev (type checking and so on).

import { rimraf } from 'rimraf'
import { spawn } from 'child_process'

type NextRuntime = 'edge' | 'nodejs' | undefined
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is fluid compute a new runtime? https://vercel.com/fluid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so, maybe a mix of node and edge.

NextRuntime

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing the idea is not to make this a full replacement until we have isomorphic capabilities? I would have expected that I only needed to install @posthog/nextjs but I guess I still need posthog-node. Happy if that's something we want to add later

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was the initial idea but the type are too different for now. posthog.init vs new Posthog, capture(event) vs capture({event})`

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need to do to make types the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't check what needs to be changed in details but basically the idea is to make sure posthog-node and posthog-js have the same public API. It was clearly out of this PR scope and it should probably be part of broader effort to share more code and have an unified API design across SDKs.

@@ -0,0 +1,98 @@
import { PostHogNextConfigComplete } from './config'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the plan to move this out of the nextjs repo so that it can be used by other frameworks / as a bundler plugin?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This webpack plugin is really next specific but we could probably make it more generic, if there is a need at some point

Comment on lines +81 to +82
authToken,
envId,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are these for? They dont follow the same names we have in our other SDKs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use those variables to upload sourcemaps during the nextjs build process. I reused the ones from the CLI => https://www.npmjs.com/package/@posthog/cli

I could rename authToken to personalApiKey to SDK convention, do we use the envId anywhere ?

Copy link
Member

@rafaeelaudibert rafaeelaudibert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this is here and not on posthog-js where we already include some stuff for NextJS - such as a PostHogProvider

I'm slightly skeptical of releasing this as is and getting people to use it but it doesnt contain all of the nice stuff we already have on @posthog-js/react meant to be used with Next. Docs will also be crazy. Is the reason behind this to simplify error tracking setup for NextJS? I think we need a solution that's easily extensible rather than a one-off

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do we need to do to make types the same?

@hpouillot
Copy link
Contributor Author

It seemed more natural to put it here as this repository is a workspace and allows the creation and publication of new packages easily. Also the initial goal was to make this library isomorphic and I needed to import posthog-js and posthog-node to do so. It turned out more complicated than I anticipated and decided to focus on sourcemap upload for now as most of our users are on vercel and deployment using a CLI requires extra configuration. See this thread.

The @posthog/react package does not exists yet, it's distributed inside posthog-js but I agree, it should be reexported by this package and more globally this package should be the only one you need if you're using the nextjs framework and it should also handle broader posthog configuration (like proxy rules and so on)

If we don't want to release this package yet because we want to put more thoughts into it, I don't mind distributing those features as an entrypoint inside the posthog-node package in the meantime.

import {withPosthHogConfig} from "posthog-node/nextjs"

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.

3 participants