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: don't load preact until you have to #1311

Merged
merged 3 commits into from
Jul 22, 2024
Merged

Conversation

pauldambra
Copy link
Member

@pauldambra pauldambra commented Jul 20, 2024

we need to keep the bundle as small as possible, it's already ~3x bigger than when people first start complaining about the size

#1281 is in merge hell because we abandoned it

this is another attempt to avoid everyone downloading preact, it reduces bundle size by 6%

Copy link

vercel bot commented Jul 20, 2024

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

Name Status Preview Updated (UTC)
posthog-js ✅ Ready (Inspect) Visit Preview Jul 20, 2024 4:31pm

Copy link

github-actions bot commented Jul 20, 2024

Size Change: -29.9 kB (-2.52%)

Total Size: 1.16 MB

Filename Size Change
dist/array.full.js 330 kB +378 B (+0.11%)
dist/array.js 152 kB -10.1 kB (-6.25%)
dist/main.js 153 kB -10.1 kB (-6.22%)
dist/module.js 152 kB -10.1 kB (-6.25%)
dist/surveys.js 64.8 kB +107 B (+0.17%)
ℹ️ View Unchanged
Filename Size
dist/exception-autocapture.js 10.4 kB
dist/recorder-v2.js 110 kB
dist/recorder.js 110 kB
dist/surveys-preview.js 59.6 kB
dist/tracing-headers.js 8.26 kB
dist/web-vitals.js 5.79 kB

compressed-size-action

@@ -147,7 +162,12 @@ describe('surveys', () => {
},
} as unknown as PostHog
Copy link
Member Author

Choose a reason for hiding this comment

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

to keep this PR smaller, i'm not replacing the mock posthog here
(we really should be, but it can wait)

@@ -757,33 +779,6 @@ describe('surveys', () => {
})
})

describe('decide response', () => {
Copy link
Member Author

Choose a reason for hiding this comment

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

since we're mocking load script we don't need to test whether or not it behaves correctly...

Comment on lines +7 to +8
;(window as any).__PosthogExtensions__ = (window as any).__PosthogExtensions__ || {}
;(window as any).__PosthogExtensions__.canActivateRepeatedly = canActivateRepeatedly
Copy link
Member Author

Choose a reason for hiding this comment

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

maybe this should even be PosthogExtensions.Surveys but 🤷

but regardless this is the mechanism we use to push code out of the main bundle so that folk who don't use surveys (or replay or web vitals etc etc) don't have to download the code for that feature

accessing canActivateRepeatedly causes the bundler to pull in preact and you only need that with surveys enabled

import { canActivateRepeatedly } from './extensions/surveys/surveys-utils'
import { isNullish } from './utils/type-utils'

const LOGGER_PREFIX = '[Surveys]'
Copy link
Member Author

Choose a reason for hiding this comment

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

QOL we put a prefix on logs so that they are easier to read

private _decideServerResponse?: boolean
public _surveyEventReceiver: SurveyEventReceiver | null

constructor(instance: PostHog) {
this.instance = instance
constructor(private readonly instance: PostHog) {
Copy link
Member Author

Choose a reason for hiding this comment

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

QOL: TS lets you declare a field like this when it's a constructor parameter

@@ -178,7 +178,7 @@ export class PostHogSurveys {
const eventBasedTargetingFlagCheck =
hasEvents || hasActions ? activatedSurveys?.includes(survey.id) : true

const overrideInternalTargetingFlagCheck = canActivateRepeatedly(survey)
const overrideInternalTargetingFlagCheck = this._canActivateRepeatedly(survey)
Copy link
Member Author

Choose a reason for hiding this comment

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

this single line effectively reduces bundle size by 6%

@@ -258,7 +258,15 @@ export class PostHogSurveys {
return nextQuestionIndex
}

console.warn('Falling back to next question index due to unexpected branching type') // eslint-disable-line no-console
Copy link
Member Author

Choose a reason for hiding this comment

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

try not to disable eslint rules, here it was warning you not to use console and it was IMHO right 😊

Comment on lines +265 to +271
// this method is lazily loaded onto the window to avoid loading preact and other dependencies if surveys is not enabled
private _canActivateRepeatedly(survey: Survey) {
if (isNullish(assignableWindow.__PosthogExtensions__.canActivateRepeatedly)) {
logger.warn(LOGGER_PREFIX, 'canActivateRepeatedly is not defined, must init before calling')
}
return assignableWindow.__PosthogExtensions__.canActivateRepeatedly(survey)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

convenience method for accessing the lazily loaded canActivateRepeatedly

@pauldambra pauldambra marked this pull request as ready for review July 20, 2024 16:32
@pauldambra pauldambra requested a review from a team July 20, 2024 16:32
Copy link
Contributor

@dmarticus dmarticus left a comment

Choose a reason for hiding this comment

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

This change seems reasonable to me, thanks for keeping at it and making the PR! @pauldambra, Is there anything else you needed from the Feature Success team other than a second pair of eyes on the review? I kicked CI again and it passed this time so all seems good from my perspective.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bump patch Bump patch version when this PR gets merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants