From ef8964c04d27688f14382363e01e19e85c3fec7f Mon Sep 17 00:00:00 2001 From: Martin Trapp <94928215+martrapp@users.noreply.github.com> Date: Tue, 31 Oct 2023 20:16:03 +0100 Subject: [PATCH] Three small improvements for handling client-only in view transitions (#8964) * client-only fixes * typo --- packages/astro/src/transitions/router.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/astro/src/transitions/router.ts b/packages/astro/src/transitions/router.ts index 583bf7dfaf..b740326dd3 100644 --- a/packages/astro/src/transitions/router.ts +++ b/packages/astro/src/transitions/router.ts @@ -532,9 +532,17 @@ async function prepareForClientOnlyComponents(newDocument: Document, toLocation: if (newDocument.body.querySelector(`astro-island[client='only']`)) { // Load the next page with an empty module loader cache const nextPage = document.createElement('iframe'); - nextPage.setAttribute('src', toLocation.href); + // do not fetch the file from the server, but use the current newDocument + nextPage.srcdoc = + (newDocument.doctype ? '' : '') + newDocument.documentElement.outerHTML; nextPage.style.display = 'none'; document.body.append(nextPage); + // silence the iframe's console + // @ts-ignore + nextPage.contentWindow!.console = Object.keys(console).reduce((acc: any, key) => { + acc[key] = () => {}; + return acc; + }, {}); await hydrationDone(nextPage); const nextHead = nextPage.contentDocument?.head; @@ -552,7 +560,7 @@ async function prepareForClientOnlyComponents(newDocument: Document, toLocation: viteIds.forEach((id) => { const style = document.head.querySelector(`style[${VITE_ID}="${id}"]`); if (style && !newDocument.head.querySelector(`style[${VITE_ID}="${id}"]`)) { - newDocument.head.appendChild(style); + newDocument.head.appendChild(style.cloneNode(true)); } }); }