Skip to content

Commit

Permalink
fix: Remove API and JS urls (#630)
Browse files Browse the repository at this point in the history
* remove api url

* 1.57.1-alpha.0

* 1.57.2-alpha.0

---------

Co-authored-by: Michael Matloka <[email protected]>
  • Loading branch information
mariusandra and Twixes authored May 17, 2023
1 parent 2bd489f commit 67e07eb
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "posthog-js",
"version": "1.57.1",
"version": "1.57.2-alpha.0",
"description": "Posthog-js allows you to automatically capture usage and send events to PostHog.",
"repository": "https://github.com/PostHog/posthog-js",
"author": "[email protected]",
Expand Down
24 changes: 16 additions & 8 deletions src/__tests__/extensions/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ describe('Toolbar', () => {
flag_2: 1,
},
userId: 12345,
apiURL: given.config.api_host,
...given.toolbarParamsOverrides,
}))

Expand All @@ -92,14 +91,20 @@ describe('Toolbar', () => {
}))

given.subject()
expect(given.toolbar.loadToolbar).toHaveBeenCalledWith({ ...given.toolbarParams, source: 'url' })
expect(given.toolbar.loadToolbar).toHaveBeenCalledWith({
...given.toolbarParams,
source: 'url',
})
})

it('should initialize the toolbar when there are editor params in the session', () => {
given('storedEditorParams', () => JSON.stringify(toolbarParams))

given.subject()
expect(given.toolbar.loadToolbar).toHaveBeenCalledWith({ ...given.toolbarParams, source: 'url' })
expect(given.toolbar.loadToolbar).toHaveBeenCalledWith({
...given.toolbarParams,
source: 'url',
})
})

it('should NOT initialize the toolbar when the activation query param does not exist', () => {
Expand Down Expand Up @@ -152,18 +157,22 @@ describe('Toolbar', () => {
token: 'public_token',
expiresAt: 'expiresAt',
apiKey: 'apiKey',
apiURL: 'http://localhost:8000',
jsURL: 'http://localhost:8000',
}))

it('should persist for next time', () => {
expect(given.subject()).toBe(true)
expect(JSON.parse(window.localStorage.getItem('_postHogToolbarParams'))).toEqual(given.toolbarParams)
expect(JSON.parse(window.localStorage.getItem('_postHogToolbarParams'))).toEqual({
...given.toolbarParams,
apiURL: 'http://api.example.com',
})
})

it('should load if not previously loaded', () => {
expect(given.subject()).toBe(true)
expect(window.ph_load_toolbar).toHaveBeenCalledWith(given.toolbarParams, given.lib)
expect(window.ph_load_toolbar).toHaveBeenCalledWith(
{ ...given.toolbarParams, apiURL: 'http://api.example.com' },
given.lib
)
})

it('should NOT load if previously loaded', () => {
Expand All @@ -184,7 +193,6 @@ describe('Toolbar', () => {
expect(window.ph_load_toolbar).toHaveBeenCalledWith(
{
...given.toolbarParams,
jsURL: 'http://api.example.com',
apiURL: 'http://api.example.com',
token: 'test_token',
},
Expand Down
11 changes: 2 additions & 9 deletions src/extensions/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ export class Toolbar {
) {
this.loadToolbar({
...toolbarParams,
apiURL: this.instance.get_config('api_host'),
})
}
}
Expand Down Expand Up @@ -80,10 +79,6 @@ export class Toolbar {
delete toolbarParams.userIntent
}

if (!toolbarParams.apiURL) {
toolbarParams.apiURL = this.instance.get_config('api_host')
}

if (toolbarParams['token'] && this.instance.get_config('token') === toolbarParams['token']) {
this.loadToolbar(toolbarParams)
return true
Expand All @@ -102,10 +97,9 @@ export class Toolbar {
// only load the toolbar once, even if there are multiple instances of PostHogLib
;(window as any)['_postHogToolbarLoaded'] = true

// the toolbar does not use the `jsURL` as that route is cached for 24 hours.
// 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 = params?.['apiURL'] || this.instance.get_config('api_host')
const host = this.instance.get_config('api_host')
const timestampToNearestThirtySeconds = Math.floor(Date.now() / 30000) * 30000
const toolbarUrl = `${host}${
host.endsWith('/') ? '' : '/'
Expand All @@ -115,10 +109,9 @@ export class Toolbar {
this.instance.get_config('advanced_disable_toolbar_metrics')

const toolbarParams = {
apiURL: host, // defaults to api_host from the instance config if nothing else set
jsURL: host, // defaults to api_host from the instance config if nothing else set
token: this.instance.get_config('token'),
...params,
apiURL: host, // defaults to api_host from the instance config if nothing else set
...(disableToolbarMetrics ? { instrument: false } : {}),
}

Expand Down
2 changes: 0 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ export type ToolbarVersion = 'toolbar'

/* sync with posthog */
export interface ToolbarParams {
apiURL?: string
jsURL?: string
token?: string /** public posthog-js token */
temporaryToken?: string /** private temporary user token */
actionId?: number
Expand Down

0 comments on commit 67e07eb

Please sign in to comment.