Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

fix(frontend): scope the logged-out import map in the docker build (#124314)

Paul D'Ambra8b1b093a4f4a
diff --git a/frontend/bin/loggedOutScenes.mjs b/frontend/bin/loggedOutScenes.mjs
index e9c27c06f9c5..6637b6527b21 100644
--- a/frontend/bin/loggedOutScenes.mjs
+++ b/frontend/bin/loggedOutScenes.mjs
@@ -31,7 +31,23 @@ export const LOGGED_OUT_SCENES = [
// The files that map a scene to its loader, as `[Scene.Name]: () => import('…')` or `Name: () => import('…')`.
const SCENE_REGISTRIES = ['src/scenes/appScenes.ts', 'src/productScenes.tsx']
-const SCENE_LOADER = /(?:\[Scene\.(\w+)\]|(\w+)):\s*\(\)\s*=>\s*import\(\s*'([^']+)'\s*\)/g
+const SCENE_LOADER = /(?:\[Scene\.(\w+)\]|(["']?)(\w+)\2):\s*\(\)\s*=>\s*import\(\s*(["'])([^"']+)\4\s*\)/g
+
+/**
+ * `[scene, import specifier]` for each loader in a registry's source. A loader this cannot read
+ * throws, because a scene missed here lands in the logged-out import map.
+ */
+export function sceneLoaders(source, registry) {
+ const loaders = [...source.matchAll(SCENE_LOADER)].map(([, sceneEnumName, , sceneKey, , specifier]) => [
+ sceneEnumName ?? sceneKey,
+ specifier,
+ ])
+ const importCount = source.match(/import\(/g)?.length ?? 0
+ if (loaders.length !== importCount) {
+ throw new Error(`stable chunks: read ${loaders.length} of ${importCount} scene loaders in ${registry}`)
+ }
+ return loaders
+}
/** A module path relative to the frontend directory, without its extension or a trailing `/index`. */
export function moduleKey(modulePath) {
@@ -60,9 +76,9 @@ export function sceneModules(frontendDir) {
const modules = new Map()
for (const registry of SCENE_REGISTRIES) {
const source = fs.readFileSync(path.resolve(frontendDir, registry), 'utf8')
- for (const [, sceneEnumName, sceneKey, specifier] of source.matchAll(SCENE_LOADER)) {
+ for (const [scene, specifier] of sceneLoaders(source, registry)) {
const modulePath = path.relative(frontendDir, path.resolve(frontendDir, resolveLoader(registry, specifier)))
- modules.set(sceneEnumName ?? sceneKey, moduleKey(modulePath.split(path.sep).join('/')))
+ modules.set(scene, moduleKey(modulePath.split(path.sep).join('/')))
}
}
return modules
diff --git a/frontend/bin/loggedOutScenes.test.ts b/frontend/bin/loggedOutScenes.test.ts
index c63a5e96a583..9e5160d065e2 100644
--- a/frontend/bin/loggedOutScenes.test.ts
+++ b/frontend/bin/loggedOutScenes.test.ts
@@ -2,7 +2,7 @@ import path from 'node:path'
import { preloadedScenes, sceneConfigurations } from 'scenes/scenes'
-import { LOGGED_OUT_SCENES, sceneModules } from './loggedOutScenes.mjs'
+import { LOGGED_OUT_SCENES, sceneLoaders, sceneModules } from './loggedOutScenes.mjs'
describe('LOGGED_OUT_SCENES', () => {
// A logged-out scene missing from the list has no import map entries for its chunks on the stable
@@ -17,4 +17,22 @@ describe('LOGGED_OUT_SCENES', () => {
expect([...LOGGED_OUT_SCENES].sort()).toEqual(loggedOutScenes.sort())
expect(LOGGED_OUT_SCENES.filter((scene) => !scenesWithLoaders.has(scene))).toEqual([])
})
+
+ // The build regenerates productScenes.tsx, and the formatter's quote style depends on which config it finds.
+ it.each([
+ ['single quotes', `{ Heatmaps: () => import('./HeatmapsScene'), [Scene.Login]: () => import('./Login') }`],
+ ['double quotes', `{ Heatmaps: () => import("./HeatmapsScene"), [Scene.Login]: () => import("./Login") }`],
+ ['quoted keys', `{ "Heatmaps": () => import("./HeatmapsScene"), [Scene.Login]: () =>\n import("./Login") }`],
+ ])('reads scene loaders written with %s', (_, source) => {
+ expect(sceneLoaders(source, 'registry.ts')).toEqual([
+ ['Heatmaps', './HeatmapsScene'],
+ ['Login', './Login'],
+ ])
+ })
+
+ it('fails on a loader it cannot read', () => {
+ expect(() => sceneLoaders('{ Heatmaps: () => import(`./HeatmapsScene`) }', 'registry.ts')).toThrow(
+ 'registry.ts'
+ )
+ })
})