refactor clientsidehighlight (#29713)
* refactor ClientSideHighlight * names * names again
This commit is contained in:
@@ -1,23 +1,31 @@
|
|||||||
import { useState, useEffect } from 'react'
|
import { useState, useEffect } from 'react'
|
||||||
|
import dynamic from 'next/dynamic'
|
||||||
|
import { useRouter } from 'next/router'
|
||||||
|
|
||||||
export default function useLazyLoadHighlightJS(path: string) {
|
const ClientSideHighlightJS = dynamic(() => import('./ClientSideHighlightJS'), {
|
||||||
|
ssr: false,
|
||||||
|
})
|
||||||
|
|
||||||
|
export function ClientSideHighlight() {
|
||||||
|
const { asPath } = useRouter()
|
||||||
// If the page contains `[data-highlight]` blocks, these pages need
|
// If the page contains `[data-highlight]` blocks, these pages need
|
||||||
// syntax highlighting. But not every page needs i t, so it's conditionally
|
// syntax highlighting. But not every page needs i t, so it's conditionally
|
||||||
// lazy-loaded on the client.
|
// lazy-loaded on the client.
|
||||||
const [lazyLoadHighlightJS, setLazyLoadHighlightJS] = useState(false)
|
const [load, setLoad] = useState(false)
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// It doesn't need to use querySelector because all we care about is if
|
// It doesn't need to use querySelector because all we care about is if
|
||||||
// there is greater than zero of these in the DOM.
|
// there is greater than zero of these in the DOM.
|
||||||
// Note! This "core selector", which determines whether to bother
|
// Note! This "core selector", which determines whether to bother
|
||||||
// or not, needs to match what's used inside ClientSideHighlightJS.tsx
|
// or not, needs to match what's used inside ClientSideHighlightJS.tsx
|
||||||
if (document.querySelector('[data-highlight]')) {
|
if (!load && document.querySelector('[data-highlight]')) {
|
||||||
setLazyLoadHighlightJS(true)
|
setLoad(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Important to depend on the current path because the first page you
|
// Important to depend on the current path because the first page you
|
||||||
// load, before any client-side navigation, might not need it, but the
|
// load, before any client-side navigation, might not need it, but the
|
||||||
// consecutive one does.
|
// consecutive one does.
|
||||||
}, [path])
|
}, [asPath])
|
||||||
|
|
||||||
return lazyLoadHighlightJS
|
if (load) return <ClientSideHighlightJS />
|
||||||
|
return null
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,8 @@
|
|||||||
import { useRouter } from 'next/router'
|
import { useRouter } from 'next/router'
|
||||||
import dynamic from 'next/dynamic'
|
|
||||||
|
|
||||||
import { ZapIcon, InfoIcon, ShieldLockIcon } from '@primer/octicons-react'
|
import { ZapIcon, InfoIcon, ShieldLockIcon } from '@primer/octicons-react'
|
||||||
import { Callout } from 'components/ui/Callout'
|
import { Callout } from 'components/ui/Callout'
|
||||||
|
|
||||||
import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS'
|
|
||||||
import { Link } from 'components/Link'
|
import { Link } from 'components/Link'
|
||||||
import { DefaultLayout } from 'components/DefaultLayout'
|
import { DefaultLayout } from 'components/DefaultLayout'
|
||||||
import { ArticleTitle } from 'components/article/ArticleTitle'
|
import { ArticleTitle } from 'components/article/ArticleTitle'
|
||||||
@@ -17,8 +15,7 @@ import { ArticleGridLayout } from './ArticleGridLayout'
|
|||||||
import { PlatformPicker } from 'components/article/PlatformPicker'
|
import { PlatformPicker } from 'components/article/PlatformPicker'
|
||||||
import { ToolPicker } from 'components/article/ToolPicker'
|
import { ToolPicker } from 'components/article/ToolPicker'
|
||||||
import { MiniTocs } from 'components/ui/MiniTocs'
|
import { MiniTocs } from 'components/ui/MiniTocs'
|
||||||
|
import { ClientSideHighlight } from 'components/ClientSideHighlight'
|
||||||
const ClientSideHighlightJS = dynamic(() => import('./ClientSideHighlightJS'), { ssr: false })
|
|
||||||
|
|
||||||
// Mapping of a "normal" article to it's interactive counterpart
|
// Mapping of a "normal" article to it's interactive counterpart
|
||||||
const interactiveAlternatives: Record<string, { href: string }> = {
|
const interactiveAlternatives: Record<string, { href: string }> = {
|
||||||
@@ -58,13 +55,9 @@ export const ArticlePage = () => {
|
|||||||
const { t } = useTranslation('pages')
|
const { t } = useTranslation('pages')
|
||||||
const currentPath = asPath.split('?')[0]
|
const currentPath = asPath.split('?')[0]
|
||||||
|
|
||||||
const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DefaultLayout>
|
<DefaultLayout>
|
||||||
{/* Doesn't matter *where* this is included because it will
|
<ClientSideHighlight />
|
||||||
never render anything. It always just return null. */}
|
|
||||||
{lazyLoadHighlightJS && <ClientSideHighlightJS />}
|
|
||||||
|
|
||||||
<div className="container-xl px-3 px-md-6 my-4">
|
<div className="container-xl px-3 px-md-6 my-4">
|
||||||
<ArticleGridLayout
|
<ArticleGridLayout
|
||||||
|
|||||||
@@ -1,7 +1,3 @@
|
|||||||
import { useRouter } from 'next/router'
|
|
||||||
import dynamic from 'next/dynamic'
|
|
||||||
|
|
||||||
import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS'
|
|
||||||
import { DefaultLayout } from 'components/DefaultLayout'
|
import { DefaultLayout } from 'components/DefaultLayout'
|
||||||
import { ArticleTitle } from 'components/article/ArticleTitle'
|
import { ArticleTitle } from 'components/article/ArticleTitle'
|
||||||
import { MarkdownContent } from 'components/ui/MarkdownContent'
|
import { MarkdownContent } from 'components/ui/MarkdownContent'
|
||||||
@@ -9,24 +5,18 @@ import { Lead } from 'components/ui/Lead'
|
|||||||
import { ArticleGridLayout } from './ArticleGridLayout'
|
import { ArticleGridLayout } from './ArticleGridLayout'
|
||||||
import { MiniTocs } from 'components/ui/MiniTocs'
|
import { MiniTocs } from 'components/ui/MiniTocs'
|
||||||
import { useAutomatedPageContext } from 'components/context/AutomatedPageContext'
|
import { useAutomatedPageContext } from 'components/context/AutomatedPageContext'
|
||||||
|
import { ClientSideHighlight } from 'components/ClientSideHighlight'
|
||||||
const ClientSideHighlightJS = dynamic(() => import('./ClientSideHighlightJS'), { ssr: false })
|
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
children: React.ReactNode
|
children: React.ReactNode
|
||||||
}
|
}
|
||||||
|
|
||||||
export const AutomatedPage = ({ children }: Props) => {
|
export const AutomatedPage = ({ children }: Props) => {
|
||||||
const { asPath } = useRouter()
|
|
||||||
const { title, intro, renderedPage, miniTocItems } = useAutomatedPageContext()
|
const { title, intro, renderedPage, miniTocItems } = useAutomatedPageContext()
|
||||||
|
|
||||||
const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DefaultLayout>
|
<DefaultLayout>
|
||||||
{/* Doesn't matter *where* this is included because it will
|
<ClientSideHighlight />
|
||||||
never render anything. It always just return null. */}
|
|
||||||
{lazyLoadHighlightJS && <ClientSideHighlightJS />}
|
|
||||||
|
|
||||||
<div className="container-xl px-3 px-md-6 my-4">
|
<div className="container-xl px-3 px-md-6 my-4">
|
||||||
<ArticleGridLayout
|
<ArticleGridLayout
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { useRouter } from 'next/router'
|
|||||||
import dynamic from 'next/dynamic'
|
import dynamic from 'next/dynamic'
|
||||||
import cx from 'classnames'
|
import cx from 'classnames'
|
||||||
|
|
||||||
import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS'
|
|
||||||
import { DefaultLayout } from 'components/DefaultLayout'
|
import { DefaultLayout } from 'components/DefaultLayout'
|
||||||
import { MarkdownContent } from 'components/ui/MarkdownContent'
|
import { MarkdownContent } from 'components/ui/MarkdownContent'
|
||||||
import { Lead } from 'components/ui/Lead'
|
import { Lead } from 'components/ui/Lead'
|
||||||
@@ -11,10 +10,7 @@ import { RestOperation } from './RestOperation'
|
|||||||
import styles from './RestOperation.module.scss'
|
import styles from './RestOperation.module.scss'
|
||||||
import { useAutomatedPageContext } from 'components/context/AutomatedPageContext'
|
import { useAutomatedPageContext } from 'components/context/AutomatedPageContext'
|
||||||
import { Operation } from './types'
|
import { Operation } from './types'
|
||||||
|
import { ClientSideHighlight } from 'components/ClientSideHighlight'
|
||||||
const ClientSideHighlightJS = dynamic(() => import('components/article/ClientSideHighlightJS'), {
|
|
||||||
ssr: false,
|
|
||||||
})
|
|
||||||
|
|
||||||
const ClientSideRedirectExceptions = dynamic(
|
const ClientSideRedirectExceptions = dynamic(
|
||||||
() => import('components/article/ClientsideRedirectExceptions'),
|
() => import('components/article/ClientsideRedirectExceptions'),
|
||||||
@@ -74,14 +70,12 @@ export const RestReferencePage = ({ restOperations }: StructuredContentT) => {
|
|||||||
})
|
})
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath)
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<DefaultLayout>
|
<DefaultLayout>
|
||||||
{/* Doesn't matter *where* this is included because it will
|
{/* Doesn't matter *where* this is included because it will
|
||||||
never render anything. It always just return null. */}
|
never render anything. It always just return null. */}
|
||||||
{loadClientsideRedirectExceptions && <ClientSideRedirectExceptions />}
|
{loadClientsideRedirectExceptions && <ClientSideRedirectExceptions />}
|
||||||
{lazyLoadHighlightJS && <ClientSideHighlightJS />}
|
<ClientSideHighlight />
|
||||||
<div
|
<div
|
||||||
className={cx(styles.restOperation, 'px-3 px-md-6 my-4 container-xl')}
|
className={cx(styles.restOperation, 'px-3 px-md-6 my-4 container-xl')}
|
||||||
data-search="article-body"
|
data-search="article-body"
|
||||||
|
|||||||
Reference in New Issue
Block a user