Vannsl / vue-3-sanitize

HTML sanitizer for Vue.js 3 apps
MIT License
21 stars 3 forks source link

[Nuxt3] unable to run on client #7

Open madebyfabian opened 1 year ago

madebyfabian commented 1 year ago

Hi there, thank you for providing this plugin. Though it seems that the underlying sanitize-html is generally not made for client-code. I get these errors while trying to use the plugin within nuxt3:

Bildschirm­foto 2022-12-14 um 08 15 26

Did everyone experience this? I am running this code on the client-side of nuxt, since I have ssr off for this route.

andrzejkupczyk commented 1 year ago

@madebyfabian yes I have this problem too. I think the cause is the htmlparser2 package because I've already tried several other packages that use htmlparser2 and they all fail. The vue-sanitize-directive works for me.

madebyfabian commented 1 year ago

@andrzejkupczyk The only thing I was getting to work was https://github.com/kkomelin/isomorphic-dompurify and without any vue/nuxt wrappers, just installed directly. Works fine on both client and server-side.

Here is an example to get it up and running as a directive:

import DOMPurify from 'isomorphic-dompurify'

export default defineNuxtPlugin(nuxtApp => {
    nuxtApp.vueApp.directive('sanitize-html', {
        mounted(el, binding) {
            try {
                el.innerHTML = binding.value ? DOMPurify.sanitize(binding.value) : ''
            } catch (error) {}
        },
    })
})
andrzejkupczyk commented 1 year ago

Even better, thanks!