Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: insert ooo chunks in the proper order #372

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/quick-goats-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"preact-render-to-string": patch
---

fix: stop client runtime from being corrupted
fix: insert ooo chunks in the proper order
93 changes: 47 additions & 46 deletions src/lib/client.js
Original file line number Diff line number Diff line change
@@ -1,51 +1,52 @@
/* eslint-disable no-var, key-spacing, object-curly-spacing, prefer-arrow-callback, semi, keyword-spacing */

function initPreactIslandElement() {
class PreactIslandElement extends HTMLElement {
connectedCallback() {
var d = this;
if (!d.isConnected) return;

let i = this.getAttribute('data-target');
if (!i) return;

var s,
e,
c = document.createNodeIterator(document, 128);
while (c.nextNode()) {
let n = c.referenceNode;

if (n.data == 'preact-island:' + i) s = n;
else if (n.data == '/preact-island:' + i) e = n;
if (s && e) break;
}
if (s && e) {
var p = e.previousSibling;
while (p != s) {
if (!p || p == s) break;
e.parentNode.removeChild(p);
p = e.previousSibling;
}

requestAnimationFrame(() => {
for (let i = 0; i <= d.children.length; i++) {
const child = d.children[i];
e.parentNode.insertBefore(child, e);
}

d.parentNode.removeChild(d);
});
}
}
}

customElements.define('preact-island', PreactIslandElement);
}

const fn = initPreactIslandElement.toString();
const INIT_SCRIPT = fn
.slice(fn.indexOf('{') + 1, fn.lastIndexOf('}'))
.replace(/\n\s+/gm, '');
// function initPreactIslandElement() {
// class PreactIslandElement extends HTMLElement {
// connectedCallback() {
// var d = this;
// if (!d.isConnected) return;

// let i = this.getAttribute('data-target');
// if (!i) return;

// var s,
// e,
// c = document.createNodeIterator(document, 128);
// while (c.nextNode()) {
// let n = c.referenceNode;

// if (n.data == 'preact-island:' + i) s = n;
// else if (n.data == '/preact-island:' + i) e = n;
// if (s && e) break;
// }
// if (s && e) {
// requestAnimationFrame(() => {
// var p = e.previousSibling;
// while (p != s) {
// if (!p || p == s) break;
// e.parentNode.removeChild(p);
// p = e.previousSibling;
// }

// c = s;
// while (d.firstChild) {
// s = d.firstChild;
// d.removeChild(s);
// c.after(s);
// c = s;
// }

// d.parentNode.removeChild(d);
// });
// }
// }
// }

// customElements.define('preact-island', PreactIslandElement);
// }

// To modify the INIT_SCRIPT, uncomment the above code, modify it, and paste it into https://try.terser.org/.
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);`;

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