: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
fix(toolbar): resolve stylesheet from the loader origin, not the baked CDN host (#86896)
posthog[bot]a57a5962e2d2
diff --git a/frontend/src/toolbar/ToolbarApp.tsx b/frontend/src/toolbar/ToolbarApp.tsxindex c9b20a20d994..784ab8df734f 100644--- a/frontend/src/toolbar/ToolbarApp.tsx+++ b/frontend/src/toolbar/ToolbarApp.tsx@@ -44,21 +44,27 @@ export function ToolbarApp(props: ToolbarProps = {}): JSX.Element {// references (fonts) are relative, so they only resolve alongside the// toolbar/assets/ directory it was built with.//- // When __POSTHOG_TOOLBAR_PUBLIC_PATH__ is baked in at build- // time (posthog-js versioned bundle), load the CSS from the- // same versioned URL as the JS bundle. The version is the- // cache key, so no cache-busting query param is needed.- //- // Otherwise (i.e. posthog/posthog's own deploys), fall back to- // serving it from the API host alongside toolbar.js, with a- // 5-minute cache-buster on the unversioned URL.+ // The loader shares the origin it was served from as+ // __POSTHOG_TOOLBAR_SCRIPT_SRC__. Resolve the CSS from that origin so it+ // follows the same reverse proxy as the JS. The loader prefers this over the+ // baked absolute CDN host for the same reason (see resolveAppUrl in loader.ts),+ // and this is why a style-src 'self' CSP or an ad blocker no longer strips the+ // stylesheet on proxied customer pages.+ const scriptSrc = (window as any).__POSTHOG_TOOLBAR_SCRIPT_SRC__ as string | undefined+if (__POSTHOG_TOOLBAR_PUBLIC_PATH__) {- styleLink.href = `${__POSTHOG_TOOLBAR_PUBLIC_PATH__}toolbar/toolbar-app.css`+ // posthog-js versioned bundle: the version in the path is the cache key, so+ // no cache-busting query param is needed.+ const base = scriptSrc || __POSTHOG_TOOLBAR_PUBLIC_PATH__+ styleLink.href = new URL('toolbar/toolbar-app.css', base).href} else {+ // posthog/posthog's own deploy: the URL is unversioned, so add a 5-minute+ // cache-buster.const fiveMinutesInMillis = 5 * 60 * 1000const timestampToNearestFiveMinutes =Math.floor(Date.now() / fiveMinutesInMillis) * fiveMinutesInMillis- styleLink.href = `${apiHost}/static/toolbar/toolbar-app.css?t=${timestampToNearestFiveMinutes}`+ const base = scriptSrc || `${apiHost}/static/toolbar.js`+ styleLink.href = new URL(`toolbar/toolbar-app.css?t=${timestampToNearestFiveMinutes}`, base).href}styleLink.onload = () => setDidLoadStyles(true)diff --git a/frontend/src/toolbar/loader.ts b/frontend/src/toolbar/loader.tsindex f174dfff183c..fc3dd8352f33 100644--- a/frontend/src/toolbar/loader.ts+++ b/frontend/src/toolbar/loader.ts@@ -25,6 +25,12 @@ type ToolbarModule = typeof import('~/toolbar/index')// build-time public path (set for posthog-js versioned CDN bundles) is the fallback.const scriptSrc = (document.currentScript as HTMLScriptElement | null)?.src+// Share the origin the loader was served from with the app bundle. The app (ToolbarApp) is a+// separately imported module and cannot read document.currentScript once it renders, so it+// resolves its own sibling assets - the stylesheet - from this value. It follows the same+// reverse proxy as the JS this way, instead of the baked absolute CDN host.+;(window as any).__POSTHOG_TOOLBAR_SCRIPT_SRC__ = scriptSrc+function resolveAppUrl(fileName: string): string {const base = scriptSrc || __POSTHOG_TOOLBAR_PUBLIC_PATH__if (!base) {