Skip to content

Commit

Permalink
feat: switch trigger async render from script to island (#831)
Browse files Browse the repository at this point in the history
* Add async render marker and utility function

* Move useAsyncRender to web file

* switch fresh async render trigger to an island
  • Loading branch information
igorbrasileiro authored Sep 16, 2024
1 parent e756771 commit 2ddc9ad
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 49 deletions.
45 changes: 2 additions & 43 deletions runtime/fresh/Bindings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,7 @@
import { Head, Partial } from "$fresh/runtime.ts";
import type { Framework } from "@deco/deco";
import { usePartialSection } from "@deco/deco/hooks";

const script = (id: string) => {
function init() {
const elem = document.getElementById(id);
const parent = elem?.parentElement;

if (elem == null || parent == null) {
console.error(
`Missing element of id ${id} or its parent element. Async rendering will NOT work properly`,
);
return;
}

const observeAndClose = (e: IntersectionObserverEntry[]) => {
e.forEach((entry) => {
if (entry.isIntersecting) {
elem.click();
observer.disconnect();
}
});
};
const observer = new IntersectionObserver(observeAndClose);
observer.observe(parent);
observeAndClose(observer.takeRecords());
}

if (document.readyState === "complete") {
init();
} else {
addEventListener("load", init);
}
};

const dataURI = (fn: typeof script, id: string) =>
btoa(
`decodeURIComponent(escape(${
unescape(encodeURIComponent(`((${fn})("${id}"))`))
}))`,
);
import DispatchAsyncRender from "./islands/DispatchAsyncRender.tsx";

const bindings: Framework = {
name: "fresh",
Expand All @@ -65,10 +27,7 @@ const bindings: Framework = {
id={btnId}
style={{ display: "none" }}
/>
<script
defer
src={`data:text/javascript;base64,${dataURI(script, btnId)}`}
/>
<DispatchAsyncRender id={btnId} />
{children}
</>
);
Expand Down
23 changes: 23 additions & 0 deletions runtime/fresh/islands/DispatchAsyncRender.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useEffect } from "preact/hooks";

interface Props {
id: string;
}

export default function DispatchAsyncRender({ id }: Props) {
useEffect(function clickOnAsyncRenderElements() {
const elem = document.getElementById(id);
const parent = elem?.parentElement;

if (elem == null || parent == null) {
console.error(
`Missing element of id ${id} or its parent element. Async rendering will NOT work properly`,
);
return;
}

elem.click();
}, []);

return <div data-dispatch-async-render />;
}
12 changes: 6 additions & 6 deletions runtime/fresh/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import { Deco, type PageData, type PageParams } from "@deco/deco";
import { framework as htmxFramework } from "@deco/deco/htmx";
import type { ComponentType } from "preact";
import framework from "./Bindings.tsx";

export interface Plugin {
name: string;
middlewares?: PluginMiddleware[];
routes?: PluginRoute[];
}
import type { Plugin } from "$fresh/server.ts";
export type { Plugin } from "$fresh/server.ts";

export interface PluginRoute {
/** A path in the format of a filename path without filetype */
Expand Down Expand Up @@ -110,5 +106,9 @@ export default function decoPlugin<TManifest extends AppManifest = AppManifest>(
catchAll,
{ ...catchAll, path: "/index" },
],
islands: {
baseLocation: import.meta.url,
paths: ["./islands/DispatchAsyncRender.tsx"],
},
};
}

0 comments on commit 2ddc9ad

Please sign in to comment.