Upload source maps for Angular
Contents
posthog-cli sourcemap inject changes JavaScript files. If you run it after Angular creates ngsw.json, the service-worker hashes no longer match and Angular rejects the application update. Use the build plugin below so Angular hashes the final JavaScript.
Setup
- 1
Install the PostHog CLI
RequiredInstall
posthog-cli: - 2
Install the esbuild integration
RequiredInstall the PostHog esbuild plugin and the community Angular custom-esbuild builder:
TerminalUse the
@angular-builders/custom-esbuildmajor version that matches your Angular major version. For example, use version 20 for Angular 20. - 3
Authenticate the PostHog CLI
RequiredTo authenticate the CLI, call the
logincommand. This opens your browser where you select your organization, project, and API scopes to grant:TerminalIf you are using the CLI in a CI/CD environment such as GitHub Actions, you can set environment variables to authenticate:
Environment Variable Description Source POSTHOG_CLI_HOSTThe PostHog host to connect to [default: https://us.posthog.com] Project settings POSTHOG_CLI_PROJECT_IDPostHog project ID Project settings POSTHOG_CLI_API_KEYPersonal API key with error tracking writeandorganization readscopesAPI key settings You can also use the
--hostoption instead of thePOSTHOG_CLI_HOSTenvironment variable to target a different PostHog instance or region. For EU users:Terminal - 4
Configure the esbuild plugin
RequiredCreate a workspace-local plugin file:
tools/posthog-esbuild-plugin.tsUpdate your production build target in
angular.json:angular.jsonKeep
outputHashingenabled. The plugin uses each content-hashed JavaScript filename as its symbol-set ID. - 5
Build your application
RequiredTerminalThe plugin adds its runtime registration before esbuild computes output hashes. Angular then creates
index.htmlandngsw.jsonfrom the final JavaScript. Verify the build output
CheckpointConfirm PostHog metadata is presentCheck a generated JavaScript file in
dist/<your-app-name>/browser. It must contain this static marker:JavaScriptOpen the matching
.js.mapfile and confirm itschunk_idequals the JavaScript filename. For example:JSONYou won't see a
//# chunkId=comment. The plugin registers the final filename at runtime instead of adding a random ID after the build.- 6
Upload source maps
RequiredUpload the already-processed output without injecting it again:
TerminalDo not use
sourcemap process,sourcemap inject, orsourcemap upload --delete-afterafter this build. Those commands rewrite JavaScript and invalidate hashes that Angular has already computed.If you don't deploy source maps, remove only the
.mapfiles after a successful upload:Terminal
Fallback without a custom builder
If you can't replace Angular's standard builder, regenerate the service-worker manifest after PostHog changes the bundles:
Run ngsw-config after every command that changes a built asset. This fallback does not repair SRI values or custom CDN manifests. Regenerate those separately after injection.