remix-run / remix

Build Better Websites. Create modern, resilient user experiences with web fundamentals.
https://remix.run
MIT License
30.02k stars 2.53k forks source link

Isomorphic-dompurify not working with Remix #8628

Open joaoguidev opened 9 months ago

joaoguidev commented 9 months ago

Reproduction

https://stackblitz.com/~/github.com/joaoguidev/RemixDompurify

System Info

System:
    OS: Windows 11 10.0.22621
    CPU: (20) x64 12th Gen Intel(R) Core(TM) i9-12900H
    Memory: 4.35 GB / 15.67 GB
  Binaries:
    Node: 18.18.0 - C:\Program Files\nodejs\node.EXE
    npm: 9.8.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Chromium (121.0.2277.83)
    Internet Explorer: 11.0.22621.1
  npmPackages:
    @remix-run/cloudflare: ^2.5.1 => 2.5.1
    @remix-run/cloudflare-pages: ^2.5.1 => 2.5.1
    @remix-run/css-bundle: ^2.5.1 => 2.5.1
    @remix-run/dev: ^2.5.1 => 2.5.1
    @remix-run/react: ^2.5.1 => 2.5.1

Used Package Manager

npm

Expected Behavior

Not get the error below when using Isomorphic-dompurify : service core:user:worker: Uncaught ReferenceError: window is not defined at c77tsl6ez28.js:29540:22 in node_modules/isomorphic-dompurify/browser.js at c77tsl6ez28.js:82:50 at c77tsl6ez28.js:30604:43

Actual Behavior

Isomorphic-dompurify does not work with Remix because it seems Remix is trying to use the browser entry point from package.json on server. This problem is listed as a known issue on the Isomorphic-dompurify repository and refer to the issue #214

sergiodxa commented 9 months ago

The error means the library is trying to use the browser version, probably because Cloudflare picks the browser one instead of the Node one, and since it's not Node this is correct.

If you check their browser build, they just require this other package https://www.npmjs.com/package/dompurify and try to find them from window first. They could just stop trying to find it globally in window and instead just use ESM, but if they don't want to fix this on their side you could skip them and import dompurify yourself.