🧑💻 (bot) Wrap custom head code scripts with try catch
To avoid having the custom head bugs reported on Sentry
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@typebot.io/js",
|
||||
"version": "0.0.70",
|
||||
"version": "0.0.71",
|
||||
"description": "Javascript library to display typebots on your website",
|
||||
"type": "module",
|
||||
"main": "dist/index.js",
|
||||
|
33
packages/embeds/js/src/utils/injectCustomHeadCode.ts
Normal file
33
packages/embeds/js/src/utils/injectCustomHeadCode.ts
Normal file
@ -0,0 +1,33 @@
|
||||
import { isNotDefined } from '@typebot.io/lib/utils'
|
||||
|
||||
export const injectCustomHeadCode = (customHeadCode: string) => {
|
||||
customHeadCode = customHeadCode.replace(
|
||||
/<script>/g,
|
||||
`<script>
|
||||
try {`
|
||||
)
|
||||
customHeadCode = customHeadCode.replace(
|
||||
/<\/script>/g,
|
||||
`} catch(e) {
|
||||
console.warn("Error while executing custom head code", e)
|
||||
}
|
||||
</script>`
|
||||
)
|
||||
const headCodes = customHeadCode.split('</noscript>')
|
||||
headCodes.forEach((headCode) => {
|
||||
const [codeToInject, noScriptContentToInject] = headCode.split('<noscript>')
|
||||
const fragment = document
|
||||
.createRange()
|
||||
.createContextualFragment(codeToInject)
|
||||
document.head.append(fragment)
|
||||
|
||||
if (isNotDefined(noScriptContentToInject)) return
|
||||
|
||||
const noScriptElement = document.createElement('noscript')
|
||||
const noScriptContentFragment = document
|
||||
.createRange()
|
||||
.createContextualFragment(noScriptContentToInject)
|
||||
noScriptElement.append(noScriptContentFragment)
|
||||
document.head.append(noScriptElement)
|
||||
})
|
||||
}
|
@ -1,3 +0,0 @@
|
||||
export const injectHeadCode = (headCode: string) => {
|
||||
document.head.innerHTML = document.head.innerHTML + headCode
|
||||
}
|
@ -2,8 +2,9 @@
|
||||
import { initGoogleAnalytics } from '@/lib/gtag'
|
||||
import { gtmBodyElement } from '@/lib/gtm'
|
||||
import { initPixel } from '@/lib/pixel'
|
||||
import { injectCustomHeadCode, isNotEmpty } from '@typebot.io/lib/utils'
|
||||
import { isNotEmpty } from '@typebot.io/lib/utils'
|
||||
import { StartPropsToInject } from '@typebot.io/schemas'
|
||||
import { injectCustomHeadCode } from './injectCustomHeadCode'
|
||||
|
||||
export const injectStartProps = async (
|
||||
startPropsToInject: StartPropsToInject
|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@typebot.io/react",
|
||||
"version": "0.0.70",
|
||||
"version": "0.0.71",
|
||||
"description": "React library to display typebots on your website",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
|
@ -286,26 +286,6 @@ export const getViewerUrl = (props?: {
|
||||
export const parseNumberWithCommas = (num: number) =>
|
||||
num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',')
|
||||
|
||||
export const injectCustomHeadCode = (customHeadCode: string) => {
|
||||
const headCodes = customHeadCode.split('</noscript>')
|
||||
headCodes.forEach((headCode) => {
|
||||
const [codeToInject, noScriptContentToInject] = headCode.split('<noscript>')
|
||||
const fragment = document
|
||||
.createRange()
|
||||
.createContextualFragment(codeToInject)
|
||||
document.head.append(fragment)
|
||||
|
||||
if (isNotDefined(noScriptContentToInject)) return
|
||||
|
||||
const noScriptElement = document.createElement('noscript')
|
||||
const noScriptContentFragment = document
|
||||
.createRange()
|
||||
.createContextualFragment(noScriptContentToInject)
|
||||
noScriptElement.append(noScriptContentFragment)
|
||||
document.head.append(noScriptElement)
|
||||
})
|
||||
}
|
||||
|
||||
export const getAtPath = <T>(obj: T, path: string): unknown => {
|
||||
if (isNotDefined(obj)) return undefined
|
||||
const pathParts = path.split('.')
|
||||
|
Reference in New Issue
Block a user