Isomorphic DOMPurify Wrapper
isomorphic-dompurify provides a universal wrapper for the DOMPurify library, enabling its seamless use across both client-side browser environments and server-side Node.js applications. Its primary function is to abstract away the environment-specific initialization details required by DOMPurify, which necessitates a DOM tree to operate. On the server, it leverages `jsdom` to create a fake DOM environment, making the API identical to client-side usage. The package is currently at version 3.9.0 and experiences a regular release cadence, primarily driven by updates to its core dependency, DOMPurify, and other development dependencies. A key differentiator is its automatic handling of `jsdom` setup and teardown for server-side XSS sanitization, along with utilities for memory management in long-running Node.js processes. This library is crucial for applications built with frameworks like Next.js that require consistent sanitization logic irrespective of the rendering environment.
Common errors
-
TS7016: Could not find a declaration file for module 'isomorphic-dompurify'
cause Missing type declaration files for browser exports in version 3.7.0.fixUpgrade `isomorphic-dompurify` to version `3.7.1` or higher. If the issue persists with later versions, ensure your `tsconfig.json` module resolution settings are appropriate for your environment (e.g., `"moduleResolution": "bundler"` or `"node16"`).
Warnings
- breaking Minimum Node.js engine requirements have been updated across major versions. Ensure your environment meets the specified versions to avoid runtime issues.
- gotcha The underlying DOMPurify library does not strictly follow Semantic Versioning, which means `isomorphic-dompurify` must release all DOMPurify updates as minor versions. This means minor version updates of `isomorphic-dompurify` could potentially contain breaking changes from DOMPurify itself.
- gotcha In long-running Node.js processes, the internal `jsdom` window used for server-side sanitization accumulates DOM state, leading to progressive memory growth and potential slowdowns if `clearWindow()` is not periodically called.
- breaking Version 3.7.0 was published without `browser.d.ts` and `browser.d.mts` type declarations due to a build issue, causing TypeScript errors (e.g., `TS7016`) when resolving browser-specific exports.
Install
-
npm install isomorphic-dompurify -
yarn add isomorphic-dompurify -
pnpm add isomorphic-dompurify
Imports
- DOMPurify
const DOMPurify = require('isomorphic-dompurify');import DOMPurify from 'isomorphic-dompurify';
- sanitize
const { sanitize } = require('isomorphic-dompurify');import { sanitize } from 'isomorphic-dompurify'; - clearWindow
const { clearWindow } = require('isomorphic-dompurify');import { clearWindow } from 'isomorphic-dompurify';
Quickstart
import DOMPurify, { sanitize, clearWindow } from "isomorphic-dompurify";
// 1. Basic sanitization: Works identically on client and server.
const dirtyHtml = `
<img src="x" onerror="alert('XSS!')">
<a href="javascript:alert('Evil!')">Click me</a>
<p>Hello, <script>alert('world');</script> user!</p>
`;
const cleanHtml = sanitize(dirtyHtml, { USE_PROFILES: { html: true } });
console.log('Sanitized HTML:', cleanHtml);
// Expected: <img src="x"><a href="">Click me</a><p>Hello, user!</p>
// 2. Using DOMPurify directly for configuration or hooks.
// On the client, this uses the browser's global window.
// On the server, it uses an internally managed JSDOM window.
DOMPurify.setConfig({ ALLOW_DATA_ATTR: false });
const cleanHtmlWithConfig = DOMPurify.sanitize('<div data-test="1">Test</div>');
console.log('Sanitized with config (no data attr):', cleanHtmlWithConfig);
// Expected: <div>Test</div>
// 3. Server-side memory management: Crucial for long-running Node.js processes.
// Calling clearWindow() releases JSDOM resources and creates a fresh internal window.
if (typeof window === 'undefined') { // Check if in Node.js environment
clearWindow();
console.log('Server-side JSDOM instance cleared for memory management.');
// After clearing, DOMPurify and sanitize continue to work with a fresh instance.
const reSanitized = sanitize('<span>Clean again.</span>');
console.log('Re-sanitized after clearWindow:', reSanitized);
}