diff --git a/components/hooks/useLazyLoadHighlightJS.ts b/components/ClientSideHighlight.tsx similarity index 61% rename from components/hooks/useLazyLoadHighlightJS.ts rename to components/ClientSideHighlight.tsx index 771568f248..693d4d5422 100644 --- a/components/hooks/useLazyLoadHighlightJS.ts +++ b/components/ClientSideHighlight.tsx @@ -1,23 +1,31 @@ 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 // syntax highlighting. But not every page needs i t, so it's conditionally // lazy-loaded on the client. - const [lazyLoadHighlightJS, setLazyLoadHighlightJS] = useState(false) + const [load, setLoad] = useState(false) useEffect(() => { // It doesn't need to use querySelector because all we care about is if // there is greater than zero of these in the DOM. // Note! This "core selector", which determines whether to bother // or not, needs to match what's used inside ClientSideHighlightJS.tsx - if (document.querySelector('[data-highlight]')) { - setLazyLoadHighlightJS(true) + if (!load && document.querySelector('[data-highlight]')) { + setLoad(true) } // Important to depend on the current path because the first page you // load, before any client-side navigation, might not need it, but the // consecutive one does. - }, [path]) + }, [asPath]) - return lazyLoadHighlightJS + if (load) return + return null } diff --git a/components/article/ClientSideHighlightJS.tsx b/components/ClientSideHighlightJS.tsx similarity index 100% rename from components/article/ClientSideHighlightJS.tsx rename to components/ClientSideHighlightJS.tsx diff --git a/components/article/ArticlePage.tsx b/components/article/ArticlePage.tsx index 3d771df30d..608f017780 100644 --- a/components/article/ArticlePage.tsx +++ b/components/article/ArticlePage.tsx @@ -1,10 +1,8 @@ import { useRouter } from 'next/router' -import dynamic from 'next/dynamic' import { ZapIcon, InfoIcon, ShieldLockIcon } from '@primer/octicons-react' import { Callout } from 'components/ui/Callout' -import useLazyLoadHighlightJS from 'components/hooks/useLazyLoadHighlightJS' import { Link } from 'components/Link' import { DefaultLayout } from 'components/DefaultLayout' import { ArticleTitle } from 'components/article/ArticleTitle' @@ -17,8 +15,7 @@ import { ArticleGridLayout } from './ArticleGridLayout' import { PlatformPicker } from 'components/article/PlatformPicker' import { ToolPicker } from 'components/article/ToolPicker' import { MiniTocs } from 'components/ui/MiniTocs' - -const ClientSideHighlightJS = dynamic(() => import('./ClientSideHighlightJS'), { ssr: false }) +import { ClientSideHighlight } from 'components/ClientSideHighlight' // Mapping of a "normal" article to it's interactive counterpart const interactiveAlternatives: Record = { @@ -58,13 +55,9 @@ export const ArticlePage = () => { const { t } = useTranslation('pages') const currentPath = asPath.split('?')[0] - const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath) - return ( - {/* Doesn't matter *where* this is included because it will - never render anything. It always just return null. */} - {lazyLoadHighlightJS && } +
import('./ClientSideHighlightJS'), { ssr: false }) +import { ClientSideHighlight } from 'components/ClientSideHighlight' type Props = { children: React.ReactNode } export const AutomatedPage = ({ children }: Props) => { - const { asPath } = useRouter() const { title, intro, renderedPage, miniTocItems } = useAutomatedPageContext() - const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath) - return ( - {/* Doesn't matter *where* this is included because it will - never render anything. It always just return null. */} - {lazyLoadHighlightJS && } +
import('components/article/ClientSideHighlightJS'), { - ssr: false, -}) +import { ClientSideHighlight } from 'components/ClientSideHighlight' const ClientSideRedirectExceptions = dynamic( () => import('components/article/ClientsideRedirectExceptions'), @@ -74,14 +70,12 @@ export const RestReferencePage = ({ restOperations }: StructuredContentT) => { }) }, []) - const lazyLoadHighlightJS = useLazyLoadHighlightJS(asPath) - return ( {/* Doesn't matter *where* this is included because it will never render anything. It always just return null. */} {loadClientsideRedirectExceptions && } - {lazyLoadHighlightJS && } +