From 6ec13004d104ba1e2a0c1d895d9a8aca9d52ef3d Mon Sep 17 00:00:00 2001 From: Paul D'Ambra Date: Fri, 15 Sep 2023 11:15:27 +0100 Subject: [PATCH] fix: toolbar cache busting (#798) --- src/extensions/toolbar.ts | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/extensions/toolbar.ts b/src/extensions/toolbar.ts index 5cc071b6e..111a69cbf 100644 --- a/src/extensions/toolbar.ts +++ b/src/extensions/toolbar.ts @@ -97,13 +97,13 @@ export class Toolbar { // only load the toolbar once, even if there are multiple instances of PostHogLib ;(window as any)['_postHogToolbarLoaded'] = true - // By design array.js, recorder.js, and toolbar.js are served from Django with no or limited caching, not from our CDN - // Django respects the query params for caching, returning a 304 if appropriate const host = this.instance.get_config('api_host') - const timestampToNearestThirtySeconds = Math.floor(Date.now() / 30000) * 30000 - const toolbarUrl = `${host}${ - host.endsWith('/') ? '' : '/' - }static/toolbar.js?_ts=${timestampToNearestThirtySeconds}` + // toolbar.js is served from the PostHog CDN, this has a TTL of 24 hours. + // the toolbar asset includes a rotating "token" that is valid for 5 minutes. + const fiveMinutesInMillis = 5 * 60 * 1000 + // this ensures that we bust the cache periodically + const timestampToNearestFiveMinutes = Math.floor(Date.now() / fiveMinutesInMillis) * fiveMinutesInMillis + const toolbarUrl = `${host}${host.endsWith('/') ? '' : '/'}static/toolbar.js?t=${timestampToNearestFiveMinutes}` const disableToolbarMetrics = !POSTHOG_MANAGED_HOSTS.includes(this.instance.get_config('api_host')) && this.instance.get_config('advanced_disable_toolbar_metrics')