From ebd0e2af7a9c6e69224ab02768941a1bdc22697b Mon Sep 17 00:00:00 2001 From: 7w1 Date: Sat, 11 Jul 2026 18:50:58 -0500 Subject: [PATCH] Optimize DOMPurify instantiation in sanitize --- src/app/utils/sanitize.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/app/utils/sanitize.ts b/src/app/utils/sanitize.ts index 7cdecda38..4cb53a47d 100644 --- a/src/app/utils/sanitize.ts +++ b/src/app/utils/sanitize.ts @@ -283,13 +283,22 @@ const pruneInvalidEmptyElements = ( }); }; +let purifyInstance: DOMPurify.DOMPurifyI | undefined; +function getPurify(): DOMPurify.DOMPurifyI { + if (!purifyInstance) { + purifyInstance = DOMPurify(window); + } + return purifyInstance; +} + export const sanitizeCustomHtml = (customHtml: string): string => { if (typeof window === 'undefined') { return sanitizeText(customHtml); } const { protectedHtml, protectedSources } = protectImageSources(customHtml); - const purify = DOMPurify(window); + const purify = getPurify(); + purify.removeAllHooks(); const allowedHtmlAttributes = [...permittedHtmlAttributes, INTERNAL_IMG_SRC_ATTR]; purify.addHook('uponSanitizeAttribute', (currentNode, hookEvent) => {