This repository has been archived by the owner on Jul 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathgatsby-browser.tsx
99 lines (84 loc) · 3.24 KB
/
gatsby-browser.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import type { GatsbyBrowser } from 'gatsby'
import aa from 'search-insights'
import { TrackJS } from 'trackjs'
import LayoutContainer from './src/layouts/layoutContainer'
import { PageContext } from './src/types/pageContext'
import { initDataDogLogging } from './src/utils/browserMetricsUtils'
import { getTargetYOffset, scrollToYPosition } from './src/utils/scrollUtils'
import { addRemoveSiteParam, getSiteFromHref } from './src/utils/siteAwareUtils'
import { initUAParser } from './src/utils/userAgent'
export const onClientEntry = () => {
const activeEnv = process.env.GATSBY_ACTIVE_ENV
if (typeof activeEnv === 'undefined') {
return
}
const isProd = activeEnv === 'production'
const isStaging = activeEnv === 'staging'
const isDev = activeEnv === 'development'
let siteLocalStorage = localStorage.getItem('site')
// HACK: this is a migration step for old ls values that shouldn't
// be an issue on prod and can be removed over time.
if (siteLocalStorage !== JSON.stringify('launchDarkly') && siteLocalStorage !== JSON.stringify('federal')) {
localStorage.removeItem('site')
siteLocalStorage = null
}
if (isProd || (isDev && process.env.RUN_DATADOG_LOCALLY === 'true')) {
initDataDogLogging()
}
if (isProd || isStaging) {
TrackJS.install({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
token: process.env.GATSBY_TRACKJS_TOKEN!,
application: isProd ? 'docs-production' : 'docs-staging',
})
}
if (isStaging) {
TrackJS.configure({ version: process.env.GATSBY_BUCKET_PREFIX })
}
initUAParser()
// Algolia search insights
window.aa = aa
const appId = process.env.GATSBY_ALGOLIA_APP_ID
const apiKey = process.env.GATSBY_ALGOLIA_SEARCH_KEY
aa('init', { appId, apiKey, useCookie: true })
const currentSiteHref = getSiteFromHref()
// query param exists
if (currentSiteHref) {
const currentSiteHrefJson = JSON.stringify(currentSiteHref)
// sync qs and ls values if they are not in sync
// Use qs value as source of truth
if (currentSiteHrefJson !== siteLocalStorage) {
localStorage.setItem('site', currentSiteHrefJson)
}
} else {
// no query param
if (!siteLocalStorage) {
localStorage.setItem('site', JSON.stringify('launchDarkly'))
} else {
// append site=federal to qs and redirect to that
if (JSON.parse(siteLocalStorage) === 'federal') {
const to = addRemoveSiteParam(location.pathname, 'federal', true)
location.replace(to)
}
// else no qs means launchDarkly by default
// ls should already be launchDarkly so we do nothing here
}
}
}
export const wrapPageElement: GatsbyBrowser<unknown, PageContext>['wrapPageElement'] = ({ element, props }) => {
return <LayoutContainer pageContext={props.pageContext}>{element}</LayoutContainer>
}
export const shouldUpdateScroll: GatsbyBrowser['shouldUpdateScroll'] = ({ routerProps: { location } }) => {
// gatsby with reach router has a known scrolling issue where it doesn't work.
// this fixes that problem.
if (location.hash) {
const offset = getTargetYOffset(location.hash)
if (offset !== null) {
setTimeout(() => {
scrollToYPosition(offset)
}, 100)
return [0, offset]
}
}
return true
}