The design language that makes your AI harness better at design.
JavaScript
Fix React hydration mismatch from live pick-cursor class on SSR roots (#286)
Paul Bakaus8eedb150c584
diff --git a/skill/scripts/live-browser.js b/skill/scripts/live-browser.jsindex bc71d8e5a..bd4d56e36 100644--- a/skill/scripts/live-browser.js+++ b/skill/scripts/live-browser.js@@ -57,7 +57,7 @@const Z = { highlight: 100001, bar: 100005, picker: 100007, toast: 100010 };const EASE = 'cubic-bezier(0.22, 1, 0.36, 1)'; // ease-out-quintconst PREFIX = 'impeccable-live';- const PICK_CURSOR_CLASS = PREFIX + '-pick-cursor';+ const PICK_CURSOR_STYLE_ID = PREFIX + '-pick-cursor-style';const MANUAL_APPLY_STATE_TTL_MS = 15 * 60 * 1000;const sessionState = window.__IMPECCABLE_LIVE_SESSION__?.createLiveBrowserSessionState({prefix: PREFIX,@@ -1916,45 +1916,45 @@syncPageInteractionCursor();}- let pageInteractionCursorActive = false;-- function ensurePickCursorStyle() {- if (document.getElementById(PREFIX + '-pick-cursor-style')) return;- const style = document.createElement('style');- style.id = PREFIX + '-pick-cursor-style';+ /**+ * Drive the page-level pick / insert cursor through the textContent of one+ * injected <style>, never by mutating <html> (className or inline style).+ * Frameworks that server-render the <html>/<body> roots (Next.js App Router)+ * report a React 19 hydration mismatch when the client adds an attribute the+ * server HTML never emitted, so a `class`/inline `style` toggled on+ * `document.documentElement` trips "a tree hydrated but some attributes ...+ * didn't match" on the next Fast-Refresh re-render. Keying the cursor off a+ * stable-id <style> keeps the effect off the hydrated host elements (same+ * shape as the scroll-anchor lock). A falsy cursor clears the rule.+ */+ function setPageInteractionCursor(cursor) {+ let style = document.getElementById(PICK_CURSOR_STYLE_ID);+ if (!cursor) {+ if (style) style.textContent = '';+ return;+ }+ if (!style) {+ style = document.createElement('style');+ style.id = PICK_CURSOR_STYLE_ID;+ // Styles the host page, not the chrome - inside the adapter's shadow UI+ // root (uiAppendStyle's target) these selectors would match nothing.+ (document.head || document.documentElement).appendChild(style);+ }style.textContent =- 'html.' + PICK_CURSOR_CLASS + ' * { cursor: crosshair !important; }\n'- + 'html.' + PICK_CURSOR_CLASS + ' [id^="' + PREFIX + '"],\n'- + 'html.' + PICK_CURSOR_CLASS + ' [id^="' + PREFIX + '"] * { cursor: revert !important; }';- // Styles the host page, not the chrome - inside the adapter's shadow UI- // root (uiAppendStyle's target) these selectors would match nothing.- document.head.appendChild(style);+ '* { cursor: ' + cursor + ' !important; }\n'+ + '[id^="' + PREFIX + '"],\n'+ + '[id^="' + PREFIX + '"] * { cursor: revert !important; }';}/** Page-level cursor while pick or insert mode is targeting page elements. */function syncPageInteractionCursor() {- const pickCursor = state === 'PICKING' && pickActive && !insertActive;- let axisCursor = '';- if (state === 'PICKING' && insertActive) {- axisCursor = insertHoverAnchor ? cursorForInsertAxis(insertHoverAxis || 'column') : '';- }-- if (pickCursor) {- ensurePickCursorStyle();- document.documentElement.classList.add(PICK_CURSOR_CLASS);- document.documentElement.style.cursor = '';- pageInteractionCursorActive = true;- return;- }-- document.documentElement.classList.remove(PICK_CURSOR_CLASS);- if (axisCursor) {- document.documentElement.style.cursor = axisCursor;- pageInteractionCursorActive = true;- } else if (pageInteractionCursorActive) {- document.documentElement.style.cursor = '';- pageInteractionCursorActive = false;+ let cursor = '';+ if (state === 'PICKING' && pickActive && !insertActive) {+ cursor = 'crosshair';+ } else if (state === 'PICKING' && insertActive && insertHoverAnchor) {+ cursor = cursorForInsertAxis(insertHoverAxis || 'column');}+ setPageInteractionCursor(cursor);}/**@@ -9983,7 +9983,7 @@ void main() {// Remove detection overlayswindow.postMessage({ source: 'impeccable-command', action: 'remove' }, '*');setLiveState('IDLE');- document.getElementById(PREFIX + '-pick-cursor-style')?.remove();+ document.getElementById(PICK_CURSOR_STYLE_ID)?.remove();window.__IMPECCABLE_LIVE_INIT__ = false;console.log('[impeccable] Live mode exited.');}diff --git a/tests/live-browser-regression.test.mjs b/tests/live-browser-regression.test.mjsindex 9a91ccddf..e68e0768d 100644--- a/tests/live-browser-regression.test.mjs+++ b/tests/live-browser-regression.test.mjs@@ -559,15 +559,37 @@ describe('live-browser.js regression guards', () => {/function syncPageInteractionCursor\(\)[\s\S]{0,420}?cursorForInsertAxis/,'insert picking cursor follows row/column axis',);+ // The pick / insert cursor must be driven by an injected <style>, never by a+ // class or inline style on <html>. <html>/<body> are server-rendered by+ // frameworks like Next.js App Router, so a client-only attribute the server+ // HTML never emitted trips React 19's "a tree hydrated but some attributes+ // ... didn't match" on the next Fast-Refresh re-render — surfacing as a+ // console.error that fails the nextjs-app-router live-e2e expectConsoleClean+ // probe. Same fix shape as the scroll-anchor lock above.+ assert.doesNotMatch(+ SOURCE,+ /document\.documentElement\.classList\.(?:add|remove|toggle)\(/,+ 'event=live_browser.pick_cursor_hydration actor=browser operation=sync_page_interaction_cursor risk=react19_hydration_mismatch_on_next_app_router expected=stylesheet_rule actual=class_on_html',+ );+ assert.match(+ SOURCE,+ /const PICK_CURSOR_STYLE_ID = PREFIX \+ '-pick-cursor-style';/,+ 'the pick-cursor style needs a stable id constant so it can be created and removed by id',+ );+ assert.match(+ SOURCE,+ /function setPageInteractionCursor\(cursor\)[\s\S]{0,700}?cursor: ' \+ cursor \+ ' !important/,+ 'pick / insert cursor is applied through the injected <style> textContent, keyed by PICK_CURSOR_STYLE_ID',+ );assert.match(SOURCE,- /function ensurePickCursorStyle\(\)[\s\S]{0,420}?cursor: crosshair !important/,- 'pick mode injects a crosshair cursor that wins over page pointer styles',+ /cursor = 'crosshair'/,+ 'pick mode uses a crosshair cursor that wins over page pointer styles',);assert.match(SOURCE,- /document\.documentElement\.classList\.add\(PICK_CURSOR_CLASS\)/,- 'pick mode toggles a document-level class for the crosshair cursor',+ /document\.getElementById\(PICK_CURSOR_STYLE_ID\)\?\.remove\(\)/,+ 'exiting live mode removes the injected pick-cursor <style> so it never outlives the session',);assert.match(SOURCE, /function hitSiblingInsertGap\(/, 'insert mode detects gaps between siblings');assert.match(SOURCE, /function resolveInsertHover\(/, 'insert hover resolves axis-aware boundaries');