Skip to content

Commit

Permalink
fix: inject script after boot function
Browse files Browse the repository at this point in the history
  • Loading branch information
devonik committed Aug 15, 2023
1 parent 1a29aba commit 5412e29
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
22 changes: 21 additions & 1 deletion src/runtime/Intercom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class Intercom {
visible: boolean
/** Remove all not listed non-permanent user data from cache. Anything not removed will be sent again with next update. */
permanentUserData: string[] = ['app_id', 'email', 'user_id', 'user_hash']
isScriptInjected: boolean

constructor(config: NuxtIntercomConfig, { userData = {} } = {}) {
this.appId = config.appId
Expand All @@ -20,6 +21,7 @@ export default class Intercom {
this.unreadCount = 0
this.userData = userData
this.visible = false
this.isScriptInjected = false
}

/**
Expand Down Expand Up @@ -62,6 +64,21 @@ export default class Intercom {
return this.ready
}

injectIntercomScript(appId?: string) {
if (!appId)
throw new Error('Could not inject intercom script cause appId is undefined')

if (this.isScriptInjected)
return

const intercomScript = document.createElement('script')
intercomScript.async = true
intercomScript.src = `https://widget.intercom.io/widget/${this.appId}`
const firstScript = document.getElementsByTagName('script')[0]
firstScript.parentNode.insertBefore(intercomScript, firstScript)
this.isScriptInjected = true
}

/**
* Boot Intercom. If an appId is provided, this will be used for all future calls to Intercom.
*/
Expand All @@ -84,8 +101,11 @@ export default class Intercom {
const snakeCaseKey = key.replace(/([A-Z])/g, '_$1').toLowerCase()
window.intercomSettings[snakeCaseKey] = intercomSettings[key]
}
const response = this._updateData('boot', intercomSettings)

this.injectIntercomScript(this.appId)

return this._updateData('boot', intercomSettings)
return response
}

/**
Expand Down
22 changes: 1 addition & 21 deletions src/runtime/plugin.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ function createIntercomPlaceholder() {

return placeholder
}
function includeIntercomScript(appId, callback) {
const intercomScript = document.createElement('script')
intercomScript.async = true
intercomScript.src = `https://widget.intercom.io/widget/${appId}`
const firstScript = document.getElementsByTagName('script')[0]
firstScript.parentNode.insertBefore(intercomScript, firstScript)

intercomScript.addEventListener('load', callback)
}
function callWhenPageLoaded(callback) {
if (window.attachEvent)
window.attachEvent('onload', callback)

else
window.addEventListener('load', callback, false)
}

function initialiseIntercom(ctx: any, intercom: Intercom, config: NuxtIntercomConfig) {
intercom.init()
Expand Down Expand Up @@ -54,15 +38,11 @@ export default defineNuxtPlugin((nuxtApp) => {
else {
window.Intercom = createIntercomPlaceholder()

const callWhenIntercomScriptLoaded = initialiseIntercom(
initialiseIntercom(
nuxtApp,
intercom,
config,
)

callWhenPageLoaded(() =>
includeIntercomScript(config.appId, callWhenIntercomScriptLoaded),
)
}
return {
provide: {
Expand Down

0 comments on commit 5412e29

Please sign in to comment.