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): name split stylesheets so the CDN caches them as immutable (#106504)

Paul D'Ambra608d71520a43
diff --git a/frontend/bin/stableAssetNames.test.ts b/frontend/bin/stableAssetNames.test.ts
new file mode 100644
index 000000000000..71e33ba130f1
--- /dev/null
+++ b/frontend/bin/stableAssetNames.test.ts
@@ -0,0 +1,25 @@
+import { stableFileName } from './stableChunkNames.mjs'
+import { cssGroupFileStem } from './stableCssPlan.mjs'
+
+// Mirrors the immutable-cache rule in PostHog/charts: charts/posthog-app/templates/posthog-django/syncHooks-upload-assets.yaml
+const CDN_IMMUTABLE_NAME = /^[A-Za-z0-9]+-[A-Z0-9]{6,}\.(js|css)$/
+
+describe('stable build file names match the charts immutable upload rule', () => {
+ it.each(['eager-tailwind', 'eager-global', 'eager-app', 'lazy-0A1B2C3D4E'])('css group %s', (group) => {
+ expect(`${cssGroupFileStem(group)}-HHD5DH7J.css`).toMatch(CDN_IMMUTABLE_NAME)
+ })
+
+ it.each([
+ 'chunk-S5F888C59.js',
+ 'index-DE3U56RC.js',
+ 'App-AAAA1111.js',
+ 'DecompressionWorkerManager-BBBB2222.js',
+ 'sharedChunkAnchors-CCCC3333.js',
+ 'github-dark-DDDD4444.js',
+ 'editor.main-EEEE5555.js',
+ 'hogql_parser_wasm_browser-FFFF6666.js',
+ 'architecture-7EHR7CIX-GGGG7777.js',
+ ])('js output %s', (file) => {
+ expect(stableFileName(file, 'source')).toMatch(CDN_IMMUTABLE_NAME)
+ })
+})
diff --git a/frontend/bin/stableChunkNames.mjs b/frontend/bin/stableChunkNames.mjs
index 2c55844b47c5..c6641dbc3e11 100644
--- a/frontend/bin/stableChunkNames.mjs
+++ b/frontend/bin/stableChunkNames.mjs
@@ -62,6 +62,10 @@ export function rewriteChunkSource(source, identityByFile) {
})
}
+export function alphanumericStem(name) {
+ return name.replace(/[^A-Za-z0-9]+(.?)/g, (_, next) => next.toUpperCase())
+}
+
export function stableFileName(originalFile, rewrittenSource) {
const prefix = originalFile.replace(/-[^-./]+\.js$/, '')
// Only holds because chunkNames/entryNames in common/esbuilder/utils.mjs are `[name]-[hash]` for non-dev builds.
@@ -69,7 +73,7 @@ export function stableFileName(originalFile, rewrittenSource) {
if (prefix === originalFile) {
throw new Error(`stable chunks: ${originalFile} does not end in the expected -<hash>.js suffix`)
}
- return `${prefix}-${STABLE_NAME_MARKER}${shortHash(rewrittenSource)}.js`
+ return `${alphanumericStem(prefix)}-${STABLE_NAME_MARKER}${shortHash(rewrittenSource)}.js`
}
/**
diff --git a/frontend/bin/stableCss.mjs b/frontend/bin/stableCss.mjs
index b681182c4aed..61b0fa959cef 100644
--- a/frontend/bin/stableCss.mjs
+++ b/frontend/bin/stableCss.mjs
@@ -2,6 +2,8 @@ import path from 'node:path'
import { commonConfig, esbuildBuild } from '@posthog/esbuilder'
+import { cssGroupFileStem } from './stableCssPlan.mjs'
+
/**
* Builds every group with the app's own esbuild plugins (Sass, Tailwind, PostCSS), so each
* stylesheet holds exactly the CSS the entry stylesheet holds for those files. Returns
@@ -11,7 +13,9 @@ export async function buildCssGroups(absWorkingDir, groups) {
const result = await esbuildBuild({
...commonConfig,
absWorkingDir,
- entryPoints: Object.fromEntries([...groups.keys()].map((name) => [`styles-${name}`, `css-group:${name}`])),
+ entryPoints: Object.fromEntries(
+ [...groups.keys()].map((name) => [cssGroupFileStem(name), `css-group:${name}`])
+ ),
outdir: path.resolve(absWorkingDir, 'dist'),
entryNames: '[name]-[hash]',
bundle: true,
diff --git a/frontend/bin/stableCssPlan.mjs b/frontend/bin/stableCssPlan.mjs
index cf27a4ab3dab..e008266067fe 100644
--- a/frontend/bin/stableCssPlan.mjs
+++ b/frontend/bin/stableCssPlan.mjs
@@ -1,7 +1,7 @@
import { CSS_LOAD_GLOBAL } from '@posthog/esbuilder/cssLoader.mjs'
import { BOOT_ENTRIES, ENTRY } from './bootEntries.mjs'
-import { chunkIdentity, shortHash } from './stableChunkNames.mjs'
+import { alphanumericStem, chunkIdentity, shortHash } from './stableChunkNames.mjs'
/**
* Split stylesheets for the stable build.
@@ -21,6 +21,10 @@ import { chunkIdentity, shortHash } from './stableChunkNames.mjs'
export const CSS_SPECIFIER_PREFIX = '@css/'
+export function cssGroupFileStem(groupName) {
+ return alphanumericStem(`styles-${groupName}`)
+}
+
const isStylesheet = (file) => /\.(css|scss|sass)$/.test(file)
function eagerLayer(file) {
diff --git a/posthog/test/test_stable_chunks.py b/posthog/test/test_stable_chunks.py
index 201127675765..b051b3c54fa1 100644
--- a/posthog/test/test_stable_chunks.py
+++ b/posthog/test/test_stable_chunks.py
@@ -24,7 +24,7 @@ from posthog.utils import get_context_for_template, render_template
VALID_MANIFEST = {
"imports": {"@c/eAAAA": "static/index-S0000000000.js"},
"preload": {"js": ["static/index-S0000000000.js"], "authenticatedJs": ["static/chunk-S1111111111.js"]},
- "eagerCss": ["static/styles-eager-tailwind-AAAA1111.css", "static/styles-eager-app-BBBB2222.css"],
+ "eagerCss": ["static/stylesEagerTailwind-AAAA1111.css", "static/stylesEagerApp-BBBB2222.css"],
}
@@ -52,8 +52,8 @@ class TestStableChunks(SimpleTestCase):
}
assert stable.preload_urls(include_authenticated_shell=False) == ("static/index-S0000000000.js",)
assert stable.eager_css_urls == (
- "static/styles-eager-tailwind-AAAA1111.css",
- "static/styles-eager-app-BBBB2222.css",
+ "static/stylesEagerTailwind-AAAA1111.css",
+ "static/stylesEagerApp-BBBB2222.css",
)
def test_import_map_cannot_close_its_script_tag(self):
@@ -117,7 +117,7 @@ class TestStableChunks(SimpleTestCase):
imports={"@c/eAAAA": "static/index-S0000000000.js"},
preload_js_urls=(),
authenticated_preload_js_urls=(),
- eager_css_urls=("static/styles-eager-tailwind-AAAA1111.css", "static/styles-eager-app-BBBB2222.css"),
+ eager_css_urls=("static/stylesEagerTailwind-AAAA1111.css", "static/stylesEagerApp-BBBB2222.css"),
)
context = self._context_with_stable_chunks(stable)