Skip to content

Commit 25814b6

Browse files
committed
feat: update MDX components and wrapper for improved server-side rendering and dynamic route handling
fix: adjust Giscus comment script attributes for better theme synchronization and error handling chore: update primary and document site URLs in configuration
1 parent b69a7ba commit 25814b6

6 files changed

Lines changed: 45 additions & 50 deletions

File tree

mdx-components.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
import * as mdxComponents from '@/components/mdx'
1+
import * as mdxClientComponents from '@/components/mdx'
2+
import { wrapper as ServerWrapper } from '@/components/mdx-wrapper'
23
import { type MDXComponents } from 'mdx/types'
34

45
export function useMDXComponents(components: MDXComponents) {
56
return {
67
...components,
7-
...mdxComponents,
8+
...mdxClientComponents,
9+
// Override wrapper with server component to avoid passing dynamic route props
10+
wrapper: ServerWrapper,
811
}
912
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"packageManager": "yarn@1.22.22",
44
"version": "0.1.0",
55
"private": true,
6+
"engines": {
7+
"node": ">=18.18 <25"
8+
},
69
"scripts": {
710
"dev": "next dev -p 32235",
811
"tsc": "tsc --noEmit",

src/app/(cn)/(studio)/layout.tsx

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -91,15 +91,4 @@ export default async function RootLayout({
9191
</Layout>
9292
</div>
9393
)
94-
}
95-
96-
/**
97-
* 正则表达式解释:
98-
*
99-
* - `(` 和 `)`:这是一个捕获组的开始和结束。捕获组是一种用于将正则表达式的一部分括起来,以便对该部分进行操作(例如,使用 `|` 操作符实现“或”逻辑)的方式。在这个例子中,捕获组包含了两种可能的匹配:字符串开头 `^` 或 `/` 字符。
100-
* - `^`:这个符号表示字符串的开头。当它出现在正则表达式的开始位置时,它要求匹配只能发生在字符串的开头。在这个例子中,它表示我们希望在字符串开头处找到 `page.mdx`。
101-
* - `|`:这个符号表示“或”操作,在正则表达式中,它表示我们希望匹配两个或多个可能的选项中的任意一个。在这个例子中,我们希望匹配字符串开头 `^` 或 `/` 字符。
102-
* - `\/`:这里的 `/` 字符表示字面上的斜线字符。由于斜线 `/` 通常用于表示正则表达式的边界,因此在这里需要使用反斜线 `\` 对其进行转义,以表示我们实际上想要匹配的是斜线字符本身。
103-
*
104-
* 总结一下,`(^|\/)` 这个捕获组的意义是:我们希望建立一个捕获组,以便在字符串开头或斜线字符之后找到 `page.mdx`。这样,当我们使用 `replace` 函数时,我们可以删除匹配到的部分(即 `page.mdx` 以及可能存在的前导斜线),从而将文件名转换为对应的路由路径。
105-
*/
94+
}

src/components/GiscusComment.tsx

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,22 @@ export default function GiscusComment() {
3838
script.setAttribute('data-repo-id', 'R_kgDOOI9EFg')
3939
script.setAttribute('data-category', 'Announcements')
4040
script.setAttribute('data-category-id', 'DIC_kwDOOI9EFs4Cr9K0')
41-
script.setAttribute('data-mapping', 'specific')
42-
script.setAttribute('data-term', pathname)
43-
script.setAttribute('data-strict', '1')
41+
script.setAttribute('data-mapping', 'pathname')
42+
script.setAttribute('data-strict', '0')
4443
script.setAttribute('data-reactions-enabled', '1')
4544
script.setAttribute('data-emit-metadata', '0')
4645
script.setAttribute('data-input-position', 'top')
47-
script.setAttribute(
48-
'data-theme',
49-
resolvedTheme === 'dark' ? 'transparent_dark' : 'light',
50-
)
46+
script.setAttribute('data-theme', 'preferred_color_scheme')
5147
script.setAttribute('data-loading', 'lazy')
52-
script.setAttribute('data-lang', 'zh-CN')
48+
script.setAttribute('data-lang', 'en')
5349
script.crossOrigin = 'anonymous'
5450
script.async = true
5551

52+
// 错误处理
53+
script.onerror = () => {
54+
console.error('Failed to load Giscus script')
55+
}
56+
5657
// 找到Giscus容器并添加脚本
5758
const giscusContainer = document.querySelector('.giscus')
5859
if (giscusContainer) {
@@ -75,46 +76,29 @@ export default function GiscusComment() {
7576
} // 依赖 resolvedTheme 是正确的
7677
}, [pathname, resolvedTheme]) // eslint-disable-line react-hooks/exhaustive-deps
7778

78-
// 监听主题变化,实时更新Giscus主题
79+
// 使用 preferred_color_scheme 后,Giscus 会自动根据系统主题切换
80+
// 如果需要手动同步主题,可以保留以下代码
7981
useEffect(() => {
8082
if (!isGiscusLoaded) return
8183

8284
const updateGiscusTheme = () => {
8385
const iframe = document.querySelector(
8486
'iframe.giscus-frame',
8587
) as HTMLIFrameElement
86-
if (iframe) {
87-
const theme = resolvedTheme === 'dark' ? 'transparent_dark' : 'light'
88-
iframe.contentWindow?.postMessage(
89-
{ giscus: { setConfig: { theme } } },
88+
if (iframe && iframe.contentWindow) {
89+
// 使用 preferred_color_scheme 让 giscus 自动跟随系统主题
90+
iframe.contentWindow.postMessage(
91+
{ giscus: { setConfig: { theme: 'preferred_color_scheme' } } },
9092
'https://giscus.app',
9193
)
9294
}
9395
}
9496

95-
// 监听Giscus消息
96-
const handleMessage = (event: MessageEvent) => {
97-
if (
98-
event.origin === 'https://giscus.app' &&
99-
event.data?.giscus?.discussion
100-
) {
101-
updateGiscusTheme()
102-
}
103-
}
104-
105-
window.addEventListener('message', handleMessage)
106-
107-
// 轮询尝试更新主题,确保iframe加载后应用正确主题
108-
let attempts = 0
109-
const interval = setInterval(() => {
110-
updateGiscusTheme()
111-
attempts++
112-
if (attempts >= 10) clearInterval(interval)
113-
}, 500)
97+
// 给 iframe 一些时间加载
98+
const timer = setTimeout(updateGiscusTheme, 1000)
11499

115100
return () => {
116-
window.removeEventListener('message', handleMessage)
117-
clearInterval(interval)
101+
clearTimeout(timer)
118102
}
119103
}, [resolvedTheme, isGiscusLoaded])
120104

src/components/mdx-wrapper.tsx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { Feedback } from '@/components/Feedback'
2+
import { Prose } from '@/components/Prose'
3+
4+
// Server Component wrapper for MDX pages to avoid passing dynamic route props
5+
// like `searchParams` across a client boundary, which triggers Next.js 15
6+
// sync dynamic API errors when MDX spreads page props by default.
7+
export function wrapper({ children }: { children: React.ReactNode }) {
8+
return (
9+
<article className="flex h-full flex-col pb-10 pt-16">
10+
<Prose className="flex-auto">{children}</Prose>
11+
<footer className="mx-auto mt-16 w-full max-w-2xl lg:max-w-5xl">
12+
<Feedback />
13+
</footer>
14+
</article>
15+
)
16+
}

src/config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export const MEDIA_URL = ''
1515
export const WS_URL = process.env.NEXT_PUBLIC_WS_URL
1616

1717
export const PrimarySite =
18-
process.env.NEXT_PUBLIC_PRIMARY_SITE || 'https://cloudocs.netlify.app/'
18+
process.env.NEXT_PUBLIC_PRIMARY_SITE || 'https://docs.sciol.ac.cn/'
1919

2020
export const DocumentSite =
21-
process.env.NEXT_PUBLIC_DOCUMENT_SITE || 'https://cloudocs.netlify.app/'
21+
process.env.NEXT_PUBLIC_DOCUMENT_SITE || 'https://docs.sciol.ac.cn/'
2222

2323
export const WorkflowSite = process.env.NEXT_PUBLIC_WORKFLOW_SITE || ''

0 commit comments

Comments
 (0)