-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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 1 commit
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 | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,18 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import DOMPurify from 'dompurify'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| import { JSDOM } from 'jsdom'; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| * 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() { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
coderabbitai[bot] marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (typeof window === 'undefined') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| const jsWindow = new JSDOM('').window; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| return DOMPurify(jsWindow); | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
| return DOMPurify; | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+1
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. Creating a new import DOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
let serverDOMPurify: ReturnType<typeof DOMPurify>;
/**
* 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() {
if (typeof window === 'undefined') {
if (!serverDOMPurify) {
const jsWindow = new JSDOM('').window;
serverDOMPurify = DOMPurify(jsWindow as any);
}
return serverDOMPurify;
}
return DOMPurify;
}
Author
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. Will this cause a memory leak for the serverDOMPurify variable?
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.