Skip to content

Commit

Permalink
fix(engine-dom): hydration errors due type mismatch (#2556)
Browse files Browse the repository at this point in the history
* fix(engine-dom): hydration errors due type mismatch

* wip: normalize text in vnode creation
  • Loading branch information
jodarove authored Nov 3, 2021
1 parent 2f21d93 commit 4420492
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 4 deletions.
4 changes: 2 additions & 2 deletions packages/@lwc/engine-core/src/framework/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,8 @@ function co(text: string): VComment {
}

// [d]ynamic text
function d(value: any): string | any {
return value == null ? '' : value;
function d(value: any): string {
return value == null ? '' : String(value);
}

// [b]ind function
Expand Down
4 changes: 2 additions & 2 deletions packages/@lwc/engine-core/src/framework/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ function vnodesAndElementHaveCompatibleAttrs(vnode: VNode, elm: Element): boolea
// Note: intentionally ONLY matching vnodes.attrs to elm.attrs, in case SSR is adding extra attributes.
for (const [attrName, attrValue] of Object.entries(attrs)) {
const elmAttrValue = renderer.getAttribute(elm, attrName);
if (attrValue !== elmAttrValue) {
if (String(attrValue) !== elmAttrValue) {
logError(
`Mismatch hydrating element <${elm.tagName.toLowerCase()}>: attribute "${attrName}" has different values, expected "${attrValue}" but found "${elmAttrValue}"`,
vnode.owner
Expand All @@ -300,7 +300,7 @@ function vnodesAndElementHaveCompatibleClass(vnode: VNode, elm: Element): boolea
let nodesAreCompatible = true;
let vnodeClassName;

if (!isUndefined(className) && className !== elm.className) {
if (!isUndefined(className) && String(className) !== elm.className) {
// className is used when class is bound to an expr.
nodesAreCompatible = false;
vnodeClassName = className;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export default {
snapshot(target) {
const p = target.shadowRoot.querySelector('p');
return {
p,
text: p.firstChild,
};
},
test(target, snapshots, consoleCalls) {
expect(consoleCalls.warn).toHaveSize(0);
expect(consoleCalls.error).toHaveSize(0);

const p = target.shadowRoot.querySelector('p');
expect(p).toBe(snapshots.p);
expect(p.firstChild).toBe(snapshots.text);

expect(p.textContent).toBe('123');
expect(p.getAttribute('class')).toBe('123');
expect(p.getAttribute('data-attr')).toBe('123');
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<template>
<!-- style attr is validated at compile time -->
<p class={num} data-attr={num}>{num}</p>
</template>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { LightningElement } from 'lwc';

export default class Main extends LightningElement {
num = 123;
}

0 comments on commit 4420492

Please sign in to comment.