Skip to content

Commit dea3da2

Browse files
authored
Add lazy syntax highlighting hook (github#29610)
1 parent 2a783e7 commit dea3da2

4 files changed

Lines changed: 29 additions & 53 deletions

File tree

components/article/ArticlePage.tsx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { useState, useEffect } from 'react'
21
import { useRouter } from 'next/router'
32
import dynamic from 'next/dynamic'
43

54
import { ZapIcon, InfoIcon, ShieldLockIcon } from '@primer/octicons-react'
65
import { Callout } from 'components/ui/Callout'
76

7+
import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS'
88
import { Link } from 'components/Link'
99
import { DefaultLayout } from 'components/DefaultLayout'
1010
import { ArticleTitle } from 'components/article/ArticleTitle'
@@ -58,23 +58,7 @@ export const ArticlePage = () => {
5858
const { t } = useTranslation('pages')
5959
const currentPath = asPath.split('?')[0]
6060

61-
// If the page contains `[data-highlight]` blocks, these pages need
62-
// syntax highlighting. But not every page needs it, so it's conditionally
63-
// lazy-loaded on the client.
64-
const [lazyLoadHighlightJS, setLazyLoadHighlightJS] = useState(false)
65-
useEffect(() => {
66-
// It doesn't need to use querySelector because all we care about is if
67-
// there is greater than zero of these in the DOM.
68-
// Note! This "core selector", which determines whether to bother
69-
// or not, needs to match what's used inside ClientSideHighlightJS.tsx
70-
if (document.querySelector('[data-highlight]')) {
71-
setLazyLoadHighlightJS(true)
72-
}
73-
74-
// Important to depend on the current path because the first page you
75-
// load, before any client-side navigation, might not need it, but the
76-
// consecutive one does.
77-
}, [asPath])
61+
const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath)
7862

7963
return (
8064
<DefaultLayout>

components/article/AutomatedPage.tsx

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import { useState, useEffect } from 'react'
21
import { useRouter } from 'next/router'
32
import dynamic from 'next/dynamic'
43

4+
import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS'
55
import { DefaultLayout } from 'components/DefaultLayout'
66
import { ArticleTitle } from 'components/article/ArticleTitle'
77
import { MarkdownContent } from 'components/ui/MarkdownContent'
@@ -20,23 +20,7 @@ export const AutomatedPage = ({ children }: Props) => {
2020
const { asPath } = useRouter()
2121
const { title, intro, renderedPage, miniTocItems } = useAutomatedPageContext()
2222

23-
// If the page contains `[data-highlight]` blocks, these pages need
24-
// syntax highlighting. But not every page needs it, so it's conditionally
25-
// lazy-loaded on the client.
26-
const [lazyLoadHighlightJS, setLazyLoadHighlightJS] = useState(false)
27-
useEffect(() => {
28-
// It doesn't need to use querySelector because all we care about is if
29-
// there is greater than zero of these in the DOM.
30-
// Note! This "core selector", which determines whether to bother
31-
// or not, needs to match what's used inside ClientSideHighlightJS.tsx
32-
if (document.querySelector('[data-highlight]')) {
33-
setLazyLoadHighlightJS(true)
34-
}
35-
36-
// Important to depend on the current path because the first page you
37-
// load, before any client-side navigation, might not need it, but the
38-
// consecutive one does.
39-
}, [asPath])
23+
const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath)
4024

4125
return (
4226
<DefaultLayout>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { useState, useEffect } from 'react'
2+
3+
export default function useLazyLoadHighlightJS(path: string) {
4+
// If the page contains `[data-highlight]` blocks, these pages need
5+
// syntax highlighting. But not every page needs i t, so it's conditionally
6+
// lazy-loaded on the client.
7+
const [lazyLoadHighlightJS, setLazyLoadHighlightJS] = useState(false)
8+
useEffect(() => {
9+
// It doesn't need to use querySelector because all we care about is if
10+
// there is greater than zero of these in the DOM.
11+
// Note! This "core selector", which determines whether to bother
12+
// or not, needs to match what's used inside ClientSideHighlightJS.tsx
13+
if (document.querySelector('[data-highlight]')) {
14+
setLazyLoadHighlightJS(true)
15+
}
16+
17+
// Important to depend on the current path because the first page you
18+
// load, before any client-side navigation, might not need it, but the
19+
// consecutive one does.
20+
}, [path])
21+
22+
return lazyLoadHighlightJS
23+
}

components/rest/RestReferencePage.tsx

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { useRouter } from 'next/router'
33
import dynamic from 'next/dynamic'
44
import cx from 'classnames'
55

6+
import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS'
67
import { DefaultLayout } from 'components/DefaultLayout'
78
import { MarkdownContent } from 'components/ui/MarkdownContent'
89
import { Lead } from 'components/ui/Lead'
@@ -73,23 +74,7 @@ export const RestReferencePage = ({ restOperations }: StructuredContentT) => {
7374
})
7475
}, [])
7576

76-
// If the page contains `[data-highlight]` blocks, these pages need
77-
// syntax highlighting. But not every page needs it, so it's conditionally
78-
// lazy-loaded on the client.
79-
const [lazyLoadHighlightJS, setLazyLoadHighlightJS] = useState(false)
80-
useEffect(() => {
81-
// It doesn't need to use querySelector because all we care about is if
82-
// there is greater than zero of these in the DOM.
83-
// Note! This "core selector", which determines whether to bother
84-
// or not, needs to match what's used inside ClientSideHighlightJS.tsx
85-
if (document.querySelector('[data-highlight]')) {
86-
setLazyLoadHighlightJS(true)
87-
}
88-
89-
// Important to depend on the current path because the first page you
90-
// load, before any client-side navigation, might not need it, but the
91-
// consecutive one does.
92-
}, [asPath])
77+
const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath)
9378

9479
return (
9580
<DefaultLayout>

0 commit comments

Comments
 (0)