feat(server): multiple island exports (#1397)
This commit is contained in:
@@ -47,8 +47,11 @@ function isElementNode(node: Node): node is HTMLElement {
|
||||
return node.nodeType === Node.ELEMENT_NODE;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export function revive(islands: Record<string, ComponentType>, props: any[]) {
|
||||
export function revive(
|
||||
islands: Record<string, Record<string, ComponentType>>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
props: any[],
|
||||
) {
|
||||
_walkInner(
|
||||
islands,
|
||||
props,
|
||||
@@ -127,7 +130,7 @@ interface Marker {
|
||||
* fashion over an HTMLElement's children list.
|
||||
*/
|
||||
function _walkInner(
|
||||
islands: Record<string, ComponentType>,
|
||||
islands: Record<string, Record<string, ComponentType>>,
|
||||
// deno-lint-ignore no-explicit-any
|
||||
props: any[],
|
||||
markerStack: Marker[],
|
||||
@@ -253,7 +256,7 @@ function _walkInner(
|
||||
}
|
||||
} else if (comment.startsWith("frsh")) {
|
||||
// We're opening a new island
|
||||
const [id, n] = comment.slice(5).split(":");
|
||||
const [id, exportName, n] = comment.slice(5).split(":");
|
||||
const islandProps = props[Number(n)];
|
||||
|
||||
markerStack.push({
|
||||
@@ -262,7 +265,7 @@ function _walkInner(
|
||||
text: comment,
|
||||
kind: MarkerKind.Island,
|
||||
});
|
||||
const vnode = h(islands[id], islandProps);
|
||||
const vnode = h(islands[id][exportName], islandProps);
|
||||
vnodeStack.push(vnode);
|
||||
}
|
||||
} else if (isTextNode(sib)) {
|
||||
|
||||
+14
-7
@@ -286,14 +286,21 @@ export class ServerContext {
|
||||
}
|
||||
const path = url.substring(baseUrl.length).substring("islands".length);
|
||||
const baseRoute = path.substring(1, path.length - extname(path).length);
|
||||
const name = sanitizeIslandName(baseRoute);
|
||||
const id = name.toLowerCase();
|
||||
if (typeof module.default !== "function") {
|
||||
throw new TypeError(
|
||||
`Islands must default export a component ('${self}').`,
|
||||
);
|
||||
|
||||
for (const [exportName, exportedFunction] of Object.entries(module)) {
|
||||
if (typeof exportedFunction !== "function") {
|
||||
continue;
|
||||
}
|
||||
const name = sanitizeIslandName(baseRoute);
|
||||
const id = `${name}_${exportName}`.toLowerCase();
|
||||
islands.push({
|
||||
id,
|
||||
name,
|
||||
url,
|
||||
component: exportedFunction,
|
||||
exportName,
|
||||
});
|
||||
}
|
||||
islands.push({ id, name, url, component: module.default });
|
||||
}
|
||||
|
||||
const staticFiles: StaticFile[] = [];
|
||||
|
||||
@@ -350,8 +350,9 @@ export async function render<Data>(
|
||||
let islandRegistry = "";
|
||||
for (const island of ENCOUNTERED_ISLANDS) {
|
||||
const url = addImport(`island-${island.id}.js`);
|
||||
script += `import ${island.name} from "${url}";`;
|
||||
islandRegistry += `${island.id}:${island.name},`;
|
||||
script +=
|
||||
`import * as ${island.name}_${island.exportName} from "${url}";`;
|
||||
islandRegistry += `${island.id}:${island.name}_${island.exportName},`;
|
||||
}
|
||||
script += `revive({${islandRegistry}}, STATE[0]);`;
|
||||
}
|
||||
@@ -536,7 +537,7 @@ options.vnode = (vnode) => {
|
||||
|
||||
return wrapWithMarker(
|
||||
child,
|
||||
`frsh-${island.id}:${ISLAND_PROPS.length - 1}`,
|
||||
`frsh-${island.id}:${island.exportName}:${ISLAND_PROPS.length - 1}`,
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
+2
-1
@@ -262,7 +262,7 @@ export interface Middleware<State = Record<string, unknown>> {
|
||||
|
||||
export interface IslandModule {
|
||||
// deno-lint-ignore no-explicit-any
|
||||
default: ComponentType<any>;
|
||||
[key: string]: ComponentType<any>;
|
||||
}
|
||||
|
||||
export interface Island {
|
||||
@@ -270,6 +270,7 @@ export interface Island {
|
||||
name: string;
|
||||
url: string;
|
||||
component: ComponentType<unknown>;
|
||||
exportName: string;
|
||||
}
|
||||
|
||||
// --- PLUGINS ---
|
||||
|
||||
+56
-52
@@ -18,33 +18,35 @@ import * as $12 from "./routes/index.tsx";
|
||||
import * as $13 from "./routes/intercept.tsx";
|
||||
import * as $14 from "./routes/intercept_args.tsx";
|
||||
import * as $15 from "./routes/islands/index.tsx";
|
||||
import * as $16 from "./routes/islands/returning_null.tsx";
|
||||
import * as $17 from "./routes/islands/root_fragment.tsx";
|
||||
import * as $18 from "./routes/islands/root_fragment_conditional_first.tsx";
|
||||
import * as $19 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $20 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $21 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $22 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $23 from "./routes/layeredMdw/layer2/index.ts";
|
||||
import * as $24 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $25 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $26 from "./routes/middleware_root.ts";
|
||||
import * as $27 from "./routes/not_found.ts";
|
||||
import * as $28 from "./routes/params.tsx";
|
||||
import * as $29 from "./routes/props/[id].tsx";
|
||||
import * as $30 from "./routes/state-in-props/_middleware.ts";
|
||||
import * as $31 from "./routes/state-in-props/index.tsx";
|
||||
import * as $32 from "./routes/static.tsx";
|
||||
import * as $33 from "./routes/status_overwrite.tsx";
|
||||
import * as $34 from "./routes/wildcard.tsx";
|
||||
import * as $16 from "./routes/islands/multiple_island_exports.tsx";
|
||||
import * as $17 from "./routes/islands/returning_null.tsx";
|
||||
import * as $18 from "./routes/islands/root_fragment.tsx";
|
||||
import * as $19 from "./routes/islands/root_fragment_conditional_first.tsx";
|
||||
import * as $20 from "./routes/layeredMdw/_middleware.ts";
|
||||
import * as $21 from "./routes/layeredMdw/layer2-no-mw/without_mw.ts";
|
||||
import * as $22 from "./routes/layeredMdw/layer2/_middleware.ts";
|
||||
import * as $23 from "./routes/layeredMdw/layer2/abc.ts";
|
||||
import * as $24 from "./routes/layeredMdw/layer2/index.ts";
|
||||
import * as $25 from "./routes/layeredMdw/layer2/layer3/[id].ts";
|
||||
import * as $26 from "./routes/layeredMdw/layer2/layer3/_middleware.ts";
|
||||
import * as $27 from "./routes/middleware_root.ts";
|
||||
import * as $28 from "./routes/not_found.ts";
|
||||
import * as $29 from "./routes/params.tsx";
|
||||
import * as $30 from "./routes/props/[id].tsx";
|
||||
import * as $31 from "./routes/state-in-props/_middleware.ts";
|
||||
import * as $32 from "./routes/state-in-props/index.tsx";
|
||||
import * as $33 from "./routes/static.tsx";
|
||||
import * as $34 from "./routes/status_overwrite.tsx";
|
||||
import * as $35 from "./routes/wildcard.tsx";
|
||||
import * as $$0 from "./islands/Counter.tsx";
|
||||
import * as $$1 from "./islands/ReturningNull.tsx";
|
||||
import * as $$2 from "./islands/RootFragment.tsx";
|
||||
import * as $$3 from "./islands/RootFragmentWithConditionalFirst.tsx";
|
||||
import * as $$4 from "./islands/Test.tsx";
|
||||
import * as $$5 from "./islands/folder/Counter.tsx";
|
||||
import * as $$6 from "./islands/folder/subfolder/Counter.tsx";
|
||||
import * as $$7 from "./islands/kebab-case-counter-test.tsx";
|
||||
import * as $$1 from "./islands/MultipleCounters.tsx";
|
||||
import * as $$2 from "./islands/ReturningNull.tsx";
|
||||
import * as $$3 from "./islands/RootFragment.tsx";
|
||||
import * as $$4 from "./islands/RootFragmentWithConditionalFirst.tsx";
|
||||
import * as $$5 from "./islands/Test.tsx";
|
||||
import * as $$6 from "./islands/folder/Counter.tsx";
|
||||
import * as $$7 from "./islands/folder/subfolder/Counter.tsx";
|
||||
import * as $$8 from "./islands/kebab-case-counter-test.tsx";
|
||||
|
||||
const manifest = {
|
||||
routes: {
|
||||
@@ -64,35 +66,37 @@ const manifest = {
|
||||
"./routes/intercept.tsx": $13,
|
||||
"./routes/intercept_args.tsx": $14,
|
||||
"./routes/islands/index.tsx": $15,
|
||||
"./routes/islands/returning_null.tsx": $16,
|
||||
"./routes/islands/root_fragment.tsx": $17,
|
||||
"./routes/islands/root_fragment_conditional_first.tsx": $18,
|
||||
"./routes/layeredMdw/_middleware.ts": $19,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $20,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $21,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $22,
|
||||
"./routes/layeredMdw/layer2/index.ts": $23,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $24,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $25,
|
||||
"./routes/middleware_root.ts": $26,
|
||||
"./routes/not_found.ts": $27,
|
||||
"./routes/params.tsx": $28,
|
||||
"./routes/props/[id].tsx": $29,
|
||||
"./routes/state-in-props/_middleware.ts": $30,
|
||||
"./routes/state-in-props/index.tsx": $31,
|
||||
"./routes/static.tsx": $32,
|
||||
"./routes/status_overwrite.tsx": $33,
|
||||
"./routes/wildcard.tsx": $34,
|
||||
"./routes/islands/multiple_island_exports.tsx": $16,
|
||||
"./routes/islands/returning_null.tsx": $17,
|
||||
"./routes/islands/root_fragment.tsx": $18,
|
||||
"./routes/islands/root_fragment_conditional_first.tsx": $19,
|
||||
"./routes/layeredMdw/_middleware.ts": $20,
|
||||
"./routes/layeredMdw/layer2-no-mw/without_mw.ts": $21,
|
||||
"./routes/layeredMdw/layer2/_middleware.ts": $22,
|
||||
"./routes/layeredMdw/layer2/abc.ts": $23,
|
||||
"./routes/layeredMdw/layer2/index.ts": $24,
|
||||
"./routes/layeredMdw/layer2/layer3/[id].ts": $25,
|
||||
"./routes/layeredMdw/layer2/layer3/_middleware.ts": $26,
|
||||
"./routes/middleware_root.ts": $27,
|
||||
"./routes/not_found.ts": $28,
|
||||
"./routes/params.tsx": $29,
|
||||
"./routes/props/[id].tsx": $30,
|
||||
"./routes/state-in-props/_middleware.ts": $31,
|
||||
"./routes/state-in-props/index.tsx": $32,
|
||||
"./routes/static.tsx": $33,
|
||||
"./routes/status_overwrite.tsx": $34,
|
||||
"./routes/wildcard.tsx": $35,
|
||||
},
|
||||
islands: {
|
||||
"./islands/Counter.tsx": $$0,
|
||||
"./islands/ReturningNull.tsx": $$1,
|
||||
"./islands/RootFragment.tsx": $$2,
|
||||
"./islands/RootFragmentWithConditionalFirst.tsx": $$3,
|
||||
"./islands/Test.tsx": $$4,
|
||||
"./islands/folder/Counter.tsx": $$5,
|
||||
"./islands/folder/subfolder/Counter.tsx": $$6,
|
||||
"./islands/kebab-case-counter-test.tsx": $$7,
|
||||
"./islands/MultipleCounters.tsx": $$1,
|
||||
"./islands/ReturningNull.tsx": $$2,
|
||||
"./islands/RootFragment.tsx": $$3,
|
||||
"./islands/RootFragmentWithConditionalFirst.tsx": $$4,
|
||||
"./islands/Test.tsx": $$5,
|
||||
"./islands/folder/Counter.tsx": $$6,
|
||||
"./islands/folder/subfolder/Counter.tsx": $$7,
|
||||
"./islands/kebab-case-counter-test.tsx": $$8,
|
||||
},
|
||||
baseUrl: import.meta.url,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { Signal } from "@preact/signals";
|
||||
import { IS_BROWSER } from "$fresh/runtime.ts";
|
||||
|
||||
interface CounterProps {
|
||||
count: Signal<number>;
|
||||
id: string;
|
||||
}
|
||||
|
||||
export default function CounterZero(props: CounterProps) {
|
||||
return (
|
||||
<div id={props.id}>
|
||||
<p>{props.count}</p>
|
||||
<button
|
||||
id={`b-${props.id}`}
|
||||
onClick={() => props.count.value += 1}
|
||||
disabled={!IS_BROWSER}
|
||||
>
|
||||
+1
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function CounterOne(props: CounterProps) {
|
||||
return CounterZero(props);
|
||||
}
|
||||
|
||||
export function CounterTwo(props: CounterProps) {
|
||||
return CounterZero(props);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
import { useSignal } from "@preact/signals";
|
||||
import CounterZero from "../../islands/MultipleCounters.tsx";
|
||||
import { CounterOne, CounterTwo } from "../../islands/MultipleCounters.tsx";
|
||||
import SubfolderCounter from "../../islands/folder/subfolder/Counter.tsx";
|
||||
|
||||
export default function Home() {
|
||||
return (
|
||||
<div>
|
||||
<CounterZero id="counter0" count={useSignal(4)} />
|
||||
<CounterOne id="counter1" count={useSignal(3)} />
|
||||
<CounterTwo id="counter2" count={useSignal(10)} />
|
||||
<SubfolderCounter id="counter3" count={useSignal(4)} />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -59,6 +59,41 @@ Deno.test({
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
Deno.test({
|
||||
name: "multiple islands exported from one file",
|
||||
async fn(t) {
|
||||
await withPage(async (page, address) => {
|
||||
async function counterTest(counterId: string, originalValue: number) {
|
||||
const pElem = await page.waitForSelector(`#${counterId} > p`);
|
||||
|
||||
let value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue}`, `${counterId} first value`);
|
||||
|
||||
const buttonPlus = await page.$(`#b-${counterId}`);
|
||||
await buttonPlus?.click();
|
||||
|
||||
await delay(100);
|
||||
|
||||
value = await pElem?.evaluate((el) => el.textContent);
|
||||
assert(value === `${originalValue + 1}`, `${counterId} click`);
|
||||
}
|
||||
|
||||
await page.goto(`${address}/islands/multiple_island_exports`, {
|
||||
waitUntil: "networkidle2",
|
||||
});
|
||||
|
||||
await t.step("Ensure 3 islands on 1 page are revived", async () => {
|
||||
await counterTest("counter0", 4);
|
||||
await counterTest("counter1", 3);
|
||||
await counterTest("counter2", 10);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
sanitizeOps: false,
|
||||
sanitizeResources: false,
|
||||
});
|
||||
|
||||
function withPage(fn: (page: Page, address: string) => Promise<void>) {
|
||||
return withPageName("./tests/fixture/main.ts", fn);
|
||||
}
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@ Deno.test("/ page prerender", async () => {
|
||||
assertEquals(resp.headers.get("server"), "fresh test server");
|
||||
const body = await resp.text();
|
||||
assertStringIncludes(body, `<html lang="en">`);
|
||||
assertStringIncludes(body, "test.js");
|
||||
assertStringIncludes(body, "test_default.js");
|
||||
assertStringIncludes(body, "<p>Hello!</p>");
|
||||
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
|
||||
assertStringIncludes(body, `>{"v":[[{"message":"Hello!"}],[]]}</script>`);
|
||||
@@ -638,7 +638,7 @@ Deno.test("experimental Deno.serve", {
|
||||
assertEquals(resp.headers.get("server"), "fresh test server");
|
||||
const body = await resp.text();
|
||||
assertStringIncludes(body, `<html lang="en">`);
|
||||
assertStringIncludes(body, "test.js");
|
||||
assertStringIncludes(body, "test_default.js");
|
||||
assertStringIncludes(body, "<p>Hello!</p>");
|
||||
assertStringIncludes(body, "<p>Viewing JIT render.</p>");
|
||||
assertStringIncludes(body, `>{"v":[[{"message":"Hello!"}],[]]}</script>`);
|
||||
|
||||
Reference in New Issue
Block a user