Skip to content

Commit 7dd1349

Browse files
committed
Fix client code as well
1 parent d0b53e6 commit 7dd1349

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/lib/chunked.js

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ export async function renderToChunks(vnode, { context, onWrite, abortSignal }) {
3131
// Inserting the deferred <div hidden> wrapper after </html> is invalid HTML
3232
// and causes browsers to reject the content. Instead, we inject the deferred
3333
// content before the closing tags, then emit them last.
34-
const docSuffix = getDocumentClosingTags(shell);
35-
onWrite(docSuffix ? shell.slice(0, -docSuffix.length) : shell);
34+
const docSuffixIndex = getDocumentClosingTagsIndex(shell);
35+
onWrite(docSuffixIndex !== -1 ? shell.slice(0, docSuffixIndex) : shell);
3636
onWrite('<div hidden>');
3737
onWrite(createInitScript(len));
3838
// We should keep checking all promises
3939
await forkPromises(renderer);
4040
onWrite('</div>');
41-
if (docSuffix) onWrite(docSuffix);
41+
if (docSuffixIndex !== -1) onWrite(shell.slice(docSuffixIndex));
4242
} else {
4343
onWrite(shell);
4444
}
@@ -48,11 +48,10 @@ export async function renderToChunks(vnode, { context, onWrite, abortSignal }) {
4848
* If the shell ends with </body></html> (full document rendering), return that
4949
* suffix so it can be emitted *after* the deferred content, keeping the HTML valid.
5050
* @param {string} html
51-
* @returns {string | null}
51+
* @returns {number}
5252
*/
53-
function getDocumentClosingTags(html) {
54-
const match = html.match(/(<\/body>)?\s*<\/html>\s*/i);
55-
return match ? match[0] : null;
53+
function getDocumentClosingTagsIndex(html) {
54+
return html.indexOf('</html>');
5655
}
5756

5857
async function forkPromises(renderer) {

src/lib/client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
// else if (n.data == '/preact-island:' + i) e = n;
2020
// if (s && e) break;
2121
// }
22-
// if (s && e) {
22+
// if (s && e && s.parentNode !== document) {
2323
// requestAnimationFrame(() => {
2424
// var p = e.previousSibling;
2525
// while (p != s) {
@@ -46,7 +46,7 @@
4646
// }
4747

4848
// To modify the INIT_SCRIPT, uncomment the above code, modify it, and paste it into https://try.terser.org/.
49-
const INIT_SCRIPT = `class e extends HTMLElement{connectedCallback(){var e=this;if(!e.isConnected)return;let t=this.getAttribute("data-target");if(t){for(var r,a,i=document.createNodeIterator(document,128);i.nextNode();){let e=i.referenceNode;if(e.data=="preact-island:"+t?r=e:e.data=="/preact-island:"+t&&(a=e),r&&a)break}r&&a&&requestAnimationFrame((()=>{for(var t=a.previousSibling;t!=r&&t&&t!=r;)a.parentNode.removeChild(t),t=a.previousSibling;for(i=r;e.firstChild;)r=e.firstChild,e.removeChild(r),i.after(r),i=r;e.parentNode.removeChild(e)}))}}}customElements.define("preact-island",e);`;
49+
const INIT_SCRIPT = `class e extends HTMLElement{connectedCallback(){var e=this;if(!e.isConnected)return;let t=this.getAttribute("data-target");if(t){for(var r,a,i=document.createNodeIterator(document,128);i.nextNode();){let e=i.referenceNode;if(e.data=="preact-island:"+t?r=e:e.data=="/preact-island:"+t&&(a=e),r&&a)break}r&&a&&r.parentNode!==document&&requestAnimationFrame((()=>{for(var t=a.previousSibling;t!=r&&t&&t!=r;)a.parentNode.removeChild(t),t=a.previousSibling;for(i=r;e.firstChild;)r=e.firstChild,e.removeChild(r),i.after(r),i=r;e.parentNode.removeChild(e)}))}}}customElements.define("preact-island",e);`;
5050

5151
export function createInitScript() {
5252
return `<script>(function(){${INIT_SCRIPT}}())</script>`;

0 commit comments

Comments
 (0)