Skip to content

Conversation

pauldambra
Copy link
Member

@pauldambra pauldambra commented Aug 26, 2025

a lot of the session replay code predates (or at least overlapped with the introduction of) our lazy loading
so, even though we lazy load rrweb
a large chunk of the bundle is the code that interacts with rrweb

this PR moves as much as possible of that code into a lazy loaded extension

what does this change?

  • for customers that aren't enabling replay the bundle is ~14% smaller

  • for customers that are enabling replay we've removed the race between starting rrweb and receiving remoteconfig, this is probably ok, since remote config needs to be fast for flags, but does mean some customers might see replay start very slight later than today... but given we (want to) control what script to load via remote config and allow configuring masking via remote config this is probably desirable overall

Copy link

vercel bot commented Aug 26, 2025

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Preview Updated (UTC)
posthog-js Ready Ready Preview Sep 4, 2025 9:35pm

Copy link

github-actions bot commented Aug 26, 2025

Copy link

github-actions bot commented Aug 26, 2025

Size Change: -78.7 kB (-1.5%)

Total Size: 5.15 MB

Filename Size Change
packages/browser/dist/all-external-dependencies.js 254 kB +33.1 kB (+14.95%) ⚠️
packages/browser/dist/array.full.es5.js 281 kB -33.1 kB (-10.54%) 👏
packages/browser/dist/array.full.js 377 kB -839 B (-0.22%)
packages/browser/dist/array.full.no-external.js 394 kB -979 B (-0.25%)
packages/browser/dist/array.js 150 kB -26.5 kB (-15.02%) 👏
packages/browser/dist/array.no-external.js 161 kB -31.3 kB (-16.27%) 👏
packages/browser/dist/lazy-recorder.js 147 kB +58 B (+0.04%)
packages/browser/dist/main.js 151 kB -26.5 kB (-14.94%) 👏
packages/browser/dist/module.full.js 377 kB -839 B (-0.22%)
packages/browser/dist/module.full.no-external.js 394 kB -979 B (-0.25%)
packages/browser/dist/module.js 151 kB -26.5 kB (-14.95%) 👏
packages/browser/dist/module.no-external.js 162 kB -31.3 kB (-16.2%) 👏
packages/browser/dist/recorder-v2.js 147 kB +33.5 kB (+29.65%) 🚨
packages/browser/dist/recorder.js 147 kB +33.5 kB (+29.65%) 🚨
ℹ️ View Unchanged
Filename Size
packages/ai/dist/anthropic/index.cjs 16.1 kB
packages/ai/dist/anthropic/index.mjs 16 kB
packages/ai/dist/gemini/index.cjs 15.4 kB
packages/ai/dist/gemini/index.mjs 15.2 kB
packages/ai/dist/index.cjs 119 kB
packages/ai/dist/index.mjs 118 kB
packages/ai/dist/langchain/index.cjs 39.6 kB
packages/ai/dist/langchain/index.mjs 39 kB
packages/ai/dist/openai/index.cjs 29 kB
packages/ai/dist/openai/index.mjs 28.8 kB
packages/ai/dist/vercel/index.cjs 21.9 kB
packages/ai/dist/vercel/index.mjs 21.9 kB
packages/browser/dist/crisp-chat-integration.js 1.97 kB
packages/browser/dist/customizations.full.js 16.1 kB
packages/browser/dist/dead-clicks-autocapture.js 12.6 kB
packages/browser/dist/exception-autocapture.js 9.55 kB
packages/browser/dist/external-scripts-loader.js 2.81 kB
packages/browser/dist/intercom-integration.js 2.02 kB
packages/browser/dist/posthog-recorder.js 241 kB
packages/browser/dist/surveys-preview.js 71.1 kB
packages/browser/dist/surveys.js 79.8 kB
packages/browser/dist/tracing-headers.js 1.84 kB
packages/browser/dist/web-vitals.js 10.4 kB
packages/browser/react/dist/esm/index.js 14.8 kB
packages/browser/react/dist/umd/index.js 17.5 kB
packages/core/dist/eventemitter.js 1.78 kB
packages/core/dist/eventemitter.mjs 571 B
packages/core/dist/featureFlagUtils.js 7.56 kB
packages/core/dist/featureFlagUtils.mjs 5.34 kB
packages/core/dist/gzip.js 1.95 kB
packages/core/dist/gzip.mjs 650 B
packages/core/dist/index.js 71.8 kB
packages/core/dist/index.mjs 59.2 kB
packages/core/dist/testing/index.js 2.93 kB
packages/core/dist/testing/index.mjs 79 B
packages/core/dist/testing/PostHogCoreTestClient.js 3.5 kB
packages/core/dist/testing/PostHogCoreTestClient.mjs 2.11 kB
packages/core/dist/testing/test-utils.js 2.35 kB
packages/core/dist/testing/test-utils.mjs 783 B
packages/core/dist/types.js 8.2 kB
packages/core/dist/types.mjs 5.93 kB
packages/core/dist/utils/bucketed-rate-limiter.js 3.4 kB
packages/core/dist/utils/bucketed-rate-limiter.mjs 2.02 kB
packages/core/dist/utils/index.js 9.33 kB
packages/core/dist/utils/index.mjs 1.95 kB
packages/core/dist/utils/number-utils.js 2 kB
packages/core/dist/utils/number-utils.mjs 735 B
packages/core/dist/utils/promise-queue.js 2 kB
packages/core/dist/utils/promise-queue.mjs 768 B
packages/core/dist/utils/string-utils.js 1.91 kB
packages/core/dist/utils/string-utils.mjs 414 B
packages/core/dist/utils/type-utils.js 5.2 kB
packages/core/dist/utils/type-utils.mjs 1.95 kB
packages/core/dist/vendor/uuidv7.js 8.57 kB
packages/core/dist/vendor/uuidv7.mjs 7 kB
packages/nextjs-config/dist/config.js 5.51 kB
packages/nextjs-config/dist/config.mjs 4.03 kB
packages/nextjs-config/dist/index.js 2.24 kB
packages/nextjs-config/dist/index.mjs 30 B
packages/nextjs-config/dist/utils.js 6.04 kB
packages/nextjs-config/dist/utils.mjs 3.51 kB
packages/nextjs-config/dist/utils.spec.js 723 B
packages/nextjs-config/dist/utils.spec.mjs 455 B
packages/nextjs-config/dist/webpack-plugin.js 3.69 kB
packages/nextjs-config/dist/webpack-plugin.mjs 1.98 kB
packages/node/dist/edge/index.cjs 109 kB
packages/node/dist/edge/index.mjs 109 kB
packages/node/dist/node/index.cjs 123 kB
packages/node/dist/node/index.mjs 123 kB
packages/react-native/dist/autocapture.js 4.68 kB
packages/react-native/dist/frameworks/wix-navigation.js 1.3 kB
packages/react-native/dist/hooks/useFeatureFlag.js 1.49 kB
packages/react-native/dist/hooks/useFeatureFlags.js 821 B
packages/react-native/dist/hooks/useNavigationTracker.js 2.46 kB
packages/react-native/dist/hooks/usePostHog.js 467 B
packages/react-native/dist/index.js 3.12 kB
packages/react-native/dist/native-deps.js 13.9 kB
packages/react-native/dist/optional/OptionalAsyncStorage.js 299 B
packages/react-native/dist/optional/OptionalExpoApplication.js 377 B
packages/react-native/dist/optional/OptionalExpoDevice.js 347 B
packages/react-native/dist/optional/OptionalExpoFileSystem.js 386 B
packages/react-native/dist/optional/OptionalExpoFileSystemLegacy.js 423 B
packages/react-native/dist/optional/OptionalExpoLocalization.js 383 B
packages/react-native/dist/optional/OptionalReactNativeDeviceInfo.js 415 B
packages/react-native/dist/optional/OptionalReactNativeLocalize.js 303 B
packages/react-native/dist/optional/OptionalReactNativeNavigation.js 415 B
packages/react-native/dist/optional/OptionalReactNativeNavigationWix.js 443 B
packages/react-native/dist/optional/OptionalReactNativeSafeArea.js 644 B
packages/react-native/dist/optional/OptionalSessionReplay.js 455 B
packages/react-native/dist/posthog-rn.js 28.7 kB
packages/react-native/dist/PostHogContext.js 329 B
packages/react-native/dist/PostHogProvider.js 4.77 kB
packages/react-native/dist/storage.js 3.39 kB
packages/react-native/dist/surveys/components/BottomSection.js 1.34 kB
packages/react-native/dist/surveys/components/Cancel.js 909 B
packages/react-native/dist/surveys/components/ConfirmationMessage.js 1.63 kB
packages/react-native/dist/surveys/components/QuestionHeader.js 1.11 kB
packages/react-native/dist/surveys/components/QuestionTypes.js 10.1 kB
packages/react-native/dist/surveys/components/SurveyModal.js 3.95 kB
packages/react-native/dist/surveys/components/Surveys.js 6.18 kB
packages/react-native/dist/surveys/getActiveMatchingSurveys.js 3.48 kB
packages/react-native/dist/surveys/icons.js 7.76 kB
packages/react-native/dist/surveys/index.js 600 B
packages/react-native/dist/surveys/PostHogSurveyProvider.js 5.61 kB
packages/react-native/dist/surveys/surveys-utils.js 8.96 kB
packages/react-native/dist/surveys/useActivatedSurveys.js 3.38 kB
packages/react-native/dist/surveys/useSurveyStorage.js 2.16 kB
packages/react-native/dist/types.js 70 B
packages/react-native/dist/version.js 129 B
packages/react/dist/esm/index.js 14.8 kB
packages/react/dist/umd/index.js 17.5 kB
packages/web/dist/index.cjs 13.8 kB
packages/web/dist/index.mjs 13.7 kB
tooling/rollup-utils/dist/index.js 1.17 kB

compressed-size-action

@pauldambra pauldambra changed the title feat: lazy load session replay 3000 feat: lazy load much more of session replay Aug 27, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Implements lazy loading for the majority of session recording code, reducing bundle size by ~14% for customers who don't enable replay, and fixing a race condition between starting rrweb and receiving remote config.

  • Consolidates the SessionRecording class into a lightweight wrapper that manages lazy loading
  • Removes the SessionRecordingWrapper class which was used for testing
  • Moves most session recording logic into a lazy-loaded extension

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/browser/terser-mangled-names.json Updates mangled names list for refactored code
packages/browser/src/types.ts Deprecates preview flag that's no longer needed
packages/browser/src/posthog-core.ts Simplifies initialization to always use new lazy loading approach
packages/browser/src/extensions/replay/sessionrecording.ts Major refactor to lightweight wrapper managing lazy loading
packages/browser/src/extensions/replay/sessionrecording-wrapper.ts Removes wrapper class (entire file deleted)
packages/browser/src/extensions/replay/sessionrecording-utils.ts Removes buffer splitting utilities
packages/browser/src/entrypoints/recorder.ts Adds initialization function for lazy-loaded recording
packages/browser/src/tests/extensions/replay/ Updates tests to work with new lazy loading structure

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

Comment on lines +2 to +6
testMatch: [
'**/__tests__/**/*.test.[jt]s?(x)',
'**/?(*.)+test.[jt]s?(x)',
'**/functional_tests/**/*.test.[jt]s?(x)',
],
Copy link
Member Author

Choose a reason for hiding this comment

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

i had to do this because i extracted a utils.ts file in the tests folder and then jest was unhappy because it was trying to run it as a test file

Copy link
Contributor

@veryayskiy veryayskiy left a comment

Choose a reason for hiding this comment

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

Tested locally, a bit scared to deploy on Friday 🗄️

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.

2 participants