๐ Use DOMPurify for some context by conditional exports
npm i universal-dompurify
Conditional exports apply the appropriate DOMPurify file at each run.
import DOMPurify from 'universal-dompurify'
const cleaned = DOMPurify.sanitize(/* ... */)
condition | resolve |
---|---|
browser |
DOMPurify |
default |
DOMPurify + JSDOM |
worker |
DOMPurify + JSDOM + (require: your external node polyfills) |
Tip
It is generally not recommended to use JSDOM with the restricted Edge runtime.
See also @jill64/universal-sanitizer for a more lightweight alternative.
Reduces bundle size by omitting rendering on the server.
/** @type {DOMPurify | null} */
import DOMPurify from 'universal-dompurify/browser-only'
const cleaned = DOMPurify?.sanitize?.(/* ... */) ?? 'server-fallback-value'
condition | resolve |
---|---|
browser |
DOMPurify |
default |
null |
worker |
null |