Repository navigation
build: download the PostHog bootstrap on each build #21670
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
60e0a25
chore: use the hosted PostHog script loader
marandaneto b5b06d4
fix: preserve page rendering and roadmap loading with hosted loader
marandaneto 5f3c425
Merge remote-tracking branch 'origin/master' into chore/use-posthog-s…
marandaneto 2f33610
fix: keep Gatsby startup independent of analytics downloads
marandaneto b9f9574
chore: remove redundant bootstrap documentation and license copy
marandaneto 8a87f0b
Merge remote-tracking branch 'origin/master' into chore/use-posthog-s…
marandaneto 089cef9
build: download the latest PostHog bootstrap on each build
marandaneto 7541920
Merge remote-tracking branch 'origin/master' into chore/use-posthog-s…
marandaneto 3e80ff7
build: retry transient PostHog bootstrap download failures
marandaneto d5c7b4f
chore: clarify bootstrap source and remove unused test stub
marandaneto a2557c2
Merge remote-tracking branch 'origin/master' into chore/use-posthog-s…
marandaneto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| const assert = require('node:assert/strict') | ||
| const { readFileSync } = require('node:fs') | ||
| const path = require('node:path') | ||
| const { test } = require('node:test') | ||
| const { runInNewContext } = require('node:vm') | ||
| const React = require('react') | ||
| const { act } = React | ||
| const { createRoot } = require('react-dom/client') | ||
| const { JSDOM } = require('jsdom') | ||
| const ts = require('typescript') | ||
|
|
||
| const feature = { name: 'Live feature', flagKey: 'live-feature', stage: 'beta', payload: {} } | ||
| const survey = { | ||
| id: 'waitlist-survey', | ||
| type: 'api', | ||
| linked_flag_key: 'live-feature', | ||
| start_date: '2026-01-01', | ||
| questions: [{ id: 'waitlist-question' }], | ||
| } | ||
|
|
||
| function queuedSdk() { | ||
| const queue = [] | ||
| for (const method of ['getEarlyAccessFeatures', 'getSurveys']) { | ||
| queue[method] = (...args) => queue.push([method, ...args]) | ||
| } | ||
| return queue | ||
| } | ||
|
|
||
| function readySdk() { | ||
| return { | ||
| __loaded: true, | ||
| getEarlyAccessFeatures: (callback) => queueMicrotask(() => callback([feature])), | ||
| getSurveys: (callback) => queueMicrotask(() => callback([survey])), | ||
| } | ||
| } | ||
|
|
||
| function mount(t, sdk) { | ||
| t.mock.timers.enable({ apis: ['setTimeout', 'setInterval'] }) | ||
| const dom = new JSDOM('<div id="root"></div>', { url: 'https://website.example' }) | ||
| dom.window.posthog = sdk | ||
| const descriptors = new Map( | ||
| ['window', 'document', 'IS_REACT_ACT_ENVIRONMENT'].map((key) => [ | ||
| key, | ||
| Object.getOwnPropertyDescriptor(globalThis, key), | ||
| ]) | ||
| ) | ||
| Object.assign(globalThis, { window: dom.window, document: dom.window.document, IS_REACT_ACT_ENVIRONMENT: true }) | ||
| const root = createRoot(dom.window.document.getElementById('root')) | ||
| t.after(() => { | ||
| act(() => root.unmount()) | ||
| dom.window.close() | ||
| t.mock.timers.reset() | ||
| for (const [key, descriptor] of descriptors) { | ||
| if (descriptor) Object.defineProperty(globalThis, key, descriptor) | ||
| else Reflect.deleteProperty(globalThis, key) | ||
| } | ||
| }) | ||
| const load = (file, dependencies) => { | ||
| const { outputText } = ts.transpileModule(readFileSync(path.resolve(__dirname, '..', file), 'utf8'), { | ||
| compilerOptions: { module: ts.ModuleKind.CommonJS, esModuleInterop: true }, | ||
| }) | ||
| const exports = {} | ||
| runInNewContext(outputText, { | ||
| exports, | ||
| window: dom.window, | ||
| setTimeout, | ||
| clearTimeout, | ||
| setInterval, | ||
| clearInterval, | ||
| require: (name) => { | ||
| assert.ok(name in dependencies, `Unexpected dependency: ${name}`) | ||
| return dependencies[name] | ||
| }, | ||
| }) | ||
| return exports | ||
| } | ||
| const { useEarlyAccessFeatures } = load('src/hooks/useEarlyAccessFeatures.ts', { | ||
| react: React, | ||
| gatsby: { graphql: () => '', useStaticQuery: () => ({ allEarlyAccessFeature: { nodes: [] } }) }, | ||
| }) | ||
| let state | ||
| function Consumer() { | ||
| state = useEarlyAccessFeatures() | ||
| return React.createElement( | ||
| 'output', | ||
| null, | ||
| state.loading ? 'loading' : state.features.map((f) => f.name).join(', ') | ||
| ) | ||
| } | ||
| return { | ||
| window: dom.window, | ||
| render: () => root.render(React.createElement(Consumer)), | ||
| state: () => state, | ||
| text: () => dom.window.document.querySelector('output').textContent, | ||
| } | ||
| } | ||
|
|
||
| test('a ready SDK renders live features with the linked waitlist survey', async (t) => { | ||
| const page = mount(t, readySdk()) | ||
| await act(async () => page.render()) | ||
| assert.equal(page.text(), 'Live feature') | ||
| assert.equal(page.state().loading, false) | ||
| assert.equal(page.state().features[0].payload.survey_id, survey.id) | ||
| }) | ||
|
|
||
| test('a roadmap mounted before SDK download renders live features after the SDK replaces its queue', async (t) => { | ||
| const queue = queuedSdk() | ||
| const page = mount(t, queue) | ||
| await act(async () => page.render()) | ||
| assert.equal(page.text(), 'loading') | ||
| const sdk = readySdk() | ||
| await act(async () => { | ||
| page.window.posthog = sdk | ||
| const pending = queue.splice(0) | ||
| for (const [method, ...args] of pending) sdk[method](...args) | ||
| t.mock.timers.tick(300) | ||
| }) | ||
| assert.equal(page.text(), 'Live feature', `Calls left on the discarded queue: ${queue.map(([method]) => method)}`) | ||
| assert.equal(page.state().loading, false) | ||
| assert.equal(page.state().features[0].payload.survey_id, survey.id) | ||
| }) | ||
|
|
||
| for (const [name, sdk] of [ | ||
| ['legacy snippet', []], | ||
| ['hosted loader', queuedSdk()], | ||
| ]) { | ||
| test(`failed SDK download stops loading after six seconds (${name})`, async (t) => { | ||
| const page = mount(t, sdk) | ||
| await act(async () => page.render()) | ||
| act(() => t.mock.timers.tick(5999)) | ||
| assert.equal(page.state().loading, true) | ||
| act(() => t.mock.timers.tick(1)) | ||
| assert.equal(page.state().loading, false) | ||
| assert.equal(page.text(), '') | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] Small one: both new test files carry the same
loadhelper (read the file,ts.transpileModuleto CommonJS,runInNewContextwith a stubbedrequire), and they already drift a bit, this one asserts on unexpected deps while the loader test falls through to the realrequire. Worth a sharedscripts/test-utils/load-ts-module.cjswith the strict-deps check as an option, so the next hook test doesn't add a third copy? Or better as a follow-up?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can be a follow up