2
0

🧑‍💻 (bot) Wrap custom head code scripts with try catch

To avoid having the custom head bugs reported on Sentry
This commit is contained in:
Baptiste Arnaud
2023-07-11 10:04:39 +02:00
parent 8ce5447012
commit 1beb4bb1b9
6 changed files with 37 additions and 26 deletions

View File

@ -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",

View 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)
})
}

View File

@ -1,3 +0,0 @@
export const injectHeadCode = (headCode: string) => {
document.head.innerHTML = document.head.innerHTML + headCode
}

View File

@ -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

View File

@ -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",

View File

@ -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('.')