Backtestsvuerun-20261009-161443#15081
SSR hydration warning: dynamic props on SVG elements are patched as properties instead of attributes
Our patch was judged better than the merged fix.
In this judgment, Patch B = our agent, Patch A = the merged fix.
Both patches fix the root cause the same way. In hydrateElement, they work out the element's own namespace from el.namespaceURI ('svg', 'mathml' or undefined) and pass it to patchProp instead of undefined. runtime-dom's shouldSetAsProp then sends SVG props to setAttribute. Both use the element's own namespace rather than getContainerType, so <foreignObject> is correctly treated as SVG. On correctness they are essentially equal. B also passes the namespace in the onClick fast path, which is harmless and consistent. B also null-checks namespaceURI, which avoids A's non-null assertion. The real difference is the tests. A's first test hydrates a plain <svg> in jsdom without making width, height or viewBox read-only. jsdom's SVGSVGElement does not expose these as settable properties, so `key in el` is likely false and setAttribute is used either way. That test would probably pass even without the fix. Only A's foreignObject test, which defines a getter, would actually fail before the fix. B adds a helper that makes the relevant SVG properties getter-only, which mimics how browsers behave. It then tests <svg> and a nested <rect> in dev mode, asserting no 'Failed setting prop' warning. It also covers production mode, <foreignObject> with an HTML child that is still patched as a property, and teleported SVG content. Each of these would fail before the fix and pass after it, and they directly reproduce the issue's symptom. The shared helper (getElementNamespace) is a clean, documented addition next to the existing namespace helpers. B wins on test quality and slightly on craft.
The judge saw only the two patches, labelled A and B in random order, with issue links and version numbers removed. The reasoning above is shown verbatim.
Solve
6m 28s
Wall-clock time in the container
Full test suite
No new failures
1,731 passed · 1 skipped · 1,734 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
158vs 67 merged
Changed lines, ours vs merged
The issue
View on GitHubVersion
3.5.39
Reproduction link
Steps to reproduce
- Open the reproduction.
- Open the browser devtools Console.
- Observe the
[Vue warn]: Failed setting prop …error logged during hydration.
What is expected?
During hydration, dynamic props on SVG (and MathML) elements width, height, viewBox, etc. should be patched as attributes (via setAttribute) exactly as they are during normal client-side rendering. Hydration should be silent.
What is actually happening?
Since 3.5.39, dynamic props on SVG elements are patched as DOM properties during hydration, which throws for read-only SVG IDL properties:
[Vue warn]: Failed setting prop "viewBox" on <svg>: value 0 0 24 24 is invalid.
TypeError: Cannot set property viewBox of #<SVGSVGElement> which has only a getter
at patchDOMProp
at patchProp
at hydrateElement
at hydrateNode
at hydrateSubTree
at <App>
Nothing is visually broken but the console is flooded with warnings.
We ran across this issue in a Nuxt app but I believe the issue is coming from Vue core
<!-- generated by vue-issues. DO NOT REMOVE -->
The two patches
packages/runtime-core/__tests__/hydration.spec.tsindex 144473dd3..db365b0ae 100644@@ -2776,5 +2776,148 @@ describe('SSR hydration', () => { expect(el.getAttribute('id')).toBe('client') expect(el.getAttribute('value')).toBe('server') })++ // mimic browsers, where SVG IDL attributes like viewBox / width are+ // getter-only properties (SVGAnimated*) that throw when assigned+ function defineReadonlySVGProp(el: Element, key: string) {+ Object.defineProperty(el, key, {+ configurable: true,+ get: () => ({}),+ })+ }++ function hydrateWithReadonlySVGProps(+ html: string,+ readonly: [selector: string, key: string][],+ render: () => any,+ ) {+ const container = document.createElement('div')+ container.innerHTML = html+ readonly.forEach(([selector, key]) =>+ defineReadonlySVGProp(container.querySelector(selector)!, key),+ )+ createSSRApp({ render }).mount(container)+ return container+ }++ test('patches dynamic props on svg elements as attributes', () => {+ const container = hydrateWithReadonlySVGProps(+ `<svg viewBox="0 0 24 24" width="24"><rect width="10"></rect></svg>`,+ [+ ['svg', 'viewBox'],+ ['svg', 'width'],+ ['rect', 'width'],+ ],+ () => (+ openBlock(),+ createElementBlock(+ 'svg',+ { viewBox: '0 0 24 24', width: '24' },+ [+ createElementVNode(+ 'rect',+ { width: '10' },+ null,+ PatchFlags.PROPS,+ ['width'],+ ),+ ],+ PatchFlags.PROPS,+ ['viewBox', 'width'],+ )+ ),+ )+ expect(`Failed setting prop`).not.toHaveBeenWarned()+ const svg = container.querySelector('svg')!+ expect(svg.getAttribute('viewBox')).toBe('0 0 24 24')+ expect(svg.getAttribute('width')).toBe('24')+ expect(container.querySelector('rect')!.getAttribute('width')).toBe('10')+ })++ test('patches dynamic props on svg elements as attributes in production', () => {+ __DEV__ = false+ try {+ const container = hydrateWithReadonlySVGProps(+ `<svg viewBox="0 0 10 10"></svg>`,+ [['svg', 'viewBox']],+ () => (+ openBlock(),+ createElementBlock(+ 'svg',+ { viewBox: '0 0 24 24' },+ null,+ PatchFlags.PROPS,+ ['viewBox'],+ )+ ),+ )+ expect(container.querySelector('svg')!.getAttribute('viewBox')).toBe(+ '0 0 24 24',+ )+ } finally {+ __DEV__ = true+ }+ })++ test('patches dynamic props on <foreignObject> as svg attributes', () => {+ const container = hydrateWithReadonlySVGProps(+ `<svg><foreignObject width="10"><div id="a"></div></foreignObject></svg>`,+ [['foreignObject', 'width']],+ () => (+ openBlock(),+ createElementBlock('svg', null, [+ createElementVNode(+ 'foreignObject',+ { width: '10' },+ [+ createElementVNode('div', { id: 'a' }, null, PatchFlags.PROPS, [+ 'id',+ ]),+ ],+ PatchFlags.PROPS,+ ['width'],+ ),+ ])+ ),+ )+ expect(`Failed setting prop`).not.toHaveBeenWarned()+ expect(+ container.querySelector('foreignObject')!.getAttribute('width'),+ ).toBe('10')+ expect(container.querySelector('div')!.id).toBe('a')+ })++ test('patches dynamic props on teleported svg elements as attributes', () => {+ const teleportContainer = document.createElement('div')+ teleportContainer.id = 'teleport-svg'+ teleportContainer.innerHTML =+ `<!--teleport start anchor--><svg viewBox="0 0 24 24"></svg>` ++ `<!--teleport anchor-->`+ document.body.appendChild(teleportContainer)+ defineReadonlySVGProp(teleportContainer.querySelector('svg')!, 'viewBox')++ const container = document.createElement('div')+ container.innerHTML = `<!--teleport start--><!--teleport end-->`+ createSSRApp({+ render: () =>+ h(Teleport, { to: '#teleport-svg' }, [+ (openBlock(),+ createElementBlock(+ 'svg',+ { viewBox: '0 0 24 24' },+ null,+ PatchFlags.PROPS,+ ['viewBox'],+ )),+ ]),+ }).mount(container)++ expect(`Failed setting prop`).not.toHaveBeenWarned()+ expect(`Hydration`).not.toHaveBeenWarned()+ expect(+ teleportContainer.querySelector('svg')!.getAttribute('viewBox'),+ ).toBe('0 0 24 24')+ teleportContainer.remove()+ }) }) })packages/runtime-core/src/hydration.tsindex c315ab940..04bfb38c8 100644@@ -72,6 +72,16 @@ const isSVGContainer = (container: Element) => const isMathMLContainer = (container: Element) => container.namespaceURI!.includes('MathML') +// namespace of the element itself (unlike getContainerType, which returns the+// namespace for the container's children, so <foreignObject> is still 'svg')+const getElementNamespace = (el: Element): 'svg' | 'mathml' | undefined => {+ const ns = el.namespaceURI+ if (!ns) return undefined+ if (ns.includes('svg')) return 'svg'+ if (ns.includes('MathML')) return 'mathml'+ return undefined+}+ const getContainerType = ( container: Element | ShadowRoot, ): 'svg' | 'mathml' | undefined => {@@ -500,6 +510,7 @@ export function createHydrationFunctions( patchFlag & (PatchFlags.FULL_PROPS | PatchFlags.NEED_HYDRATION) ) { const isCustomElement = el.tagName.includes('-')+ const namespace = getElementNamespace(el) for (const key in props) { // check hydration mismatch if (@@ -520,7 +531,7 @@ export function createHydrationFunctions( (isCustomElement && !isReservedProp(key)) || (dynamicProps && dynamicProps.includes(key)) ) {- patchProp(el, key, null, props[key], undefined, parentComponent)+ patchProp(el, key, null, props[key], namespace, parentComponent) } } } else if (props.onClick) {@@ -531,7 +542,7 @@ export function createHydrationFunctions( 'onClick', null, props.onClick,- undefined,+ getElementNamespace(el), parentComponent, ) } else if (patchFlag & PatchFlags.STYLE && isReactive(props.style)) {
packages/runtime-core/__tests__/hydration.spec.tsindex 144473dd3..df0114065 100644@@ -2755,6 +2755,66 @@ describe('SSR hydration', () => { } }) + test('force patch svg dynamic props with correct namespace when hydrating', () => {+ __DEV__ = false+ try {+ const { container } = mountWithHydration(+ `<svg width="24" height="24" viewBox="0 0 24 24"></svg>`,+ () =>+ createElementVNode(+ 'svg',+ { width: 48, height: 48, viewBox: '0 0 48 48' },+ null,+ PatchFlags.PROPS,+ ['width', 'height', 'viewBox'],+ ),+ )+ const el = container.firstChild as Element+ expect(el.namespaceURI).toContain('svg')+ expect(el.getAttribute('width')).toBe('48')+ expect(el.getAttribute('height')).toBe('48')+ expect(el.getAttribute('viewBox')).toBe('0 0 48 48')+ } finally {+ __DEV__ = true+ }+ })++ test('force patch foreignObject dynamic props with correct namespace when hydrating', () => {+ __DEV__ = false+ try {+ const container = document.createElement('div')+ container.innerHTML =+ '<svg><foreignObject width="24"></foreignObject></svg>'+ const el = container.querySelector('foreignObject')!+ expect(el.namespaceURI).toContain('svg')+ // jsdom doesn't implement SVGForeignObjectElement.width.+ Object.defineProperty(el, 'width', {+ configurable: true,+ get: () => 24,+ })++ createSSRApp({+ render: () => (+ openBlock(),+ createElementBlock('svg', null, [+ (openBlock(),+ createElementBlock(+ 'foreignObject',+ { width: 48 },+ null,+ PatchFlags.PROPS,+ ['width'],+ )),+ ])+ ),+ }).mount(container)++ expect(el.getAttribute('width')).toBe('48')+ } finally {+ __DEV__ = true+ }+ })+ test('only patches declared dynamic props when hydrating', () => { const { container } = mountWithHydration( `<div data-allow-mismatch="attribute" id="server" value="server"></div>`,packages/runtime-core/src/hydration.tsindex c315ab940..405a5db93 100644@@ -500,6 +500,11 @@ export function createHydrationFunctions( patchFlag & (PatchFlags.FULL_PROPS | PatchFlags.NEED_HYDRATION) ) { const isCustomElement = el.tagName.includes('-')+ const namespace = el.namespaceURI!.includes('svg')+ ? 'svg'+ : el.namespaceURI!.includes('MathML')+ ? 'mathml'+ : undefined for (const key in props) { // check hydration mismatch if (@@ -520,7 +525,7 @@ export function createHydrationFunctions( (isCustomElement && !isReservedProp(key)) || (dynamicProps && dynamicProps.includes(key)) ) {- patchProp(el, key, null, props[key], undefined, parentComponent)+ patchProp(el, key, null, props[key], namespace, parentComponent) } } } else if (props.onClick) {