Skip to content

Commit 0c4f087

Browse files
feat(analytics): upload source maps to PostHog on production builds
Wrap the Next.js config with @posthog/nextjs-config (outermost wrapper, required for its build hooks to run) so client source maps are uploaded to PostHog and deleted from the build output afterwards. Upload is enabled only when POSTHOG_API_KEY and POSTHOG_PROJECT_ID are set and SKIP_ENV_VALIDATION is not (Nix sandbox builds have no network): the plugin throws at config-load time if enabled without credentials, which would otherwise break dev and deploy builds missing the env vars.
1 parent 5e4d9dc commit 0c4f087

4 files changed

Lines changed: 116 additions & 8 deletions

File tree

.env.example

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,10 @@ BASIC_AUTH_PASSWORD=
116116
# EU cloud project token (public by design, shipped to the browser).
117117
# Leave empty to disable analytics entirely.
118118
NEXT_PUBLIC_POSTHOG_PROJECT_TOKEN=
119+
120+
# Source map upload on production builds (optional).
121+
# Personal API key (secret) and numeric project ID; leave empty to skip uploading.
122+
POSTHOG_API_KEY=
123+
POSTHOG_PROJECT_ID=
124+
# PostHog instance the source maps are uploaded to, e.g. https://eu.i.posthog.com
125+
NEXT_PUBLIC_POSTHOG_HOST=

next.config.mjs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import './src/env.mjs';
22
import createNextIntlPlugin from 'next-intl/plugin';
3+
import { withPostHogConfig } from "@posthog/nextjs-config";
34

45
// Log which DB the build will use (host + db name only, no credentials)
56
try {
@@ -84,5 +85,19 @@ const nextConfig = {
8485
skipTrailingSlashRedirect: true,
8586
};
8687

87-
export default withNextIntl(nextConfig);
88+
export default withPostHogConfig(withNextIntl(nextConfig), {
89+
personalApiKey: process.env.POSTHOG_API_KEY,
90+
projectId: process.env.POSTHOG_PROJECT_ID,
91+
host: process.env.NEXT_PUBLIC_POSTHOG_HOST,
92+
sourcemaps: {
93+
// SKIP_ENV_VALIDATION=1 is set in Nix sandbox builds where network is blocked.
94+
// Disable there to prevent upload failures; production (DO App Platform) uses npm run build directly.
95+
// The plugin throws at config-load time if enabled without credentials, so also
96+
// require them to be present (contributors without a .env.local, deploys without the vars).
97+
enabled: !process.env.SKIP_ENV_VALIDATION
98+
&& !!process.env.POSTHOG_API_KEY
99+
&& !!process.env.POSTHOG_PROJECT_ID,
100+
deleteAfterUpload: true,
101+
},
102+
});
88103

package-lock.json

Lines changed: 92 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"@mapbox/mapbox-gl-draw": "^1.5.0",
5656
"@mux/mux-video": "^0.20.2",
5757
"@mux/mux-video-react": "^0.26.1",
58+
"@posthog/nextjs-config": "^1.9.68",
5859
"@prisma/client": "^5.22.0",
5960
"@radix-ui/react-checkbox": "^1.1.1",
6061
"@radix-ui/react-collapsible": "^1.1.0",

0 commit comments

Comments
 (0)