posthog-js 1.78.5 → 1.78.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array.full.js +1 -1
- package/dist/array.full.js.map +1 -1
- package/dist/array.js +1 -1
- package/dist/array.js.map +1 -1
- package/dist/es.js +1 -1
- package/dist/es.js.map +1 -1
- package/dist/module.js +1 -1
- package/dist/module.js.map +1 -1
- package/lib/package.json +1 -1
- package/lib/src/extensions/toolbar.js +6 -4
- package/lib/src/extensions/toolbar.js.map +1 -1
- package/package.json +1 -1
package/lib/package.json
CHANGED
|
@@ -107,11 +107,13 @@ var Toolbar = /** @class */ (function () {
|
|
|
107
107
|
// only load the toolbar once, even if there are multiple instances of PostHogLib
|
|
108
108
|
;
|
|
109
109
|
window['_postHogToolbarLoaded'] = true;
|
|
110
|
-
// By design array.js, recorder.js, and toolbar.js are served from Django with no or limited caching, not from our CDN
|
|
111
|
-
// Django respects the query params for caching, returning a 304 if appropriate
|
|
112
110
|
var host = this.instance.get_config('api_host');
|
|
113
|
-
|
|
114
|
-
|
|
111
|
+
// toolbar.js is served from the PostHog CDN, this has a TTL of 24 hours.
|
|
112
|
+
// the toolbar asset includes a rotating "token" that is valid for 5 minutes.
|
|
113
|
+
var fiveMinutesInMillis = 5 * 60 * 1000;
|
|
114
|
+
// this ensures that we bust the cache periodically
|
|
115
|
+
var timestampToNearestFiveMinutes = Math.floor(Date.now() / fiveMinutesInMillis) * fiveMinutesInMillis;
|
|
116
|
+
var toolbarUrl = "".concat(host).concat(host.endsWith('/') ? '' : '/', "static/toolbar.js?t=").concat(timestampToNearestFiveMinutes);
|
|
115
117
|
var disableToolbarMetrics = !POSTHOG_MANAGED_HOSTS.includes(this.instance.get_config('api_host')) &&
|
|
116
118
|
this.instance.get_config('advanced_disable_toolbar_metrics');
|
|
117
119
|
var toolbarParams = __assign(__assign(__assign({ token: this.instance.get_config('token') }, params), { apiURL: host }), (disableToolbarMetrics ? { instrument: false } :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U,MAAM,CAAC,OAAO;QAExB,OAAO,IAAI,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,CAAA;IACjE,CAAC;IACL,cAAC;AAAD,CAAC,AAhJD,IAgJC","sourcesContent":["import { _getHashParam, _register_event, loadScript, logger } from '../utils'\nimport { PostHog } from '../posthog-core'\nimport { DecideResponse, ToolbarParams } from '../types'\nimport { POSTHOG_MANAGED_HOSTS } from './cloud'\n\nexport class Toolbar {\n instance: PostHog\n constructor(instance: PostHog) {\n this.instance = instance\n }\n\n afterDecideResponse(response: DecideResponse) {\n const toolbarParams: ToolbarParams =\n response['toolbarParams'] ||\n response['editorParams'] ||\n (response['toolbarVersion'] ? { toolbarVersion: response['toolbarVersion'] } : {})\n if (\n response['isAuthenticated'] &&\n toolbarParams['toolbarVersion'] &&\n toolbarParams['toolbarVersion'].indexOf('toolbar') === 0\n ) {\n this.loadToolbar({\n ...toolbarParams,\n })\n }\n }\n\n /**\n * To load the toolbar, we need an access token and other state. That state comes from one of three places:\n * 1. In the URL hash params\n * 2. From session storage under the key `toolbarParams` if the toolbar was initialized on a previous page\n */\n maybeLoadToolbar(\n location = window.location,\n localStorage: Storage | undefined = undefined,\n history = window.history\n ): boolean {\n try {\n // Before running the code we check if we can access localStorage, if not we opt-out\n if (!localStorage) {\n try {\n window.localStorage.setItem('test', 'test')\n window.localStorage.removeItem('test')\n } catch (error) {\n return false\n }\n\n // If localStorage was undefined, and localStorage is supported we set the default value\n localStorage = window.localStorage\n }\n\n const stateHash = _getHashParam(location.hash, '__posthog') || _getHashParam(location.hash, 'state')\n const state = stateHash ? JSON.parse(decodeURIComponent(stateHash)) : null\n const parseFromUrl = state && state['action'] === 'ph_authorize'\n let toolbarParams: ToolbarParams\n\n if (parseFromUrl) {\n // happens if they are initializing the toolbar using an old snippet\n toolbarParams = state\n toolbarParams.source = 'url'\n\n if (toolbarParams && Object.keys(toolbarParams).length > 0) {\n if (state['desiredHash']) {\n // hash that was in the url before the redirect\n location.hash = state['desiredHash']\n } else if (history) {\n history.replaceState('', document.title, location.pathname + location.search) // completely remove hash\n } else {\n location.hash = '' // clear hash (but leaves # unfortunately)\n }\n }\n } else {\n // get credentials from localStorage from a previous initialzation\n toolbarParams = JSON.parse(localStorage.getItem('_postHogToolbarParams') || '{}')\n toolbarParams.source = 'localstorage'\n\n // delete \"add-action\" or other intent from toolbarParams, otherwise we'll have the same intent\n // every time we open the page (e.g. you just visiting your own site an hour later)\n delete toolbarParams.userIntent\n }\n\n if (toolbarParams['token'] && this.instance.get_config('token') === toolbarParams['token']) {\n this.loadToolbar(toolbarParams)\n return true\n } else {\n return false\n }\n } catch (e) {\n return false\n }\n }\n\n loadToolbar(params?: ToolbarParams): boolean {\n if ((window as any)['_postHogToolbarLoaded']) {\n return false\n }\n // only load the toolbar once, even if there are multiple instances of PostHogLib\n ;(window as any)['_postHogToolbarLoaded'] = true\n\n // By design array.js, recorder.js, and toolbar.js are served from Django with no or limited caching, not from our CDN\n // Django respects the query params for caching, returning a 304 if appropriate\n const host = this.instance.get_config('api_host')\n const timestampToNearestThirtySeconds = Math.floor(Date.now() / 30000) * 30000\n const toolbarUrl = `${host}${\n host.endsWith('/') ? '' : '/'\n }static/toolbar.js?_ts=${timestampToNearestThirtySeconds}`\n const disableToolbarMetrics =\n !POSTHOG_MANAGED_HOSTS.includes(this.instance.get_config('api_host')) &&\n this.instance.get_config('advanced_disable_toolbar_metrics')\n\n const toolbarParams = {\n token: this.instance.get_config('token'),\n ...params,\n apiURL: host, // defaults to api_host from the instance config if nothing else set\n ...(disableToolbarMetrics ? { instrument: false } : {}),\n }\n\n const { source: _discard, ...paramsToPersist } = toolbarParams // eslint-disable-line\n window.localStorage.setItem('_postHogToolbarParams', JSON.stringify(paramsToPersist))\n\n loadScript(toolbarUrl, (err) => {\n if (err) {\n logger.error('Failed to load toolbar', err)\n return\n }\n ;((window as any)['ph_load_toolbar'] || (window as any)['ph_load_editor'])(toolbarParams, this.instance)\n })\n // Turbolinks doesn't fire an onload event but does replace the entire body, including the toolbar.\n // Thus, we ensure the toolbar is only loaded inside the body, and then reloaded on turbolinks:load.\n _register_event(window, 'turbolinks:load', () => {\n ;(window as any)['_postHogToolbarLoaded'] = false\n this.loadToolbar(toolbarParams)\n })\n return true\n }\n\n /** @deprecated Use \"loadToolbar\" instead. */\n _loadEditor(params: ToolbarParams): boolean {\n return this.loadToolbar(params)\n }\n\n /** @deprecated Use \"maybeLoadToolbar\" instead. */\n maybeLoadEditor(\n location = window.location,\n localStorage: Storage | undefined = undefined,\n history = window.history\n ): boolean {\n return this.maybeLoadToolbar(location, localStorage, history)\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"toolbar.js","sourceRoot":"","sources":["../../../src/extensions/toolbar.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAAE,aAAa,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,EAAE,MAAM,UAAU,CAAA;AAG7E,OAAO,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAE/C;IAEI,iBAAY,QAAiB;QACzB,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;IAC5B,CAAC;IAED,qCAAmB,GAAnB,UAAoB,QAAwB;QACxC,IAAM,aAAa,GACf,QAAQ,CAAC,eAAe,CAAC;YACzB,QAAQ,CAAC,cAAc,CAAC;YACxB,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CAAC,CAAC,CAAC,EAAE,cAAc,EAAE,QAAQ,CAAC,gBAAgB,CAAC,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAA;QACtF,IACI,QAAQ,CAAC,iBAAiB,CAAC;YAC3B,aAAa,CAAC,gBAAgB,CAAC;YAC/B,aAAa,CAAC,gBAAgB,CAAC,CAAC,OAAO,CAAC,SAAS,CAAC,KAAK,CAAC,EAC1D;YACE,IAAI,CAAC,WAAW,cACT,aAAa,EAClB,CAAA;SACL;IACL,CAAC;IAED;;;;OAIG;IACH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B,EAAE;YACvC,CAAC;YAAC,MAAc,CAAC,uBAAuB,CAAC,GAAG,KAAK,CAAA;YACjD,KAAI,CAAC,WAAW,CAAC,aAAa,CAAC,CAAA;QACnC,CAAC,CAAC,CAAA;QACF,OAAO,IAAI,CAAA;IACf,CAAC;IAED,6CAA6C;IAC7C,6BAAW,GAAX,UAAY,MAAqB;QAC7B,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;IACnC,CAAC;IAED,kDAAkD;IAClD,iCAAe,GAAf,UACI,QAA0B,EAC1B,YAA6C,EAC7C,OAAwB;QAFxB,yBAAA,EAAA,WAAW,MAAM,CAAC,QAAQ;QAC1B,6BAAA,EAAA,wBAA6C;QAC7C,wBAAA,EAAA,UAAU,MAAM,CAAC,OAAO;QAExB,OAAO,IAAI,CAAC,gBAAgB,CAAC,QAAQ,EAAE,YAAY,EAAE,OAAO,CAAC,CAAA;IACjE,CAAC;IACL,cAAC;AAAD,CAAC,AAhJD,IAgJC","sourcesContent":["import { _getHashParam, _register_event, loadScript, logger } from '../utils'\nimport { PostHog } from '../posthog-core'\nimport { DecideResponse, ToolbarParams } from '../types'\nimport { POSTHOG_MANAGED_HOSTS } from './cloud'\n\nexport class Toolbar {\n instance: PostHog\n constructor(instance: PostHog) {\n this.instance = instance\n }\n\n afterDecideResponse(response: DecideResponse) {\n const toolbarParams: ToolbarParams =\n response['toolbarParams'] ||\n response['editorParams'] ||\n (response['toolbarVersion'] ? { toolbarVersion: response['toolbarVersion'] } : {})\n if (\n response['isAuthenticated'] &&\n toolbarParams['toolbarVersion'] &&\n toolbarParams['toolbarVersion'].indexOf('toolbar') === 0\n ) {\n this.loadToolbar({\n ...toolbarParams,\n })\n }\n }\n\n /**\n * To load the toolbar, we need an access token and other state. That state comes from one of three places:\n * 1. In the URL hash params\n * 2. From session storage under the key `toolbarParams` if the toolbar was initialized on a previous page\n */\n maybeLoadToolbar(\n location = window.location,\n localStorage: Storage | undefined = undefined,\n history = window.history\n ): boolean {\n try {\n // Before running the code we check if we can access localStorage, if not we opt-out\n if (!localStorage) {\n try {\n window.localStorage.setItem('test', 'test')\n window.localStorage.removeItem('test')\n } catch (error) {\n return false\n }\n\n // If localStorage was undefined, and localStorage is supported we set the default value\n localStorage = window.localStorage\n }\n\n const stateHash = _getHashParam(location.hash, '__posthog') || _getHashParam(location.hash, 'state')\n const state = stateHash ? JSON.parse(decodeURIComponent(stateHash)) : null\n const parseFromUrl = state && state['action'] === 'ph_authorize'\n let toolbarParams: ToolbarParams\n\n if (parseFromUrl) {\n // happens if they are initializing the toolbar using an old snippet\n toolbarParams = state\n toolbarParams.source = 'url'\n\n if (toolbarParams && Object.keys(toolbarParams).length > 0) {\n if (state['desiredHash']) {\n // hash that was in the url before the redirect\n location.hash = state['desiredHash']\n } else if (history) {\n history.replaceState('', document.title, location.pathname + location.search) // completely remove hash\n } else {\n location.hash = '' // clear hash (but leaves # unfortunately)\n }\n }\n } else {\n // get credentials from localStorage from a previous initialzation\n toolbarParams = JSON.parse(localStorage.getItem('_postHogToolbarParams') || '{}')\n toolbarParams.source = 'localstorage'\n\n // delete \"add-action\" or other intent from toolbarParams, otherwise we'll have the same intent\n // every time we open the page (e.g. you just visiting your own site an hour later)\n delete toolbarParams.userIntent\n }\n\n if (toolbarParams['token'] && this.instance.get_config('token') === toolbarParams['token']) {\n this.loadToolbar(toolbarParams)\n return true\n } else {\n return false\n }\n } catch (e) {\n return false\n }\n }\n\n loadToolbar(params?: ToolbarParams): boolean {\n if ((window as any)['_postHogToolbarLoaded']) {\n return false\n }\n // only load the toolbar once, even if there are multiple instances of PostHogLib\n ;(window as any)['_postHogToolbarLoaded'] = true\n\n const host = this.instance.get_config('api_host')\n // toolbar.js is served from the PostHog CDN, this has a TTL of 24 hours.\n // the toolbar asset includes a rotating \"token\" that is valid for 5 minutes.\n const fiveMinutesInMillis = 5 * 60 * 1000\n // this ensures that we bust the cache periodically\n const timestampToNearestFiveMinutes = Math.floor(Date.now() / fiveMinutesInMillis) * fiveMinutesInMillis\n const toolbarUrl = `${host}${host.endsWith('/') ? '' : '/'}static/toolbar.js?t=${timestampToNearestFiveMinutes}`\n const disableToolbarMetrics =\n !POSTHOG_MANAGED_HOSTS.includes(this.instance.get_config('api_host')) &&\n this.instance.get_config('advanced_disable_toolbar_metrics')\n\n const toolbarParams = {\n token: this.instance.get_config('token'),\n ...params,\n apiURL: host, // defaults to api_host from the instance config if nothing else set\n ...(disableToolbarMetrics ? { instrument: false } : {}),\n }\n\n const { source: _discard, ...paramsToPersist } = toolbarParams // eslint-disable-line\n window.localStorage.setItem('_postHogToolbarParams', JSON.stringify(paramsToPersist))\n\n loadScript(toolbarUrl, (err) => {\n if (err) {\n logger.error('Failed to load toolbar', err)\n return\n }\n ;((window as any)['ph_load_toolbar'] || (window as any)['ph_load_editor'])(toolbarParams, this.instance)\n })\n // Turbolinks doesn't fire an onload event but does replace the entire body, including the toolbar.\n // Thus, we ensure the toolbar is only loaded inside the body, and then reloaded on turbolinks:load.\n _register_event(window, 'turbolinks:load', () => {\n ;(window as any)['_postHogToolbarLoaded'] = false\n this.loadToolbar(toolbarParams)\n })\n return true\n }\n\n /** @deprecated Use \"loadToolbar\" instead. */\n _loadEditor(params: ToolbarParams): boolean {\n return this.loadToolbar(params)\n }\n\n /** @deprecated Use \"maybeLoadToolbar\" instead. */\n maybeLoadEditor(\n location = window.location,\n localStorage: Storage | undefined = undefined,\n history = window.history\n ): boolean {\n return this.maybeLoadToolbar(location, localStorage, history)\n }\n}\n"]}
|
package/package.json
CHANGED