Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/deploy-preview.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ jobs:
- name: Check Twig guide integrations
run: pnpm test:twig-integration

- name: Check analytics bootstrap
run: pnpm test:posthog-loader
env:
PUPPETEER_EXECUTABLE_PATH: /usr/bin/google-chrome

- name: Check shared window paths
run: pnpm test:internal-path

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ static/fonts/MatterSQItalicVF.woff2
static/fonts/MatterSQVF.woff
static/fonts/MatterSQVF.woff2
static/scripts/posthog-init.js
static/scripts/posthog-bootstrap.js
.vercel
*Type.ts
CLAUDE.local.md
Expand Down
43 changes: 36 additions & 7 deletions gatsby/onPreBootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import path from 'path'
import fs from 'fs'
import { execFile } from 'child_process'
import { promisify } from 'util'
import { setTimeout as delay } from 'timers/promises'

import { fetchAndProcessMCPTools, writeMCPToolsToFile } from './utils/fetchMCPTools'
import { fetchScoutSkills, writeScoutSkillsToFile } from './utils/fetchScoutSkills'
Expand All @@ -16,6 +17,9 @@ export const MCP_TOOLS_CACHE_KEY = 'onPreBootstrap@@mcp-tools'
const GATSBY_SOURCE_GIT_CACHE_DIR = path.resolve(__dirname, '../.cache/gatsby-source-git')
const BRANCH_MANIFEST_FILE = path.join(GATSBY_SOURCE_GIT_CACHE_DIR, '.branch-manifest.json')

// Download the region-independent release artifact. Runtime regions and proxies come from data-asset-host.
const POSTHOG_BOOTSTRAP_URL = 'https://us-assets.i.posthog.com/static/1/loader-bootstrap.js'

/**
* Invalidates the gatsby-source-git cache if the configured branch has changed.
* This prevents stale docs from being served when switching GATSBY_POSTHOG_BRANCH.
Expand Down Expand Up @@ -79,6 +83,35 @@ async function sparseCloneGitSource(store): Promise<void> {
}
}

async function downloadPostHogBootstrap(): Promise<string> {
for (let attempt = 0; ; attempt++) {
let retryable = true
try {
// A Request bypasses the URL-only development cache for this mutable asset.
const response = await fetch(
new Request(POSTHOG_BOOTSTRAP_URL, {
cache: 'no-store',
signal: AbortSignal.timeout(30000),
})
)
if (!response.ok) {
retryable = response.status === 408 || response.status === 429 || response.status >= 500
await response.body?.cancel()
throw new Error(`Unable to download PostHog bootstrap: HTTP ${response.status}`)
}
const bootstrap = (await response.text()).replace(/\n\/\/# sourceMappingURL=.*\s*$/, '\n')
retryable = false
if (!bootstrap.trim()) {
throw new Error('Downloaded PostHog bootstrap is empty')
}
return bootstrap
} catch (error) {
if (!retryable || attempt === 2) throw error
await delay(500 * 2 ** attempt)
}
}
}

export const onPreBootstrap: GatsbyNode['onPreBootstrap'] = async ({ cache, store }) => {
cacheFetchResponsesInDevelopment()
// Invalidate gatsby-source-git cache if branch has changed
Expand All @@ -87,17 +120,12 @@ export const onPreBootstrap: GatsbyNode['onPreBootstrap'] = async ({ cache, stor
// Enrich video data with thumbnails and titles from APIs
await enrichVideos()
if (process.env.GATSBY_POSTHOG_API_KEY && process.env.GATSBY_POSTHOG_API_HOST) {
const bootstrap = await downloadPostHogBootstrap()
const assetHost = process.env.GATSBY_POSTHOG_ASSET_HOST
// The "1" pins the array.js major version; strict_script_versioning makes the
// SDK load its lazy chunks from matching versioned paths on the asset host.
const arrayRoute = assetHost
? `${assetHost}/static/1/array.js`
: `${process.env.GATSBY_POSTHOG_API_HOST}/static/array.js`
const assetHostConfig = assetHost
? `asset_host: "${assetHost}",\n strict_script_versioning: true,\n `
: ''
const posthogScript = `!function(t,e){var o,n,p,r;e.__SV||(window.posthog=e,e._i=[],e.init=function(i,s,a){function g(t,e){var o=e.split(".");2==o.length&&(t=t[o[0]],e=o[1]),t[e]=function(){t.push([e].concat(Array.prototype.slice.call(arguments,0)))}}(p=t.createElement("script")).type="text/javascript",p.async=!0,p.src="${arrayRoute}",(r=t.getElementsByTagName("script")[0]).parentNode.insertBefore(p,r);var u=e;for(void 0!==a?u=e[a]=[]:a="posthog",u.people=u.people||[],Object.defineProperty(u,"toString",{configurable:!0,enumerable:!0,writable:!0,value:function(t){var e="posthog";return"posthog"!==a&&(e+="."+a),t||(e+=" (stub)"),e}}),Object.defineProperty(u.people,"toString",{configurable:!0,enumerable:!0,writable:!0,value:function(){return u.toString(1)+".people (stub)"}}),o="capture identify alias people.set people.set_once set_config register register_once unregister opt_out_capturing has_opted_out_capturing opt_in_capturing reset isFeatureEnabled onFeatureFlags".split(" "),n=0;n<o.length;n++)g(u,o[n]);e._i.push([i,s,a])},e.__SV=1)}(document,window.posthog||[]);
posthog.init("${process.env.GATSBY_POSTHOG_API_KEY}", {
const posthogScript = `posthog.init("${process.env.GATSBY_POSTHOG_API_KEY}", {
api_host: "${process.env.GATSBY_POSTHOG_API_HOST}",
ui_host: "${process.env.GATSBY_POSTHOG_UI_HOST}",
defaults: 'latest',
Expand Down Expand Up @@ -148,6 +176,7 @@ posthog.init("${process.env.GATSBY_POSTHOG_API_KEY}", {
},
})`
const scriptsDir = path.resolve(__dirname, '../static/scripts')
fs.writeFileSync(path.join(scriptsDir, 'posthog-bootstrap.js'), bootstrap)
fs.writeFileSync(path.join(scriptsDir, 'posthog-init.js'), posthogScript)
}

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"test:tier-list": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test src/components/TierList/catalog.test.ts src/components/TierList/state.test.ts",
"test:middleware": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test middleware.test.ts",
"test:gatsby-functions": "node --test scripts/gatsby-functions.test.cjs",
"test:posthog-loader": "node --test scripts/posthog-loader.test.cjs scripts/early-access-features.test.cjs",
"test:gatsby-utils": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test gatsby/utils.test.ts",
"test:internal-path": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test src/lib/internalPath.test.ts",
"test:navs": "node --disable-warning=MODULE_TYPELESS_PACKAGE_JSON --test src/navs/activeMenu.test.ts src/navs/applicationMetrics.test.ts src/navs/applicationMetricsPresence.test.ts src/components/ReaderView/tabNavigation.test.ts src/components/PocketGuides/learnRoutes.test.ts src/components/PocketGuides/learnTracking.test.ts src/components/Docs/AppsList/index.test.ts",
Expand Down
136 changes: 136 additions & 0 deletions scripts/early-access-features.test.cjs
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) => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🤖 An AI agent wrote this on behalf of @ioannisj.

[suggestion] Small one: both new test files carry the same load helper (read the file, ts.transpileModule to CommonJS, runInNewContext with a stubbed require), and they already drift a bit, this one asserts on unexpected deps while the loader test falls through to the real require. Worth a shared scripts/test-utils/load-ts-module.cjs with the strict-deps check as an option, so the next hook test doesn't add a third copy? Or better as a follow-up?

Copy link
Copy Markdown
Member Author

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

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(), '')
})
}
Loading
Loading