Skip to content
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

fix: survey preview better bundle size #1665

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

lucasheriques
Copy link
Contributor

@lucasheriques lucasheriques commented Jan 21, 2025

Changes

no functional changes

changes some imports to optimize for bundle size

Checklist

  • Tests for new code (see advice on the tests we use)
  • Accounted for the impact of any changes across different browsers
  • Accounted for backwards compatibility of any changes (no breaking changes in posthog-js!)

Copy link

vercel bot commented Jan 21, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Jan 22, 2025 0:14am

Copy link

github-actions bot commented Jan 21, 2025

Size Change: -10.3 kB (-0.32%)

Total Size: 3.25 MB

Filename Size Change
dist/all-external-dependencies.js 209 kB +1.65 kB (+0.79%)
dist/array.full.es5.js 265 kB +232 B (+0.09%)
dist/array.full.js 369 kB +212 B (+0.06%)
dist/array.full.no-external.js 368 kB +212 B (+0.06%)
dist/array.js 179 kB -1.81 kB (-1%)
dist/array.no-external.js 178 kB -1.81 kB (-1%)
dist/main.js 180 kB -1.78 kB (-0.98%)
dist/module.full.js 369 kB +212 B (+0.06%)
dist/module.full.no-external.js 368 kB +212 B (+0.06%)
dist/module.js 180 kB -1.44 kB (-0.79%)
dist/module.no-external.js 178 kB -1.44 kB (-0.8%)
dist/surveys-preview.js 60.7 kB -6.39 kB (-9.53%) 👏
dist/surveys.js 65.4 kB +1.64 kB (+2.57%)
ℹ️ View Unchanged
Filename Size
dist/customizations.full.js 13.8 kB
dist/dead-clicks-autocapture.js 14.4 kB
dist/exception-autocapture.js 9.48 kB
dist/external-scripts-loader.js 2.64 kB
dist/recorder-v2.js 116 kB
dist/recorder.js 116 kB
dist/tracing-headers.js 1.76 kB
dist/web-vitals.js 10.4 kB

compressed-size-action

@lucasheriques
Copy link
Contributor Author

hey @pauldambra appreciate if you could take a look!!

from the previous PR, #1661, which increased size by 9.2kb, this one decreased it by 10.3kb.

I tested it locally both on the nextjs playground, and also on the posthog/posthog ponting to the file, looks like everything worked. can you double check to see if I did anything weird?

@pauldambra
Copy link
Member

very quick and tired brain scan but that looks great to me!
and 1% down on the main bundle too 💯

@lucasheriques lucasheriques marked this pull request as ready for review January 22, 2025 20:12
@lucasheriques lucasheriques requested a review from a team January 22, 2025 20:12
@lucasheriques lucasheriques marked this pull request as draft January 22, 2025 21:28
Comment on lines +197 to +200
if (isNullish(assignableWindow.__PosthogExtensions__?.getNextSurveyStep)) {
logger.warn('init was not called')
return 0
}
Copy link
Member

Choose a reason for hiding this comment

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

Lets do better logging here similar to #1663
so we know if the problem is the PosthogExtensions not loaded, or the getNextSurveyStep somehow wasn't available

@@ -844,6 +843,9 @@ describe('surveys', () => {
})

describe('branching logic', () => {
beforeEach(() => {
surveys.getNextSurveyStep = getNextSurveyStep
Copy link
Member

Choose a reason for hiding this comment

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

i tend to mock the external dependency loader so that more of the flow is being exercised...

so e.g. in session replay

        loadScriptMock.mockImplementation((_ph, _path, callback) => {
            addRRwebToWindow()
            callback()
        })

        assignableWindow.__PosthogExtensions__.loadExternalDependency = loadScriptMock
        ```
        
        otherwise that startup can be broken and tests carry on passing
        
        

Copy link
Member

Choose a reason for hiding this comment

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

ofc explicit tests somewhere else for it is totally fine too

@@ -78,6 +78,7 @@ interface PostHogExtensions {
rrwebPlugins?: { getRecordConsolePlugin: any; getRecordNetworkPlugin?: any }
canActivateRepeatedly?: (survey: any) => boolean
generateSurveys?: (posthog: PostHog) => any | undefined
getNextSurveyStep?: (survey: any, currentQuestionIndex: number, response: string | string[] | number | null) => any
Copy link
Member

Choose a reason for hiding this comment

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

we can't move generateSurveys because older clients are already reading from that location...

but it might be nice to put a surveys on here so you'd have

// deprecated: still in place for older clients but you should use `surveys.generateSurveys`
generateSurveys?: (posthog: PostHog) => any | undefined
surveys: {
	generateSurveys?: (posthog: PostHog) => any | undefined,
    getNextSurveyStep?: (survey: any, currentQuestionIndex: number, response: string | string[] | number | null) => 
any
}

so that over time it stays obvious what surveys has put on the window

not a hard requirement but i think tidier in the long run (and i wish we'd started doing this earlier)

Copy link
Member

@pauldambra pauldambra left a comment

Choose a reason for hiding this comment

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

i don't know how to test surveys but this looks correct
and you can see how this took the main bundle down by 1%
there's for sure more that could be moved out of the main bundle if you enjoyed the adventure

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