diff options
author | 2025-03-21 13:46:37 +0100 | |
---|---|---|
committer | 2025-03-21 13:46:37 +0100 | |
commit | e9e9245c7c0ad6e3bda2b7600ff2bd845921a19d (patch) | |
tree | 58e78c757e57790d712ff368d511408d8427275b | |
parent | 12cc4d88f216ba3f684ddffb33d117baa7197726 (diff) | |
download | astro-e9e9245c7c0ad6e3bda2b7600ff2bd845921a19d.tar.gz astro-e9e9245c7c0ad6e3bda2b7600ff2bd845921a19d.tar.zst astro-e9e9245c7c0ad6e3bda2b7600ff2bd845921a19d.zip |
Repair server islands to work with client router (#13481)
* rerun server island scripts when used with client router
-rw-r--r-- | .changeset/tough-tires-kneel.md | 5 | ||||
-rw-r--r-- | packages/astro/src/runtime/server/render/server-islands.ts | 12 |
2 files changed, 11 insertions, 6 deletions
diff --git a/.changeset/tough-tires-kneel.md b/.changeset/tough-tires-kneel.md new file mode 100644 index 000000000..61d5afcad --- /dev/null +++ b/.changeset/tough-tires-kneel.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Makes server island work with the client router again. diff --git a/packages/astro/src/runtime/server/render/server-islands.ts b/packages/astro/src/runtime/server/render/server-islands.ts index 093254cd3..05617a7f6 100644 --- a/packages/astro/src/runtime/server/render/server-islands.ts +++ b/packages/astro/src/runtime/server/render/server-islands.ts @@ -105,7 +105,7 @@ export function renderServerIsland( ); } - destination.write(`<script async type="module" data-island-id="${hostId}"> + destination.write(`<script async type="module" data-astro-rerun data-island-id="${hostId}"> let script = document.querySelector('script[data-island-id="${hostId}"]'); ${ @@ -128,11 +128,11 @@ let response = await fetch('${serverIslandUrl}', { } if (script) { if( - response.status === 200 - && response.headers.has('content-type') + response.status === 200 + && response.headers.has('content-type') && response.headers.get('content-type').split(";")[0].trim() === 'text/html') { let html = await response.text(); - + // Swap! while(script.previousSibling && script.previousSibling.nodeType !== 8 && @@ -140,11 +140,11 @@ if (script) { script.previousSibling.remove(); } script.previousSibling?.remove(); - + let frag = document.createRange().createContextualFragment(html); script.before(frag); } - script.remove(); + script.remove(); // Prior to v5.4.2, this was the trick to force rerun of scripts. Keeping it to minimize change to the existing behavior. } </script>`); }, |