-
-
Notifications
You must be signed in to change notification settings - Fork 948
fix: add SSR support for DOMPurify and fix streaming initial output #1530
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,11 +52,13 @@ | |
| "clsx": "^2.1.1", | ||
| "dompurify": "^3.2.6", | ||
| "html-react-parser": "^5.2.5", | ||
| "jsdom": "^26.1.0", | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 浏览器场景不应该依赖 jsdom |
||
| "katex": "^0.16.22", | ||
| "marked": "^15.0.12" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/dompurify": "^3.0.5", | ||
| "@types/jsdom": "^27.0.0", | ||
| "@types/lodash.throttle": "^4.1.9", | ||
| "@types/react": "^19.0.2", | ||
| "@types/react-dom": "^19.0.2", | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,43 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import DOMPurify from 'dompurify'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| let serverDOMPurify: ReturnType<typeof DOMPurify> | null = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| let jsdomInstance: any = null; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| function initializeServerDOMPurify(): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const jsdomModule = require('jsdom'); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const JSDOM = jsdomModule.JSDOM || jsdomModule.default?.JSDOM || jsdomModule; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!JSDOM) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| jsdomInstance = new JSDOM(''); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| serverDOMPurify = DOMPurify(jsdomInstance.window); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return true; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof window === 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| initializeServerDOMPurify(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns the DOMPurify instance, compatible with both server-side (Node.js) and client-side (browser) environments. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * On the server, it creates a DOMPurify instance with a jsdom window; on the client, it returns the browser's DOMPurify. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * @see https://github.com/cure53/DOMPurify?tab=readme-ov-file#running-dompurify-on-the-server | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| export function getDOMPurify(): ReturnType<typeof DOMPurify> { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof window === 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!serverDOMPurify) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| initializeServerDOMPurify(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return serverDOMPurify || DOMPurify; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return DOMPurify; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+34
to
+43
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 服务端初始化失败时的回退逻辑存在问题。 第 39 行的回退逻辑
建议改进回退策略: export function getDOMPurify(): ReturnType<typeof DOMPurify> {
if (typeof window === 'undefined') {
if (!serverDOMPurify) {
initializeServerDOMPurify();
}
- return serverDOMPurify || DOMPurify;
+ if (!serverDOMPurify) {
+ throw new Error(
+ 'Failed to initialize DOMPurify for SSR. Please ensure jsdom is installed.'
+ );
+ }
+ return serverDOMPurify;
}
return DOMPurify;
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export { getDOMPurify } from './getDOMPurify'; |
Uh oh!
There was an error while loading. Please reload this page.