Skip to content

Commit

Permalink
🧑‍💻 (bot) Wrap custom head code scripts with try catch
Browse files Browse the repository at this point in the history
To avoid having the custom head bugs reported on Sentry
  • Loading branch information
baptisteArno committed Jul 11, 2023
1 parent 8ce5447 commit 1beb4bb
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/embeds/js/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
33 changes: 33 additions & 0 deletions packages/embeds/js/src/utils/injectCustomHeadCode.ts
Original file line number Diff line number Diff line change
@@ -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)
})
}
3 changes: 0 additions & 3 deletions packages/embeds/js/src/utils/injectHeadCode.ts

This file was deleted.

3 changes: 2 additions & 1 deletion packages/embeds/js/src/utils/injectStartProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/embeds/react/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 0 additions & 20 deletions packages/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')
Expand Down

3 comments on commit 1beb4bb

@vercel
Copy link

@vercel vercel bot commented on 1beb4bb Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on 1beb4bb Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

builder-v2 – ./apps/builder

builder-v2-typebot-io.vercel.app
builder-v2-git-main-typebot-io.vercel.app
app.typebot.io

@vercel
Copy link

@vercel vercel bot commented on 1beb4bb Jul 11, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

docs – ./apps/docs

docs-typebot-io.vercel.app
docs-git-main-typebot-io.vercel.app
docs.typebot.io

Please sign in to comment.