Skip to content

Commit

Permalink
Allow setting asset_host to not load assets from api_host
Browse files Browse the repository at this point in the history
  • Loading branch information
frankh committed Jan 9, 2024
1 parent 958fec4 commit 72c94ba
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/__tests__/extensions/replay/sessionrecording.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ describe('SessionRecording', () => {

config = {
api_host: 'https://test.com',
asset_host: 'https://test.com',
disable_session_recording: false,
enable_recording_console_log: false,
autocapture: false, // Assert that session recording works even if `autocapture = false`
Expand Down
4 changes: 2 additions & 2 deletions src/decide.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Decide {
const surveysGenerator = window?.extendPostHogWithSurveys

if (response['surveys'] && !surveysGenerator) {
loadScript(this.instance.config.api_host + `/static/surveys.js`, (err) => {
loadScript(this.instance.config.asset_host + `/static/surveys.js`, (err) => {
if (err) {
return logger.error(`Could not load surveys script`, err)
}
Expand All @@ -95,7 +95,7 @@ export class Decide {
!!response['autocaptureExceptions'] &&
_isUndefined(exceptionAutoCaptureAddedToWindow)
) {
loadScript(this.instance.config.api_host + `/static/exception-autocapture.js`, (err) => {
loadScript(this.instance.config.asset_host + `/static/exception-autocapture.js`, (err) => {
if (err) {
return logger.error(`Could not load exception autocapture script`, err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/replay/sessionrecording.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ export class SessionRecording {
// imported) or matches the requested recorder version, don't load script. Otherwise, remotely import
// recorder.js from cdn since it hasn't been loaded.
if (this.instance.__loaded_recorder_version !== this.recordingVersion) {
loadScript(this.instance.config.api_host + `/static/${recorderJS}?v=${Config.LIB_VERSION}`, (err) => {
loadScript(this.instance.config.asset_host + `/static/${recorderJS}?v=${Config.LIB_VERSION}`, (err) => {
if (err) {
return logger.error(`Could not load ${recorderJS}`, err)
}
Expand Down
2 changes: 1 addition & 1 deletion src/extensions/toolbar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class Toolbar {
// only load the toolbar once, even if there are multiple instances of PostHogLib
assignableWindow['_postHogToolbarLoaded'] = true

const host = this.instance.config.api_host
const host = this.instance.config.asset_host || this.instance.config.api_host
// 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
Expand Down
7 changes: 7 additions & 0 deletions src/posthog-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ export const defaultConfig = (): PostHogConfig => ({
api_host: 'https://app.posthog.com',
api_method: 'POST',
api_transport: 'XHR',
asset_host: undefined,
ui_host: null,
token: '',
autocapture: true,
Expand Down Expand Up @@ -1592,6 +1593,9 @@ export class PostHog {
* // the original web app host needs to be passed here so that links to the web app are still convenient.
* ui_host: 'https://app.posthog.com',
*
* // Host to load static assets from. If undefined falls back to api_host
* asset_host: undefined,
*
* // Automatically capture clicks, form submissions and change events
* autocapture: true
*
Expand Down Expand Up @@ -1722,6 +1726,9 @@ export class PostHog {

// We assume the api_host is without a trailing slash in most places throughout the codebase
this.config.api_host = this.config.api_host.replace(/\/$/, '')
if (this.config.asset_host === undefined) {
this.config.asset_host = this.config.api_host
}
this.persistence?.update_config(this.config)
this.sessionPersistence?.update_config(this.config)

Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export interface PostHogConfig {
api_host: string
api_method: string
api_transport: string
asset_host?: string
ui_host: string | null
token: string
autocapture: boolean | AutocaptureConfig
Expand Down

0 comments on commit 72c94ba

Please sign in to comment.