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

WIP: client hydration with portals #7

Open
wants to merge 1 commit into
base: next
Choose a base branch
from
Open
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
39 changes: 35 additions & 4 deletions iso/lib/preact.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createContext, h } from "preact";
import { h } from "preact";
import {
Fragment,
createPortal,
render,
useContext,
useEffect,
Expand All @@ -27,16 +28,45 @@ type ThenArg<T> = T extends PromiseLike<infer U> ? U : T;
const injectCache: Record<string, any> = {};

export const inject = function ({
logInjects = process.env.NODE_ENV !== "production",
logInjects = false, // process.env.NODE_ENV !== "production",
} = {}) {
let jsonElList = document.getElementsByClassName(`sosse-interactive`);
if (jsonElList.length === 0) {
return;
}

const componentPromises: Record<string, any> = {};
console.log(process.env.NODE_ENV);

const elVdom = [];
const componentPromises: Record<string, any> = {};
const jsonEls = Array.from(jsonElList);
for (const el of jsonEls) {
const id = el["dataset"].interactive;
const { component, ssr, suspense, lazy, wrapper } = injectCache[id];

const Component = _lazy(async () => ({ default: await component() }));
const props = JSON.parse(el.innerHTML);
let vdom = <Component {...props} />;

if (wrapper.length) {
for (const Wrapper of wrapper) {
vdom = <Wrapper>{vdom}</Wrapper>;
}
}

const Suspense = suspense;
vdom = <Suspense>{vdom}</Suspense>;

const containerEl = el.nextElementSibling;
vdom = createPortal(vdom, containerEl);

elVdom.push(vdom);
}

hydrate(h(Fragment, {}, ...elVdom), document.querySelector("#inject-root"));

/*

jsonEls.forEach(async (el) => {
const id = el["dataset"].interactive;
const { component, ssr, suspense, lazy, wrapper } = injectCache[id];
Expand All @@ -47,7 +77,7 @@ export const inject = function ({
(async () => {
let ComponentPromise = component();
return ASuspense
? _lazy(async () => ({ default: await ComponentPromise }))
?
: await ComponentPromise;
})();

Expand Down Expand Up @@ -83,6 +113,7 @@ export const inject = function ({
aInject();
}
});
*/
};

const defaultContainer = function ({ children }) {
Expand Down
4 changes: 3 additions & 1 deletion lib/ctx.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ export class Ctx {
);
html = html.replace(
"</body>",
Object.values(this._injectHtml.footer).join("") + "</body>"
Object.values(this._injectHtml.footer).join("") +
'<div id="inject-root"></div>' +
"</body>"
);

if (this._otion.enable) {
Expand Down