Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 | 69x 61x 40x 40x 40x 40x 40x 4x 36x 32x 4x 56x 56x 2x 54x 9x 9x 9x 9x 3x 9x 45x 1x 2x 2x 2x 2x 1x 44x 44x 44x 56x 56x 4x 4x 40x 40x 320x 320x 5x 5x 5x 1x 1x 1x 1x 4x 3x 40x 40x 40x 27x 40x 40x 38x 38x 38x 11x 11x 27x 26x 1x 1x 40x 35x 32x 32x 32x 59x 32x 32x 32x 2x 42x 42x 16x 7x 7x 2x 2x 65x 62x 62x 62x 62x 62x 2x 60x 1x 1x 1x 62x 62x 62x 62x 26x 25x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* Mismatch detection for Coherent.js hydration
*
* Compares server-rendered DOM against client virtual DOM to detect
* hydration mismatches in development mode.
*/
/**
* Format path segments into readable string
* @param {Array} segments - Path segments
* @returns {string} - Formatted path
*/
export function formatPath(segments) {
if (!segments || segments.length === 0) return 'root';
return segments.join('.');
}
/**
* Get children from virtual node
* @private
*/
function getVNodeChildren(vNode) {
Iif (!vNode || typeof vNode !== 'object' || Array.isArray(vNode)) {
return [];
}
const tagName = Object.keys(vNode)[0];
const props = vNode[tagName];
Iif (!props || typeof props !== 'object') {
return [];
}
if (props.children) {
return Array.isArray(props.children) ? props.children : [props.children];
}
if (props.text !== undefined) {
return [String(props.text)];
}
return [];
}
/**
* Detect mismatches between DOM and virtual DOM
*
* @param {Element} domElement - Real DOM element
* @param {Object|string|number} virtualNode - Virtual DOM node
* @param {Array} path - Current path for error reporting
* @returns {Array} - Array of mismatch objects
*/
export function detectMismatch(domElement, virtualNode, path = []) {
const mismatches = [];
// Handle null/undefined virtual node
if (virtualNode === null || virtualNode === undefined) {
return mismatches;
}
// Handle text nodes (string or number in virtual DOM)
if (typeof virtualNode === 'string' || typeof virtualNode === 'number') {
const expectedText = String(virtualNode).trim();
// DOM might be a text node or element containing text
let actualText;
if (domElement.nodeType === 3) { // Node.TEXT_NODE
actualText = domElement.textContent?.trim() || '';
} else E{
// For element nodes, get direct text content
actualText = domElement.textContent?.trim() || '';
}
if (actualText !== expectedText) {
mismatches.push({
path: formatPath(path),
type: 'text',
expected: expectedText,
actual: actualText,
domPath: getDOMPath(domElement)
});
}
return mismatches;
}
// Handle arrays
if (Array.isArray(virtualNode)) {
virtualNode.forEach((child, index) => {
const domChild = getDOMChildAtIndex(domElement, index);
if (domChild) {
const childMismatches = detectMismatch(
domChild,
child,
[...path, `[${index}]`]
);
mismatches.push(...childMismatches);
} else E{
mismatches.push({
path: formatPath([...path, `[${index}]`]),
type: 'missing_element',
expected: describeVNode(child),
actual: null,
domPath: `${getDOMPath(domElement)} > child[${index}]`
});
}
});
return mismatches;
}
// Handle element nodes
Iif (typeof virtualNode !== 'object') {
return mismatches;
}
const tagName = Object.keys(virtualNode)[0];
const props = virtualNode[tagName] || {};
// Check tag name
const domTagName = domElement.tagName?.toLowerCase();
if (domTagName !== tagName.toLowerCase()) {
mismatches.push({
path: formatPath(path),
type: 'tagName',
expected: tagName,
actual: domTagName,
domPath: getDOMPath(domElement)
});
// Can't continue comparing if tag is different
return mismatches;
}
// Check critical attributes
const attributeChecks = [
{ virtual: 'className', dom: 'class' },
{ virtual: 'id', dom: 'id' },
{ virtual: 'type', dom: 'type' },
{ virtual: 'value', dom: 'value' },
{ virtual: 'checked', dom: 'checked' },
{ virtual: 'disabled', dom: 'disabled' },
{ virtual: 'href', dom: 'href' },
{ virtual: 'src', dom: 'src' }
];
attributeChecks.forEach(({ virtual, dom }) => {
const expectedValue = props[virtual];
if (expectedValue === undefined) return;
const actualValue = domElement.getAttribute(dom);
const expectedStr = String(expectedValue);
// Handle boolean attributes
if (typeof expectedValue === 'boolean') {
const actualBool = actualValue !== null;
Eif (expectedValue !== actualBool) {
mismatches.push({
path: formatPath([...path, `@${dom}`]),
type: 'attribute',
expected: expectedValue,
actual: actualBool,
domPath: getDOMPath(domElement)
});
}
return;
}
if (expectedStr !== actualValue) {
mismatches.push({
path: formatPath([...path, `@${dom}`]),
type: 'attribute',
expected: expectedStr,
actual: actualValue,
domPath: getDOMPath(domElement)
});
}
});
// Recursively check children
const vChildren = getVNodeChildren({ [tagName]: props });
const dChildren = getSignificantDOMChildren(domElement);
// Check for child count mismatch
if (vChildren.length !== dChildren.length) {
mismatches.push({
path: formatPath([...path, 'children']),
type: 'children_count',
expected: vChildren.length,
actual: dChildren.length,
domPath: getDOMPath(domElement)
});
}
// Compare each child
const maxChildren = Math.max(vChildren.length, dChildren.length);
for (let i = 0; i < maxChildren; i++) {
const vChild = vChildren[i];
const dChild = dChildren[i];
if (vChild && dChild) {
const childMismatches = detectMismatch(
dChild,
vChild,
[...path, `children[${i}]`]
);
mismatches.push(...childMismatches);
} else if (vChild && !dChild) {
mismatches.push({
path: formatPath([...path, `children[${i}]`]),
type: 'missing_dom_child',
expected: describeVNode(vChild),
actual: null,
domPath: getDOMPath(domElement)
});
} else Eif (!vChild && dChild) {
mismatches.push({
path: formatPath([...path, `children[${i}]`]),
type: 'extra_dom_child',
expected: null,
actual: describeNode(dChild),
domPath: getDOMPath(domElement)
});
}
}
return mismatches;
}
/**
* Report mismatches to console with detailed information
*
* @param {Array} mismatches - Array of mismatch objects
* @param {Object} options - Reporting options
*/
export function reportMismatches(mismatches, options = {}) {
if (!mismatches || mismatches.length === 0) return;
const { componentName = 'Unknown', strict = false } = options;
const header = `[Coherent.js] Hydration mismatch detected in "${componentName}"!\n` +
`Found ${mismatches.length} difference(s) between server and client:\n`;
const details = mismatches.map((m, i) => {
return `\n${i + 1}. ${m.type} at ${m.path}\n` +
` DOM path: ${m.domPath}\n` +
` Expected: ${JSON.stringify(m.expected)}\n` +
` Actual: ${JSON.stringify(m.actual)}`;
}).join('');
const advice = '\n\nThis usually happens when:\n' +
' - Server renders with different data than client\n' +
' - Using Date.now(), Math.random(), or browser-only APIs during render\n' +
' - Component is not pure (has side effects during render)\n';
console.warn(header + details + advice);
if (strict) {
throw new Error(`Hydration failed: ${mismatches.length} mismatch(es) found. See console for details.`);
}
}
/**
* Get significant DOM children (elements and non-empty text nodes)
* @private
*/
function getSignificantDOMChildren(element) {
Iif (!element || !element.childNodes) return [];
return Array.from(element.childNodes).filter(node => {
if (node.nodeType === 1) return true; // Element node
Eif (node.nodeType === 3) { // Text node
return node.textContent && node.textContent.trim().length > 0;
}
return false;
});
}
/**
* Get DOM child at specific index (considering only significant children)
* @private
*/
function getDOMChildAtIndex(parent, index) {
const children = getSignificantDOMChildren(parent);
return children[index] || null;
}
/**
* Get a readable DOM path for debugging
* @private
*/
function getDOMPath(element) {
if (!element || !element.tagName) return '(unknown)';
const parts = [];
let current = element;
while (current && current.tagName) {
let selector = current.tagName.toLowerCase();
if (current.id) {
selector += `#${current.id}`;
} else if (current.className && typeof current.className === 'string') {
const classes = current.className.trim().split(/\s+/).slice(0, 2);
Eif (classes.length > 0 && classes[0]) {
selector += `.${classes.join('.')}`;
}
}
parts.unshift(selector);
current = current.parentElement;
// Limit depth
Iif (parts.length > 5) {
parts.unshift('...');
break;
}
}
return parts.join(' > ');
}
/**
* Describe a virtual node for error messages
* @private
*/
function describeVNode(vNode) {
if (typeof vNode === 'string' || typeof vNode === 'number') {
return `text: "${String(vNode).substring(0, 50)}"`;
}
Iif (Array.isArray(vNode)) {
return `array[${vNode.length}]`;
}
Eif (typeof vNode === 'object' && vNode !== null) {
const tagName = Object.keys(vNode)[0];
return `<${tagName}>`;
}
return String(vNode);
}
/**
* Describe a DOM node for error messages
* @private
*/
function describeNode(node) {
Iif (!node) return '(null)';
Iif (node.nodeType === 3) { // Text node
return `text: "${(node.textContent || '').substring(0, 50)}"`;
}
Eif (node.nodeType === 1) { // Element
return `<${node.tagName.toLowerCase()}>`;
}
return `node(type=${node.nodeType})`;
}
|