Skip to content
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

Build error when using isomorphic-dompurify in angular 15 universal #203

Open
praveshShetty opened this issue Jul 1, 2023 · 8 comments
Open
Labels
bug Something isn't working dependencies Pull requests that update a dependency file

Comments

@praveshShetty
Copy link

praveshShetty commented Jul 1, 2023

I'm trying to use isomorphic dompurify's sanitize function, in my angular application, which uses angular universal for server side rendering. While there is no error when running the application as a client side rendered one. server side rendering is not working.

Screenshot 2023-07-01 at 2 53 51 PM

I'm importing the sanitize function using ES6 syntax:

import { sanitize } from 'isomorphic-dompurify';

Is this a known issue.

I'm currently using

"angular" : 15.2.2
"typescript": "4.9.4"
"node" : 16.19.1
"isomorphic-dompurify": "^1.7.0"

@kkomelin
Copy link
Owner

kkomelin commented Jul 1, 2023

Hi @praveshShetty,

Thanks for reporting the issue.

Unfortunately, the log doesn't say a lot. Could you please provide a sandbox project where I could reproduce the issue?

@kkomelin
Copy link
Owner

kkomelin commented Jul 1, 2023

What is your hosting environment?

@JeremyGuinn
Copy link

JeremyGuinn commented Jul 25, 2023

@kkomelin, I'm also seeing this error, but I have a bit more info relating to it.

import DOMPurify from 'isomorphic-dompurify';
./node_modules/jsdom/lib/jsdom/living/generated/utils.js:34
const AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}).prototype);
                                      ^


TypeError: Cannot convert undefined or null to object
    at Function.getPrototypeOf (<anonymous>)
    at Object.39178 (./node_modules/jsdom/lib/jsdom/living/generated/utils.js:34:39)
    at __webpack_require__ (./webpack/bootstrap:19:1)
    at Object.96057 (./node_modules/jsdom/lib/api.js:11:18)
    at __webpack_require__ (./webpack/bootstrap:19:1)
    at <anonymous> (./node_modules/isomorphic-dompurify/index.js:1:196)
    at Object.64682 (./node_modules/isomorphic-dompurify/index.js:1:262)
    at __webpack_require__ (./webpack/bootstrap:19:1)
    at Module.682 (./src/app/core/services/footer.service.ts:7:21)
    at __webpack_require__ (./webpack/bootstrap:19:1)

Node.js v18.12.1

I'll update this comment with a minimal reproduction.

@kkomelin
Copy link
Owner

kkomelin commented Jul 25, 2023

Thanks @JeremyGuinn for the information.
Please also take a look at the other jsdom related issue #54 , if it has a workaround which can help you.

@JeremyGuinn
Copy link

Here is a codesandbox that gets the same error when running dev:ssr

@kkomelin
Copy link
Owner

Thanks @JeremyGuinn,
From what I can see, the reason is the same as here #54:

Warning: Module not found: Error: Can't resolve 'canvas' in '/workspace/node_modules/isomorphic-dompurify/node_modules/jsdom/lib/jsdom'

@JeremyGuinn
Copy link

JeremyGuinn commented Jul 25, 2023

Yeah, I'm able to resolve this by using @angular-builders/custom-webpack, and using the same solution from #54 by adding the externals so webpack doesn't try to bundle them in the browser code. I've updated the codesandbox with this fix.

angular.json

{
  ...
  "architect": {
    "build": {
      "builder": "@angular-builders/custom-webpack:browser",
      "options": {
        "customWebpackConfig": {
          "path": "./webpack.config.js"
        },
        ...
      },
      ...
    },
    ...
    "server": {
      "builder": "@angular-builders/custom-webpack:server",
      "options": {
        "customWebpackConfig": {
          "path": "./webpack.config.js"
        },
        ...
      },
      ...
    },
    ...
  },
  ...
}

webpack.config.js

module.exports = function(config) {
  return {
    ...config,
    externals: {
      ...config.externals,
      canvas: 'commonjs canvas',
      jsdom: 'commonjs jsdom'
    }
  }
}

@kkomelin
Copy link
Owner

That's great news @JeremyGuinn. Thanks for sharing the working solution!

My guess is you only need the proposed customization of the Webpack config for the server side (@angular-builders/custom-webpack:server). But I didn't try it myself.

@kkomelin kkomelin added bug Something isn't working dependencies Pull requests that update a dependency file and removed needs more info labels Jul 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working dependencies Pull requests that update a dependency file
Projects
None yet
Development

No branches or pull requests

3 participants