From 5a245e5e7a22e7d5888a86440455f88464205071 Mon Sep 17 00:00:00 2001 From: LilianBarbe <110779804+LilianBarbe@users.noreply.github.com> Date: Tue, 13 Oct 2026 14:15:38 +0200 Subject: [PATCH] fix(preview): a canvas patch keeps the CSS Vite hot-swapped MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A style edit written to a stylesheet that reaches the page through a CSS `@import` (global.css importing utilities.css) showed on the canvas and was then lost, or never showed: the file held the new value, the canvas the one from when the dev server started. Astro renders such a stylesheet from its first server-side compile for as long as the server runs, while Vite hot-swaps the live ` + - `` + - '', - 'text/html', - ); + // Vite has hot-swapped this one since the server first compiled it. + const sheet = devStyle(SHEET, '.pad { padding: 3rem; }'); + head.append(page, card, own, sheet); + const rendering = (pageCss) => + new dom.window.DOMParser().parseFromString( + '' + + `` + + `` + + `` + + '', + 'text/html', + ); + const before = rendering('.box { color: red; }'); + const served = rendering('.box { color: blue; }'); const writes = []; const observer = new dom.window.MutationObserver((records) => writes.push(...records)); observer.observe(head, { childList: true, subtree: true, characterData: true }); - refreshDevStyles(served); + refreshDevStyles(before, served); writes.push(...observer.takeRecords()); observer.disconnect(); check( @@ -600,6 +614,27 @@ const LIVE_TABS = (labels, active) => 'a stylesheet the rendering does not carry is left to Vite', own.textContent === '.vite { margin: 0; }', ); + check( + 'a stylesheet the server renders stale keeps the text Vite gave it', + sheet.textContent === '.pad { padding: 3rem; }', + sheet.textContent, + ); + // A stylesheet new to the server's rendering has no earlier text to agree with. + const arrived = devStyle(CARD, '.card { gap: 0; }'); + head.append(arrived); + const without = new dom.window.DOMParser().parseFromString( + '', + 'text/html', + ); + card.remove(); + refreshDevStyles(without, served); + check( + 'a stylesheet the previous rendering lacked takes the server text', + arrived.textContent === '.card { gap: 1rem; }', + arrived.textContent, + ); + arrived.remove(); + head.append(card); check( 'exactly one stylesheet was written', new Set( @@ -612,6 +647,7 @@ const LIVE_TABS = (labels, active) => page.remove(); card.remove(); own.remove(); + sheet.remove(); } if (failures.length) {