Backtestsvuerun-20261005-155915#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. During hydration they derive the element's namespace from `el.namespaceURI` and pass it to `patchProp` instead of `undefined`. `patchProp` then treats SVG and MathML props as attributes, which matches client-side mount behaviour. Both correctly use the element's own namespace, so `<foreignObject>` props are still patched as SVG. Correctness is effectively equal. B is slightly more defensive: it null-checks `namespaceURI` rather than using a `!` assertion. B also passes the namespace on the `onClick`-only path, which is consistent and harmless. Tests are the main difference. - A's first test (plain svg with width, height and viewBox under jsdom) likely passes even without the fix. jsdom does not implement these as getter-only IDL properties, so `patchProp` would fall back to `setAttribute` either way. - A's second test is meaningful. It defines a getter-only `width` on a `foreignObject` instance with `__DEV__` off, so the property-set path would fail silently and leave the attribute unchanged. - B's test emulates browser behaviour directly by installing getter-only `viewBox` and `x` on `SVGElement.prototype`, covering both an `<svg>` and a nested `<foreignObject>`. It asserts the attributes are updated and that no 'Failed setting prop' warning appears, which reproduces the exact warning from the issue. It would fail before the fix, cleans up the prototype afterwards, and accounts for the expected mismatch warnings. For craft, B extracts a small, well-commented `getElementNamespace` helper that explains how it differs from `getContainerType`. A uses an inline nested ternary with non-null assertions. Overall, B's fix is equivalent but its regression test is stronger and its code is slightly cleaner.
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
7m 56s
Wall-clock time in the container
Full test suite
No new failures
3,613 passed · 6 skipped · 3,621 total
Contamination audit
Clean
No sign the agent saw the real fix
Patch size
66vs 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..74b2c4bf3 100644@@ -2755,6 +2755,55 @@ describe('SSR hydration', () => { } }) + // #15081+ test('patch dynamic props on SVG elements as attributes when hydrating', () => {+ // emulate browsers, where many SVG IDL attributes are getter-only+ const getterOnly = (proto: object, key: string) => {+ Object.defineProperty(proto, key, {+ configurable: true,+ get() {+ return null+ },+ })+ return () => delete (proto as any)[key]+ }+ const restoreViewBox = getterOnly(SVGElement.prototype, 'viewBox')+ const restoreX = getterOnly(SVGElement.prototype, 'x')+ try {+ const { container } = mountWithHydration(+ `<div><svg viewBox="0 0 24 24"><foreignObject x="1"></foreignObject></svg></div>`,+ () => (+ openBlock(),+ createElementBlock('div', null, [+ createElementVNode(+ 'svg',+ { viewBox: '0 0 48 48' },+ [+ createElementVNode(+ 'foreignObject',+ { x: '2' },+ null,+ PatchFlags.PROPS,+ ['x'],+ ),+ ],+ PatchFlags.PROPS,+ ['viewBox'],+ ),+ ])+ ),+ )+ const svg = container.querySelector('svg')!+ expect(svg.getAttribute('viewBox')).toBe('0 0 48 48')+ expect(svg.querySelector('foreignObject')!.getAttribute('x')).toBe('2')+ expect(`Failed setting prop`).not.toHaveBeenWarned()+ } finally {+ restoreViewBox()+ restoreX()+ }+ expect(`Hydration attribute mismatch`).toHaveBeenWarnedTimes(2)+ })+ 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..7242e9fb0 100644@@ -81,6 +81,18 @@ const getContainerType = ( return undefined } +// namespace of the element itself (unlike getContainerType, which resolves+// the namespace of its children), matching what the renderer passes to+// patchProp on mount: <foreignObject> props are still patched as SVG.+const getElementNamespace = (el: Element): 'svg' | 'mathml' | undefined => {+ const ns = el.namespaceURI+ if (ns) {+ if (ns.includes('svg')) return 'svg'+ if (ns.includes('MathML')) return 'mathml'+ }+ return undefined+}+ export const isComment = (node: Node): node is Comment => node.nodeType === DOMNodeTypes.COMMENT @@ -500,6 +512,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 +533,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 +544,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) {