Uncaught (in promise) Event
when rendering SVG node in React Vite SPA, only on Chrome
#201
Open
1 of 3 tasks
First, I want to make clear:
dom-to-image
packageBut I wanted to document it here in case anyone else ran into it, and in case the maintainers have any insights. Feel free to close.
Take a look at the
test.zip
example below. It has a simple SVG in the static HTML, and then the same SVG rendered through JSX in a Vite React SPA. When you try to runtoPng
on the first SVG, it works fine. When you try to run it on the second SVG, you get an uncaught error. And this doesn't happen in Firefox or Safari, just Chrome (131.0.6778.86
).I've traced the error to somewhere in the
makeSvgDataUri
function. If you get rid of thexhtml
xmlns
andforeignObject
stuff -- i.e. justdata:image/svg+xml;charset=utf-8,${new XMLSerializer().serializeToString(svg)}
-- the error goes away. (I'm not sure why the original package does this crazy multi-namespace wrapper stuff, seems like it's bound to cause more trouble than whatever edge case it was meant to support).Another way to fix it is to wrap the problematic SVG in a div, and run
toPng
on that instead.This is one of the most specific and weird bugs I've seen in my career. I'll update this issue if I'm able to figure out exactly what the Chromium/React bug is. Since the static HTML SVG works fine, I'm thinking it must have something to do with invisible properties (as opposed to static attributes) React is setting on the DOM element... Comparing the static HTML and
getComputedStyles
on both SVGs, they are both exactly the same.Minimum reproducible example
test.zip
Run
npm install
andnpm run dev
.Library version
3.5.0
Browsers
The text was updated successfully, but these errors were encountered: