fix(runtime-core): pass SVG/MathML namespace to patchProp during hydration#15050
fix(runtime-core): pass SVG/MathML namespace to patchProp during hydration#15050masoudei wants to merge 2 commits into
Conversation
During SSR hydration, hydrateElement was passing undefined as the namespace parameter to patchProp. This caused SVG coordinate attributes (x1, y1, x2, y2, cx, cy, r, etc.) to be incorrectly set as read-only DOM properties instead of via setAttribute, triggering console warnings. Added elementNamespace detection from el.namespaceURI and pass it to both patchProp call sites in hydrateElement. Signed-off-by: Masoud Ehteshami <ehteshami.developer@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (2)
🚧 Files skipped from review as they are similar to previous changes (2)
📝 WalkthroughWalkthroughUpdates hydration so SVG and MathML-aware prop setting uses the element’s own namespace during SSR hydration. Adds coverage for SVG line, circle, and foreignObject attribute hydration without “Failed setting prop” warnings. ChangesSVG/MathML namespace hydration fix
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related PRs
Suggested labels: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
packages/runtime-core/__tests__/hydration.spec.ts (1)
1597-1628: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick winAdd coverage for
foreignObjectown attributes.These tests cover
<line>/<circle>well but don't exercise a<foreignObject>element's ownx/y/width/heightattributes, which is the edge case where the currentelementNamespacecomputation inhydration.ts(excludingforeignObjectfrom the svg namespace) would regress.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/runtime-core/__tests__/hydration.spec.ts` around lines 1597 - 1628, Add a hydration test for SVG foreignObject own attributes using mountWithHydration and h, similar to the existing SVG coordinate tests. The new test should render a foreignObject inside an svg with x, y, width, and height, then assert those values are present as attributes on the element and that no prop-setting warning is emitted. Reference the existing SVG hydration tests in hydration.spec.ts and the foreignObject namespace handling in hydration.ts to locate the edge case.packages/runtime-core/src/hydration.ts (1)
396-400: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winPrefer exact namespace match over substring
.includes().
el.namespaceURIis a full URI (http://www.w3.org/2000/svg,http://www.w3.org/1998/Math/MathML). Substring matching is more fragile than exact equality and diverges from howsvgNS/mathmlNSare compared elsewhere in the renderer. Consider comparing against the exact known namespace URIs instead of.includes().🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@packages/runtime-core/src/hydration.ts` around lines 396 - 400, Update the namespace detection in hydration logic to use exact namespace URI equality instead of substring checks. In the elementNamespace selection in hydration.ts, compare el.namespaceURI against the known SVG and MathML namespace constants/URIs used elsewhere in the renderer, while preserving the foreignObject exception. Keep the existing branching structure but replace the .includes() checks with exact matches so the behavior is consistent with svgNS/mathmlNS handling.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@packages/runtime-core/__tests__/hydration.spec.ts`:
- Around line 1597-1628: Add a hydration test for SVG foreignObject own
attributes using mountWithHydration and h, similar to the existing SVG
coordinate tests. The new test should render a foreignObject inside an svg with
x, y, width, and height, then assert those values are present as attributes on
the element and that no prop-setting warning is emitted. Reference the existing
SVG hydration tests in hydration.spec.ts and the foreignObject namespace
handling in hydration.ts to locate the edge case.
In `@packages/runtime-core/src/hydration.ts`:
- Around line 396-400: Update the namespace detection in hydration logic to use
exact namespace URI equality instead of substring checks. In the
elementNamespace selection in hydration.ts, compare el.namespaceURI against the
known SVG and MathML namespace constants/URIs used elsewhere in the renderer,
while preserving the foreignObject exception. Keep the existing branching
structure but replace the .includes() checks with exact matches so the behavior
is consistent with svgNS/mathmlNS handling.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 24c21107-f7f4-4955-9630-33d6b27ba5b0
📒 Files selected for processing (2)
packages/runtime-core/__tests__/hydration.spec.tspackages/runtime-core/src/hydration.ts
|
The direction looks right, but this should compute the namespace after the |
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
vue
@vue/compat
commit: |
Size ReportBundles
Usages
|
…t in hydrateElement Move SVG/MathML namespace detection after <template> replacement so that el is the actual element, not a <template> placeholder (relevant for <Transition appear>). Also fixes optional chaining lint error (ES2016 target). Adds foreignObject hydration test. Signed-off-by: Masoud Ehteshami <ehteshami.developer@gmail.com>
|
Addressed review feedback in 3604943: edison1105: Moved elementNamespace computation after the template replacement block so the actual element (not a template placeholder) is used — relevant for Transition appear where el starts as template then gets replaced with SVG content. CodeRabbit nitpick (optional chaining): Replaced ?. with ! (non-null assertion) to fix ES2016 target lint error, matching the existing isSVGContainer pattern. CodeRabbit nitpick (foreignObject test): Added test verifying foreignObject own attributes (x, y, width, height) hydrate correctly through the HTML attribute path. |
Description
hydrateElementinruntime-core/src/hydration.tswas passingundefinedas the namespace parameter topatchPropwhen hydrating SVG and MathML elements. This caused SVG coordinate attributes (x1,y1,x2,y2,cx,cy,r,x,y,width,height) to be set as read-only DOM properties instead of viasetAttribute, triggering console warnings on every SSR hydration of SVG content.Root Cause
In
hydrateElement,patchPropis called withundefinedas the namespace argument:In
patchProp, the namespace is used to determineisSVG(namespace === 'svg'). Whenundefined,isSVGis falsy, andshouldSetAsPropfalls through to the HTML path wherereturn key in elreturnstruefor SVG coordinate properties (they exist as read-onlySVGAnimatedLengthgetters). Vue then attempts DOM property assignment (el.x1 = 50) instead ofsetAttribute, causing a TypeError.Fix
Added namespace detection from the element itself in
hydrateElement:This is then passed to both
patchPropcall sites inhydrateElement, mirroring the same pattern already used bymountComponent(which correctly passesgetContainerType(container)).Tests Added
<line>elements withx1,y1,x2,y2hydrate without warnings<circle>elements withcx,cy,rhydrate without warningsBoth tests assert that:
getAttribute()Failed setting propwarnings were emittedAll 114 hydration tests pass.
Summary by CodeRabbit
x1/y1/x2/y2andcx/cy/rare applied correctly.foreignObject.foreignObjectsizing/positioning, verifying correct DOM hydration and absence of related warnings.