diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index d708a71c..a395d1c8 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -577,6 +577,62 @@ async function fetchPartials(url: URL, realUrl: URL, init?: RequestInit) { }); } +function collectPartials( + encounteredPartials: RenderRequest[], + islands: IslandRegistry, + state: SerializedState, + node: Node, +) { + let startNode = null; + let sib: ChildNode | null = node.firstChild; + let partialCount = 0; + while (sib !== null) { + if (isCommentNode(sib)) { + const comment = sib.data; + if (comment.startsWith("frsh-partial")) { + startNode = sib; + partialCount++; + } else if (comment.startsWith("/frsh-partial")) { + partialCount--; + // Create a fake DOM node that spans the partial we discovered. + // We need to include the partial markers itself for _walkInner + // to register them. + const rootFrag = { + _frshRootFrag: true, + nodeType: 1, + nextSibling: null, + firstChild: startNode, + parentNode: node, + get childNodes() { + const children: ChildNode[] = [startNode!]; + let node = startNode!; + while ((node = node.nextSibling) !== null) { + children.push(node); + } + + return children; + }, + // deno-lint-ignore no-explicit-any + } as any as HTMLElement; + + _walkInner( + islands, + state[0] ?? [], + [], + [h(Fragment, null)], + rootFrag, + encounteredPartials, + ); + } + } else if (partialCount === 0 && isElementNode(sib)) { + // Do not recurse if we know that we are inisde a partial + collectPartials(encounteredPartials, islands, state, sib); + } + + sib = sib.nextSibling; + } +} + /** * Apply partials from a HTML response */ @@ -644,48 +700,7 @@ export async function applyPartials(res: Response): Promise { // Collect all partials and build up the vnode tree const encounteredPartials: RenderRequest[] = []; - let startNode = null; - let sib: ChildNode | null = doc.body.firstChild; - while (sib !== null) { - if (isCommentNode(sib)) { - const comment = sib.data; - if (comment.startsWith("frsh-partial")) { - startNode = sib; - } else if (comment.startsWith("/frsh-partial")) { - // Create a fake DOM node that spans the partial we discovered. - // We need to include the partial markers itself for _walkInner - // to register them. - const rootFrag = { - _frshRootFrag: true, - nodeType: 1, - nextSibling: null, - firstChild: startNode, - parentNode: doc.body, - get childNodes() { - const children: ChildNode[] = [startNode!]; - let node = startNode!; - while ((node = node.nextSibling) !== null) { - children.push(node); - } - - return children; - }, - // deno-lint-ignore no-explicit-any - } as any as HTMLElement; - - _walkInner( - islands, - state[0] ?? [], - [], - [h(Fragment, null)], - rootFrag, - encounteredPartials, - ); - } - } - - sib = sib.nextSibling; - } + collectPartials(encounteredPartials, islands, state, doc.body); if (encounteredPartials.length === 0) { throw new Error( diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index 7e3dfbc4..55e0f6a2 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -24,58 +24,61 @@ import * as $18 from "./routes/client_nav_opt_out/injected.tsx"; import * as $19 from "./routes/client_nav_opt_out/page-a.tsx"; import * as $20 from "./routes/client_nav_opt_out/page-b.tsx"; import * as $21 from "./routes/client_nav_opt_out/page-c.tsx"; -import * as $22 from "./routes/form/index.tsx"; -import * as $23 from "./routes/form/injected.tsx"; -import * as $24 from "./routes/form/update.tsx"; -import * as $25 from "./routes/fragment_nav.tsx"; -import * as $26 from "./routes/index.tsx"; -import * as $27 from "./routes/island_instance/index.tsx"; -import * as $28 from "./routes/island_instance/injected.tsx"; -import * as $29 from "./routes/island_instance/partial.tsx"; -import * as $30 from "./routes/island_instance/partial_remove.tsx"; -import * as $31 from "./routes/island_instance/partial_replace.tsx"; -import * as $32 from "./routes/island_instance_multiple/index.tsx"; -import * as $33 from "./routes/island_instance_multiple/injected.tsx"; -import * as $34 from "./routes/island_instance_multiple/partial.tsx"; -import * as $35 from "./routes/island_instance_multiple/partial_both.tsx"; -import * as $36 from "./routes/island_instance_nested/index.tsx"; -import * as $37 from "./routes/island_instance_nested/injected.tsx"; -import * as $38 from "./routes/island_instance_nested/partial.tsx"; -import * as $39 from "./routes/island_instance_nested/replace.tsx"; -import * as $40 from "./routes/island_props/index.tsx"; -import * as $41 from "./routes/island_props/injected.tsx"; -import * as $42 from "./routes/island_props/partial.tsx"; -import * as $43 from "./routes/island_props_signals/index.tsx"; -import * as $44 from "./routes/island_props_signals/injected.tsx"; -import * as $45 from "./routes/island_props_signals/partial.tsx"; -import * as $46 from "./routes/keys/index.tsx"; -import * as $47 from "./routes/keys/injected.tsx"; -import * as $48 from "./routes/keys/swap.tsx"; -import * as $49 from "./routes/keys_components/index.tsx"; -import * as $50 from "./routes/keys_components/injected.tsx"; -import * as $51 from "./routes/keys_components/swap.tsx"; -import * as $52 from "./routes/keys_dom/index.tsx"; -import * as $53 from "./routes/keys_dom/injected.tsx"; -import * as $54 from "./routes/keys_dom/swap.tsx"; -import * as $55 from "./routes/keys_outside/index.tsx"; -import * as $56 from "./routes/loading/index.tsx"; -import * as $57 from "./routes/loading/injected.tsx"; -import * as $58 from "./routes/loading/update.tsx"; -import * as $59 from "./routes/missing_partial/index.tsx"; -import * as $60 from "./routes/missing_partial/injected.tsx"; -import * as $61 from "./routes/missing_partial/update.tsx"; -import * as $62 from "./routes/mode/append.tsx"; -import * as $63 from "./routes/mode/index.tsx"; -import * as $64 from "./routes/mode/injected.tsx"; -import * as $65 from "./routes/mode/prepend.tsx"; -import * as $66 from "./routes/mode/replace.tsx"; -import * as $67 from "./routes/no_islands/index.tsx"; -import * as $68 from "./routes/no_islands/injected.tsx"; -import * as $69 from "./routes/no_islands/update.tsx"; -import * as $70 from "./routes/no_partial_response/index.tsx"; -import * as $71 from "./routes/no_partial_response/injected.tsx"; -import * as $72 from "./routes/no_partial_response/update.tsx"; -import * as $73 from "./routes/partial_slot_inside_island.tsx"; +import * as $22 from "./routes/deep_partial/index.tsx"; +import * as $23 from "./routes/deep_partial/injected.tsx"; +import * as $24 from "./routes/deep_partial/update.tsx"; +import * as $25 from "./routes/form/index.tsx"; +import * as $26 from "./routes/form/injected.tsx"; +import * as $27 from "./routes/form/update.tsx"; +import * as $28 from "./routes/fragment_nav.tsx"; +import * as $29 from "./routes/index.tsx"; +import * as $30 from "./routes/island_instance/index.tsx"; +import * as $31 from "./routes/island_instance/injected.tsx"; +import * as $32 from "./routes/island_instance/partial.tsx"; +import * as $33 from "./routes/island_instance/partial_remove.tsx"; +import * as $34 from "./routes/island_instance/partial_replace.tsx"; +import * as $35 from "./routes/island_instance_multiple/index.tsx"; +import * as $36 from "./routes/island_instance_multiple/injected.tsx"; +import * as $37 from "./routes/island_instance_multiple/partial.tsx"; +import * as $38 from "./routes/island_instance_multiple/partial_both.tsx"; +import * as $39 from "./routes/island_instance_nested/index.tsx"; +import * as $40 from "./routes/island_instance_nested/injected.tsx"; +import * as $41 from "./routes/island_instance_nested/partial.tsx"; +import * as $42 from "./routes/island_instance_nested/replace.tsx"; +import * as $43 from "./routes/island_props/index.tsx"; +import * as $44 from "./routes/island_props/injected.tsx"; +import * as $45 from "./routes/island_props/partial.tsx"; +import * as $46 from "./routes/island_props_signals/index.tsx"; +import * as $47 from "./routes/island_props_signals/injected.tsx"; +import * as $48 from "./routes/island_props_signals/partial.tsx"; +import * as $49 from "./routes/keys/index.tsx"; +import * as $50 from "./routes/keys/injected.tsx"; +import * as $51 from "./routes/keys/swap.tsx"; +import * as $52 from "./routes/keys_components/index.tsx"; +import * as $53 from "./routes/keys_components/injected.tsx"; +import * as $54 from "./routes/keys_components/swap.tsx"; +import * as $55 from "./routes/keys_dom/index.tsx"; +import * as $56 from "./routes/keys_dom/injected.tsx"; +import * as $57 from "./routes/keys_dom/swap.tsx"; +import * as $58 from "./routes/keys_outside/index.tsx"; +import * as $59 from "./routes/loading/index.tsx"; +import * as $60 from "./routes/loading/injected.tsx"; +import * as $61 from "./routes/loading/update.tsx"; +import * as $62 from "./routes/missing_partial/index.tsx"; +import * as $63 from "./routes/missing_partial/injected.tsx"; +import * as $64 from "./routes/missing_partial/update.tsx"; +import * as $65 from "./routes/mode/append.tsx"; +import * as $66 from "./routes/mode/index.tsx"; +import * as $67 from "./routes/mode/injected.tsx"; +import * as $68 from "./routes/mode/prepend.tsx"; +import * as $69 from "./routes/mode/replace.tsx"; +import * as $70 from "./routes/no_islands/index.tsx"; +import * as $71 from "./routes/no_islands/injected.tsx"; +import * as $72 from "./routes/no_islands/update.tsx"; +import * as $73 from "./routes/no_partial_response/index.tsx"; +import * as $74 from "./routes/no_partial_response/injected.tsx"; +import * as $75 from "./routes/no_partial_response/update.tsx"; +import * as $76 from "./routes/partial_slot_inside_island.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/CounterA.tsx"; import * as $$2 from "./islands/CounterB.tsx"; @@ -115,58 +118,61 @@ const manifest = { "./routes/client_nav_opt_out/page-a.tsx": $19, "./routes/client_nav_opt_out/page-b.tsx": $20, "./routes/client_nav_opt_out/page-c.tsx": $21, - "./routes/form/index.tsx": $22, - "./routes/form/injected.tsx": $23, - "./routes/form/update.tsx": $24, - "./routes/fragment_nav.tsx": $25, - "./routes/index.tsx": $26, - "./routes/island_instance/index.tsx": $27, - "./routes/island_instance/injected.tsx": $28, - "./routes/island_instance/partial.tsx": $29, - "./routes/island_instance/partial_remove.tsx": $30, - "./routes/island_instance/partial_replace.tsx": $31, - "./routes/island_instance_multiple/index.tsx": $32, - "./routes/island_instance_multiple/injected.tsx": $33, - "./routes/island_instance_multiple/partial.tsx": $34, - "./routes/island_instance_multiple/partial_both.tsx": $35, - "./routes/island_instance_nested/index.tsx": $36, - "./routes/island_instance_nested/injected.tsx": $37, - "./routes/island_instance_nested/partial.tsx": $38, - "./routes/island_instance_nested/replace.tsx": $39, - "./routes/island_props/index.tsx": $40, - "./routes/island_props/injected.tsx": $41, - "./routes/island_props/partial.tsx": $42, - "./routes/island_props_signals/index.tsx": $43, - "./routes/island_props_signals/injected.tsx": $44, - "./routes/island_props_signals/partial.tsx": $45, - "./routes/keys/index.tsx": $46, - "./routes/keys/injected.tsx": $47, - "./routes/keys/swap.tsx": $48, - "./routes/keys_components/index.tsx": $49, - "./routes/keys_components/injected.tsx": $50, - "./routes/keys_components/swap.tsx": $51, - "./routes/keys_dom/index.tsx": $52, - "./routes/keys_dom/injected.tsx": $53, - "./routes/keys_dom/swap.tsx": $54, - "./routes/keys_outside/index.tsx": $55, - "./routes/loading/index.tsx": $56, - "./routes/loading/injected.tsx": $57, - "./routes/loading/update.tsx": $58, - "./routes/missing_partial/index.tsx": $59, - "./routes/missing_partial/injected.tsx": $60, - "./routes/missing_partial/update.tsx": $61, - "./routes/mode/append.tsx": $62, - "./routes/mode/index.tsx": $63, - "./routes/mode/injected.tsx": $64, - "./routes/mode/prepend.tsx": $65, - "./routes/mode/replace.tsx": $66, - "./routes/no_islands/index.tsx": $67, - "./routes/no_islands/injected.tsx": $68, - "./routes/no_islands/update.tsx": $69, - "./routes/no_partial_response/index.tsx": $70, - "./routes/no_partial_response/injected.tsx": $71, - "./routes/no_partial_response/update.tsx": $72, - "./routes/partial_slot_inside_island.tsx": $73, + "./routes/deep_partial/index.tsx": $22, + "./routes/deep_partial/injected.tsx": $23, + "./routes/deep_partial/update.tsx": $24, + "./routes/form/index.tsx": $25, + "./routes/form/injected.tsx": $26, + "./routes/form/update.tsx": $27, + "./routes/fragment_nav.tsx": $28, + "./routes/index.tsx": $29, + "./routes/island_instance/index.tsx": $30, + "./routes/island_instance/injected.tsx": $31, + "./routes/island_instance/partial.tsx": $32, + "./routes/island_instance/partial_remove.tsx": $33, + "./routes/island_instance/partial_replace.tsx": $34, + "./routes/island_instance_multiple/index.tsx": $35, + "./routes/island_instance_multiple/injected.tsx": $36, + "./routes/island_instance_multiple/partial.tsx": $37, + "./routes/island_instance_multiple/partial_both.tsx": $38, + "./routes/island_instance_nested/index.tsx": $39, + "./routes/island_instance_nested/injected.tsx": $40, + "./routes/island_instance_nested/partial.tsx": $41, + "./routes/island_instance_nested/replace.tsx": $42, + "./routes/island_props/index.tsx": $43, + "./routes/island_props/injected.tsx": $44, + "./routes/island_props/partial.tsx": $45, + "./routes/island_props_signals/index.tsx": $46, + "./routes/island_props_signals/injected.tsx": $47, + "./routes/island_props_signals/partial.tsx": $48, + "./routes/keys/index.tsx": $49, + "./routes/keys/injected.tsx": $50, + "./routes/keys/swap.tsx": $51, + "./routes/keys_components/index.tsx": $52, + "./routes/keys_components/injected.tsx": $53, + "./routes/keys_components/swap.tsx": $54, + "./routes/keys_dom/index.tsx": $55, + "./routes/keys_dom/injected.tsx": $56, + "./routes/keys_dom/swap.tsx": $57, + "./routes/keys_outside/index.tsx": $58, + "./routes/loading/index.tsx": $59, + "./routes/loading/injected.tsx": $60, + "./routes/loading/update.tsx": $61, + "./routes/missing_partial/index.tsx": $62, + "./routes/missing_partial/injected.tsx": $63, + "./routes/missing_partial/update.tsx": $64, + "./routes/mode/append.tsx": $65, + "./routes/mode/index.tsx": $66, + "./routes/mode/injected.tsx": $67, + "./routes/mode/prepend.tsx": $68, + "./routes/mode/replace.tsx": $69, + "./routes/no_islands/index.tsx": $70, + "./routes/no_islands/injected.tsx": $71, + "./routes/no_islands/update.tsx": $72, + "./routes/no_partial_response/index.tsx": $73, + "./routes/no_partial_response/injected.tsx": $74, + "./routes/no_partial_response/update.tsx": $75, + "./routes/partial_slot_inside_island.tsx": $76, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture_partials/routes/deep_partial/index.tsx b/tests/fixture_partials/routes/deep_partial/index.tsx new file mode 100644 index 00000000..75667206 --- /dev/null +++ b/tests/fixture_partials/routes/deep_partial/index.tsx @@ -0,0 +1,27 @@ +import { Partial } from "$fresh/runtime.ts"; +import { Fader } from "../../islands/Fader.tsx"; +import { Logger } from "../../islands/Logger.tsx"; + +export default function SlotDemo() { + return ( +
+ + + +

initial

+
+
+
+

+ + update + +

+
+    
+ ); +} diff --git a/tests/fixture_partials/routes/deep_partial/injected.tsx b/tests/fixture_partials/routes/deep_partial/injected.tsx new file mode 100644 index 00000000..f1fd4145 --- /dev/null +++ b/tests/fixture_partials/routes/deep_partial/injected.tsx @@ -0,0 +1,10 @@ +import { defineRoute } from "$fresh/src/server/defines.ts"; + +export default defineRoute(() => { + return new Response("", { + status: 302, + headers: { + Location: "/deep_partial", + }, + }); +}); diff --git a/tests/fixture_partials/routes/deep_partial/update.tsx b/tests/fixture_partials/routes/deep_partial/update.tsx new file mode 100644 index 00000000..2dd2665c --- /dev/null +++ b/tests/fixture_partials/routes/deep_partial/update.tsx @@ -0,0 +1,26 @@ +import { defineRoute } from "$fresh/src/server/defines.ts"; +import { RouteConfig } from "$fresh/server.ts"; +import { Partial } from "$fresh/runtime.ts"; +import { Fader } from "../../islands/Fader.tsx"; +import { Logger } from "../../islands/Logger.tsx"; + +export const config: RouteConfig = { + skipAppWrapper: true, + skipInheritedLayouts: true, +}; + +export default defineRoute((req, ctx) => { + return ( +
+
+ + + +

updated

+
+
+
+
+
+ ); +}); diff --git a/tests/partials_test.ts b/tests/partials_test.ts index f7118eb2..812c33b2 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -115,6 +115,19 @@ Deno.test("injects content with island and keeps island instance alive", async ( ); }); +Deno.test("finds partial nested in response", async () => { + await withPageName( + "./tests/fixture_partials/main.ts", + async (page, address) => { + await page.goto(`${address}/deep_partial`); + await page.waitForSelector(".status"); + + await page.click(".update-link"); + await waitForText(page, ".status-updated", "updated"); + }, + ); +}); + Deno.test("unmounts island", async () => { await withPageName( "./tests/fixture_partials/main.ts",