When using webdriver to test our website, an Intercom widget becomes embedded in the screenshot as the webdriver scrolls down. Attempts to hide Intercom via CSS have resulted in a javascript error:
Cannot read properties of null (reading 'style').
It's worth noting that Intercom is loaded through Segment.
The loading mechanism involves NextJS, with the following snippet found in /pages/document.tsx
:
<script
type="text/partytown" dangerouslySetInnerHTML={{ __html: `!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e<analytics.methods.length;e++){var key=analytics.methods[e];analytics[key]=analytics.factory(key)}analytics.load=function(key,e){var t=document.createElement("script");t.type="text/javascript";t.async=!0;t.src="https://cdn.segment.com/analytics.js/v1/" + key + "/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(t,n);analytics._loadOptions=e};analytics._writeKey="xxxxx";;analytics.SNIPPET_VERSION="4.15.3";analytics.load("xxxx");analytics.page();}}();` }} />
If there is a need to inform NextJS to unload the Segment script when webdriver loads a URL containing certain parameters (e.g. www.example.com?no-segment
), how can this be achieved?
UPDATE: The content of /pages/_document.tsx
has been revised as follows:
import { Partytown } from '@builder.io/partytown/react';
import { Head, Html, Main, NextScript } from 'next/document'
import { ServerStyleSheetDocument } from 'next-sanity/studio'
import { useRouter } from 'next/router'
import { useState, useEffect } from 'react'
const router = useRouter();
const [isUnloadSegment, setIsUnloadSegment] = useState(false);
useEffect(() => {
if (router.query['no-segment'] !== undefined) {
setIsUnloadSegment(true);
}
}, [router.query]);
export default class Document extends ServerStyleSheetDocument {
render() {
return (
<Html lang="en">
<Head>
<Partytown debug={false} forward={['dataLayer.push']} />
<style dangerouslySetInnerHTML={{
__html: `@font-face {font-family: 'Poppins';src: url('/fonts/Poppins-Regular.woff2') format('woff2');font-weight: normal;font-style: normal;font-display: swap;}@font-face {font-family: 'Poppins';src: url('/fonts/Poppins-Medium.woff2') format('woff2');font-weight: 500;font-style: normal;font-display: swap;}@font-face {font-family: 'Poppins';src: url('/fonts/Poppins-SemiBold.woff2') format('woff2'),;font-weight: 600;font-style: normal;font-display: swap;}@font-face {font-family: 'Poppins';src: url('/fonts/Poppins-Italic.woff2') format('woff2');font-weight: normal;font-style: italic;font-display: swap;}`}} />
</Head>
<body>
<Main />
<NextScript />
{!isUnloadSegment && (
<script
type="text/partytown" dangerouslySetInnerHTML={{ __html: `!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","debug","page","once","off","on","addSourceMiddleware","addIntegrationMiddleware","setAnonymousId","addDestinationMiddleware"];analytics.factory=function(e){return function(){var t=Array.prototype.slice.call(arguments);t.unshift(e);analytics.push(t);return analytics}};for(var e=0;e<analytics.methods.length;e++){var key=analytics.methods[e];analytics[key]=analytics.factory(key)}analytics.load=function...
)}
</body>
</Html>
)
}
}
Unfortunately, running the local site triggers an error message:
TypeError: Cannot read properties of null (reading 'useContext')