1- import { lazy , Suspense , type ComponentPropsWithoutRef , useEffect , useSyncExternalStore } from "react"
1+ import { type ComponentPropsWithoutRef , useEffect } from "react"
22import { useParams , Link } from "react-router-dom"
33import ReactMarkdown from "react-markdown"
44import remarkGfm from "remark-gfm"
@@ -50,10 +50,10 @@ import { StructuredData } from "@/components/StructuredData"
5050import { postStructuredData } from "@/lib/structured-data"
5151import { MarkdownChart } from "@/components/MarkdownChart"
5252import { readMarkdownCode } from "@/lib/markdown-code"
53+ import { createRetryableLoader } from "@/lib/async-loader"
54+ import { useDeferredModule } from "@/hooks/useDeferredModule"
5355
54- const LazyCodeBlock = lazy ( ( ) =>
55- import ( "@/components/CodeBlock" ) . then ( ( module ) => ( { default : module . CodeBlock } ) )
56- )
56+ const loadCodeBlock = createRetryableLoader ( ( ) => import ( "@/components/CodeBlock" ) )
5757
5858function localizeMarkdownLink ( href : string , language : Language ) {
5959 if ( / ^ \/ ( p o s t s | t a g s | s e r i e s | s e a r c h | a n a l y t i c s | a b o u t ) ( \/ | [ ? # ] | $ ) / . test ( href ) ) {
@@ -62,19 +62,6 @@ function localizeMarkdownLink(href: string, language: Language) {
6262 return href
6363}
6464
65- function subscribeHydration ( callback : ( ) => void ) {
66- const timer = window . setTimeout ( callback , 0 )
67- return ( ) => window . clearTimeout ( timer )
68- }
69-
70- function getClientSnapshot ( ) {
71- return true
72- }
73-
74- function getServerSnapshot ( ) {
75- return false
76- }
77-
7865function CodeBlockFallback ( { children, ...props } : ComponentPropsWithoutRef < "pre" > ) {
7966 return (
8067 < pre
@@ -93,16 +80,17 @@ function MarkdownCodeBlock(props: ComponentPropsWithoutRef<"pre">) {
9380}
9481
9582function HydratedCodeBlock ( props : ComponentPropsWithoutRef < "pre" > ) {
96- const isClient = useSyncExternalStore ( subscribeHydration , getClientSnapshot , getServerSnapshot )
97-
98- if ( ! isClient ) {
99- return < CodeBlockFallback { ...props } />
100- }
101-
83+ const { value : module , error, retry } = useDeferredModule ( loadCodeBlock )
84+ const { t } = useLanguage ( )
85+ if ( module ) return < module . CodeBlock { ...props } />
10286 return (
103- < Suspense fallback = { < CodeBlockFallback { ...props } /> } >
104- < LazyCodeBlock { ...props } />
105- </ Suspense >
87+ < >
88+ { error && < div className = "not-prose flex flex-wrap items-center gap-3 text-sm text-muted-foreground" role = "alert" >
89+ < p > { t . components . codeLoadError } </ p >
90+ < Button variant = "outline" size = "sm" onClick = { retry } > { t . common . retry } </ Button >
91+ </ div > }
92+ < CodeBlockFallback { ...props } />
93+ </ >
10694 )
10795}
10896
0 commit comments