Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/routes/__root.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as Sentry from '@sentry/tanstackstart-react'
import { getLiveRuntimeConfig } from '~/utils/analytics/live.functions'
import {
resumeVisitorChat,
Expand Down Expand Up @@ -181,12 +182,29 @@ class OptionalDevtoolsBoundary extends React.Component<
}
}

const LIVE_RUNTIME_CONFIG_DISABLED = {
presenceEnabled: false,
chatEnabled: false,
}

// Live presence/chat is optional. A failure here must not put the root route
// (and therefore the whole site) into its error state, but it is still
// reported so the underlying problem stays visible.
async function loadLiveRuntimeConfig() {
try {
return await getLiveRuntimeConfig()
} catch (error) {
Sentry.captureException(error, { tags: { feature: 'live-config' } })
return LIVE_RUNTIME_CONFIG_DISABLED
}
}

export const Route = createRootRouteWithContext<{
queryClient: QueryClient
}>()({
loader: async () => {
return {
live: await getLiveRuntimeConfig(),
live: await loadLiveRuntimeConfig(),
partnerPlacementSessionSeed: createPartnerPlacementSessionSeed(),
}
},
Expand Down Expand Up @@ -462,7 +480,11 @@ function ShellComponent({ children }: { children: React.ReactNode }) {
* stay disabled on the web stream, or navigations are counted twice.
*/
function PageViewTracker() {
const { live } = Route.useLoaderData()
// The shell renders even when the root loader failed or hasn't resolved,
// so loader data may be missing here. Live features default to off.
const live = Route.useLoaderData()?.live
const presenceEnabled = live?.presenceEnabled === true
const chatEnabled = live?.chatEnabled === true
const pagePath = useRouterState({
select: (s) => {
if (!s.resolvedLocation) {
Expand All @@ -479,10 +501,8 @@ function PageViewTracker() {

React.useEffect(() => {
if (import.meta.env.MODE === 'staging') return
if (live.chatEnabled) void resumeVisitorChat().catch(() => {})
const stopLivePresence = live.presenceEnabled
? startLivePresence()
: () => {}
if (chatEnabled) void resumeVisitorChat().catch(() => {})
const stopLivePresence = presenceEnabled ? startLivePresence() : () => {}
const stopTrackingHoverIntent = trackScarfExternalLinkHoverIntent()
document.addEventListener('click', trackScarfExternalLinkClick, true)
document.addEventListener('click', trackScarfDownloadClick, true)
Expand All @@ -496,7 +516,7 @@ function PageViewTracker() {
document.removeEventListener('copy', trackScarfClipboardEvent, true)
document.removeEventListener('paste', trackScarfClipboardEvent, true)
}
}, [live.presenceEnabled, live.chatEnabled])
}, [presenceEnabled, chatEnabled])

React.useEffect(() => {
// Skip until the router has resolved a location, and never send the same
Expand Down
Loading