From 0192316785bc4aef4aac15ad9069031503fa9b29 Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Sun, 15 Oct 2023 08:41:13 +0200 Subject: [PATCH] fix: unable to opt out of nested client nav (#1940) --- src/runtime/entrypoints/main.ts | 18 +- src/server/rendering/preact_hooks.ts | 2 +- tests/fixture_partials/fresh.gen.ts | 294 +++++++++--------- .../routes/client_nav_both/_layout.tsx | 50 +++ .../routes/client_nav_both/index.tsx | 21 ++ .../routes/client_nav_both/page-a.tsx | 17 + .../routes/client_nav_both/page-b.tsx | 20 ++ .../routes/client_nav_both/page-c.tsx | 17 + tests/partials_test.ts | 59 +++- 9 files changed, 347 insertions(+), 151 deletions(-) create mode 100644 tests/fixture_partials/routes/client_nav_both/_layout.tsx create mode 100644 tests/fixture_partials/routes/client_nav_both/index.tsx create mode 100644 tests/fixture_partials/routes/client_nav_both/page-a.tsx create mode 100644 tests/fixture_partials/routes/client_nav_both/page-b.tsx create mode 100644 tests/fixture_partials/routes/client_nav_both/page-c.tsx diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index 7bad8bff..105be8b5 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -872,8 +872,14 @@ export interface FreshHistoryState { scrollY: number; } -function checkClientNavEnabled() { - return document.querySelector(`[${CLIENT_NAV_ATTR}]`) !== null; +function checkClientNavEnabled(el: HTMLElement | null) { + if (el === null) { + return document.querySelector(`[${CLIENT_NAV_ATTR}="true"]`) !== null; + } + + const setting = el.closest(`[${CLIENT_NAV_ATTR}]`); + if (setting === null) return false; + return setting.getAttribute(CLIENT_NAV_ATTR) === "true"; } // Keep track of history state to apply forward or backward animations @@ -917,8 +923,7 @@ document.addEventListener("click", async (e) => { // we're doing a fragment navigation. if ( el.getAttribute("href")?.startsWith("#") || - !checkClientNavEnabled() || - el.closest(`[${CLIENT_NAV_ATTR}="true"]`) === null + !checkClientNavEnabled(el) ) { return; } @@ -978,8 +983,7 @@ document.addEventListener("click", async (e) => { // Check if the user opted out of client side navigation. if ( partial === null || - !checkClientNavEnabled() || - button.closest(`[${CLIENT_NAV_ATTR}="true"]`) === null + !checkClientNavEnabled(button) ) { return; } @@ -1009,7 +1013,7 @@ addEventListener("popstate", async (e) => { const nextIdx = state.index ?? index + 1; index = nextIdx; - if (!checkClientNavEnabled()) { + if (!checkClientNavEnabled(null)) { location.reload(); return; } diff --git a/src/server/rendering/preact_hooks.ts b/src/server/rendering/preact_hooks.ts index 455c6623..0347d619 100644 --- a/src/server/rendering/preact_hooks.ts +++ b/src/server/rendering/preact_hooks.ts @@ -205,7 +205,7 @@ options.vnode = (vnode) => { } if (typeof props[CLIENT_NAV_ATTR] === "boolean") { - props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : null; + props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : "false"; } } else if ( current && typeof vnode.type === "function" && vnode.type !== Fragment && diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index 7b318dca..285133b8 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -20,77 +20,82 @@ import * as $14 from "./routes/client_nav/injected.tsx"; import * as $15 from "./routes/client_nav/page-a.tsx"; import * as $16 from "./routes/client_nav/page-b.tsx"; import * as $17 from "./routes/client_nav/page-c.tsx"; -import * as $18 from "./routes/client_nav_opt_out/_layout.tsx"; -import * as $19 from "./routes/client_nav_opt_out/index.tsx"; -import * as $20 from "./routes/client_nav_opt_out/injected.tsx"; -import * as $21 from "./routes/client_nav_opt_out/page-a.tsx"; -import * as $22 from "./routes/client_nav_opt_out/page-b.tsx"; -import * as $23 from "./routes/client_nav_opt_out/page-c.tsx"; -import * as $24 from "./routes/deep_partial/index.tsx"; -import * as $25 from "./routes/deep_partial/injected.tsx"; -import * as $26 from "./routes/deep_partial/update.tsx"; -import * as $27 from "./routes/form/index.tsx"; -import * as $28 from "./routes/form/injected.tsx"; -import * as $29 from "./routes/form/update.tsx"; -import * as $30 from "./routes/fragment_nav.tsx"; -import * as $31 from "./routes/fragment_nav_scroll.tsx"; -import * as $32 from "./routes/head_merge/duplicate.tsx"; -import * as $33 from "./routes/head_merge/index.tsx"; -import * as $34 from "./routes/head_merge/injected.tsx"; -import * as $35 from "./routes/head_merge/update.tsx"; -import * as $36 from "./routes/index.tsx"; -import * as $37 from "./routes/island_instance/index.tsx"; -import * as $38 from "./routes/island_instance/injected.tsx"; -import * as $39 from "./routes/island_instance/partial.tsx"; -import * as $40 from "./routes/island_instance/partial_remove.tsx"; -import * as $41 from "./routes/island_instance/partial_replace.tsx"; -import * as $42 from "./routes/island_instance_multiple/index.tsx"; -import * as $43 from "./routes/island_instance_multiple/injected.tsx"; -import * as $44 from "./routes/island_instance_multiple/partial.tsx"; -import * as $45 from "./routes/island_instance_multiple/partial_both.tsx"; -import * as $46 from "./routes/island_instance_nested/index.tsx"; -import * as $47 from "./routes/island_instance_nested/injected.tsx"; -import * as $48 from "./routes/island_instance_nested/partial.tsx"; -import * as $49 from "./routes/island_instance_nested/replace.tsx"; -import * as $50 from "./routes/island_props/index.tsx"; -import * as $51 from "./routes/island_props/injected.tsx"; -import * as $52 from "./routes/island_props/partial.tsx"; -import * as $53 from "./routes/island_props_signals/index.tsx"; -import * as $54 from "./routes/island_props_signals/injected.tsx"; -import * as $55 from "./routes/island_props_signals/partial.tsx"; -import * as $56 from "./routes/keys/index.tsx"; -import * as $57 from "./routes/keys/injected.tsx"; -import * as $58 from "./routes/keys/swap.tsx"; -import * as $59 from "./routes/keys_components/index.tsx"; -import * as $60 from "./routes/keys_components/injected.tsx"; -import * as $61 from "./routes/keys_components/swap.tsx"; -import * as $62 from "./routes/keys_confusion/index.tsx"; -import * as $63 from "./routes/keys_dom/index.tsx"; -import * as $64 from "./routes/keys_dom/injected.tsx"; -import * as $65 from "./routes/keys_dom/swap.tsx"; -import * as $66 from "./routes/keys_outside/index.tsx"; -import * as $67 from "./routes/loading/index.tsx"; -import * as $68 from "./routes/loading/injected.tsx"; -import * as $69 from "./routes/loading/update.tsx"; -import * as $70 from "./routes/missing_partial/index.tsx"; -import * as $71 from "./routes/missing_partial/injected.tsx"; -import * as $72 from "./routes/missing_partial/update.tsx"; -import * as $73 from "./routes/mode/append.tsx"; -import * as $74 from "./routes/mode/index.tsx"; -import * as $75 from "./routes/mode/injected.tsx"; -import * as $76 from "./routes/mode/prepend.tsx"; -import * as $77 from "./routes/mode/replace.tsx"; -import * as $78 from "./routes/no_islands/index.tsx"; -import * as $79 from "./routes/no_islands/injected.tsx"; -import * as $80 from "./routes/no_islands/update.tsx"; -import * as $81 from "./routes/no_partial_response/index.tsx"; -import * as $82 from "./routes/no_partial_response/injected.tsx"; -import * as $83 from "./routes/no_partial_response/update.tsx"; -import * as $84 from "./routes/partial_slot_inside_island.tsx"; -import * as $85 from "./routes/relative_link/index.tsx"; -import * as $86 from "./routes/scroll_restoration/index.tsx"; -import * as $87 from "./routes/scroll_restoration/injected.tsx"; -import * as $88 from "./routes/scroll_restoration/update.tsx"; +import * as $18 from "./routes/client_nav_both/_layout.tsx"; +import * as $19 from "./routes/client_nav_both/index.tsx"; +import * as $20 from "./routes/client_nav_both/page-a.tsx"; +import * as $21 from "./routes/client_nav_both/page-b.tsx"; +import * as $22 from "./routes/client_nav_both/page-c.tsx"; +import * as $23 from "./routes/client_nav_opt_out/_layout.tsx"; +import * as $24 from "./routes/client_nav_opt_out/index.tsx"; +import * as $25 from "./routes/client_nav_opt_out/injected.tsx"; +import * as $26 from "./routes/client_nav_opt_out/page-a.tsx"; +import * as $27 from "./routes/client_nav_opt_out/page-b.tsx"; +import * as $28 from "./routes/client_nav_opt_out/page-c.tsx"; +import * as $29 from "./routes/deep_partial/index.tsx"; +import * as $30 from "./routes/deep_partial/injected.tsx"; +import * as $31 from "./routes/deep_partial/update.tsx"; +import * as $32 from "./routes/form/index.tsx"; +import * as $33 from "./routes/form/injected.tsx"; +import * as $34 from "./routes/form/update.tsx"; +import * as $35 from "./routes/fragment_nav.tsx"; +import * as $36 from "./routes/fragment_nav_scroll.tsx"; +import * as $37 from "./routes/head_merge/duplicate.tsx"; +import * as $38 from "./routes/head_merge/index.tsx"; +import * as $39 from "./routes/head_merge/injected.tsx"; +import * as $40 from "./routes/head_merge/update.tsx"; +import * as $41 from "./routes/index.tsx"; +import * as $42 from "./routes/island_instance/index.tsx"; +import * as $43 from "./routes/island_instance/injected.tsx"; +import * as $44 from "./routes/island_instance/partial.tsx"; +import * as $45 from "./routes/island_instance/partial_remove.tsx"; +import * as $46 from "./routes/island_instance/partial_replace.tsx"; +import * as $47 from "./routes/island_instance_multiple/index.tsx"; +import * as $48 from "./routes/island_instance_multiple/injected.tsx"; +import * as $49 from "./routes/island_instance_multiple/partial.tsx"; +import * as $50 from "./routes/island_instance_multiple/partial_both.tsx"; +import * as $51 from "./routes/island_instance_nested/index.tsx"; +import * as $52 from "./routes/island_instance_nested/injected.tsx"; +import * as $53 from "./routes/island_instance_nested/partial.tsx"; +import * as $54 from "./routes/island_instance_nested/replace.tsx"; +import * as $55 from "./routes/island_props/index.tsx"; +import * as $56 from "./routes/island_props/injected.tsx"; +import * as $57 from "./routes/island_props/partial.tsx"; +import * as $58 from "./routes/island_props_signals/index.tsx"; +import * as $59 from "./routes/island_props_signals/injected.tsx"; +import * as $60 from "./routes/island_props_signals/partial.tsx"; +import * as $61 from "./routes/keys/index.tsx"; +import * as $62 from "./routes/keys/injected.tsx"; +import * as $63 from "./routes/keys/swap.tsx"; +import * as $64 from "./routes/keys_components/index.tsx"; +import * as $65 from "./routes/keys_components/injected.tsx"; +import * as $66 from "./routes/keys_components/swap.tsx"; +import * as $67 from "./routes/keys_confusion/index.tsx"; +import * as $68 from "./routes/keys_dom/index.tsx"; +import * as $69 from "./routes/keys_dom/injected.tsx"; +import * as $70 from "./routes/keys_dom/swap.tsx"; +import * as $71 from "./routes/keys_outside/index.tsx"; +import * as $72 from "./routes/loading/index.tsx"; +import * as $73 from "./routes/loading/injected.tsx"; +import * as $74 from "./routes/loading/update.tsx"; +import * as $75 from "./routes/missing_partial/index.tsx"; +import * as $76 from "./routes/missing_partial/injected.tsx"; +import * as $77 from "./routes/missing_partial/update.tsx"; +import * as $78 from "./routes/mode/append.tsx"; +import * as $79 from "./routes/mode/index.tsx"; +import * as $80 from "./routes/mode/injected.tsx"; +import * as $81 from "./routes/mode/prepend.tsx"; +import * as $82 from "./routes/mode/replace.tsx"; +import * as $83 from "./routes/no_islands/index.tsx"; +import * as $84 from "./routes/no_islands/injected.tsx"; +import * as $85 from "./routes/no_islands/update.tsx"; +import * as $86 from "./routes/no_partial_response/index.tsx"; +import * as $87 from "./routes/no_partial_response/injected.tsx"; +import * as $88 from "./routes/no_partial_response/update.tsx"; +import * as $89 from "./routes/partial_slot_inside_island.tsx"; +import * as $90 from "./routes/relative_link/index.tsx"; +import * as $91 from "./routes/scroll_restoration/index.tsx"; +import * as $92 from "./routes/scroll_restoration/injected.tsx"; +import * as $93 from "./routes/scroll_restoration/update.tsx"; import * as $$0 from "./islands/Counter.tsx"; import * as $$1 from "./islands/CounterA.tsx"; import * as $$2 from "./islands/CounterB.tsx"; @@ -127,77 +132,82 @@ const manifest = { "./routes/client_nav/page-a.tsx": $15, "./routes/client_nav/page-b.tsx": $16, "./routes/client_nav/page-c.tsx": $17, - "./routes/client_nav_opt_out/_layout.tsx": $18, - "./routes/client_nav_opt_out/index.tsx": $19, - "./routes/client_nav_opt_out/injected.tsx": $20, - "./routes/client_nav_opt_out/page-a.tsx": $21, - "./routes/client_nav_opt_out/page-b.tsx": $22, - "./routes/client_nav_opt_out/page-c.tsx": $23, - "./routes/deep_partial/index.tsx": $24, - "./routes/deep_partial/injected.tsx": $25, - "./routes/deep_partial/update.tsx": $26, - "./routes/form/index.tsx": $27, - "./routes/form/injected.tsx": $28, - "./routes/form/update.tsx": $29, - "./routes/fragment_nav.tsx": $30, - "./routes/fragment_nav_scroll.tsx": $31, - "./routes/head_merge/duplicate.tsx": $32, - "./routes/head_merge/index.tsx": $33, - "./routes/head_merge/injected.tsx": $34, - "./routes/head_merge/update.tsx": $35, - "./routes/index.tsx": $36, - "./routes/island_instance/index.tsx": $37, - "./routes/island_instance/injected.tsx": $38, - "./routes/island_instance/partial.tsx": $39, - "./routes/island_instance/partial_remove.tsx": $40, - "./routes/island_instance/partial_replace.tsx": $41, - "./routes/island_instance_multiple/index.tsx": $42, - "./routes/island_instance_multiple/injected.tsx": $43, - "./routes/island_instance_multiple/partial.tsx": $44, - "./routes/island_instance_multiple/partial_both.tsx": $45, - "./routes/island_instance_nested/index.tsx": $46, - "./routes/island_instance_nested/injected.tsx": $47, - "./routes/island_instance_nested/partial.tsx": $48, - "./routes/island_instance_nested/replace.tsx": $49, - "./routes/island_props/index.tsx": $50, - "./routes/island_props/injected.tsx": $51, - "./routes/island_props/partial.tsx": $52, - "./routes/island_props_signals/index.tsx": $53, - "./routes/island_props_signals/injected.tsx": $54, - "./routes/island_props_signals/partial.tsx": $55, - "./routes/keys/index.tsx": $56, - "./routes/keys/injected.tsx": $57, - "./routes/keys/swap.tsx": $58, - "./routes/keys_components/index.tsx": $59, - "./routes/keys_components/injected.tsx": $60, - "./routes/keys_components/swap.tsx": $61, - "./routes/keys_confusion/index.tsx": $62, - "./routes/keys_dom/index.tsx": $63, - "./routes/keys_dom/injected.tsx": $64, - "./routes/keys_dom/swap.tsx": $65, - "./routes/keys_outside/index.tsx": $66, - "./routes/loading/index.tsx": $67, - "./routes/loading/injected.tsx": $68, - "./routes/loading/update.tsx": $69, - "./routes/missing_partial/index.tsx": $70, - "./routes/missing_partial/injected.tsx": $71, - "./routes/missing_partial/update.tsx": $72, - "./routes/mode/append.tsx": $73, - "./routes/mode/index.tsx": $74, - "./routes/mode/injected.tsx": $75, - "./routes/mode/prepend.tsx": $76, - "./routes/mode/replace.tsx": $77, - "./routes/no_islands/index.tsx": $78, - "./routes/no_islands/injected.tsx": $79, - "./routes/no_islands/update.tsx": $80, - "./routes/no_partial_response/index.tsx": $81, - "./routes/no_partial_response/injected.tsx": $82, - "./routes/no_partial_response/update.tsx": $83, - "./routes/partial_slot_inside_island.tsx": $84, - "./routes/relative_link/index.tsx": $85, - "./routes/scroll_restoration/index.tsx": $86, - "./routes/scroll_restoration/injected.tsx": $87, - "./routes/scroll_restoration/update.tsx": $88, + "./routes/client_nav_both/_layout.tsx": $18, + "./routes/client_nav_both/index.tsx": $19, + "./routes/client_nav_both/page-a.tsx": $20, + "./routes/client_nav_both/page-b.tsx": $21, + "./routes/client_nav_both/page-c.tsx": $22, + "./routes/client_nav_opt_out/_layout.tsx": $23, + "./routes/client_nav_opt_out/index.tsx": $24, + "./routes/client_nav_opt_out/injected.tsx": $25, + "./routes/client_nav_opt_out/page-a.tsx": $26, + "./routes/client_nav_opt_out/page-b.tsx": $27, + "./routes/client_nav_opt_out/page-c.tsx": $28, + "./routes/deep_partial/index.tsx": $29, + "./routes/deep_partial/injected.tsx": $30, + "./routes/deep_partial/update.tsx": $31, + "./routes/form/index.tsx": $32, + "./routes/form/injected.tsx": $33, + "./routes/form/update.tsx": $34, + "./routes/fragment_nav.tsx": $35, + "./routes/fragment_nav_scroll.tsx": $36, + "./routes/head_merge/duplicate.tsx": $37, + "./routes/head_merge/index.tsx": $38, + "./routes/head_merge/injected.tsx": $39, + "./routes/head_merge/update.tsx": $40, + "./routes/index.tsx": $41, + "./routes/island_instance/index.tsx": $42, + "./routes/island_instance/injected.tsx": $43, + "./routes/island_instance/partial.tsx": $44, + "./routes/island_instance/partial_remove.tsx": $45, + "./routes/island_instance/partial_replace.tsx": $46, + "./routes/island_instance_multiple/index.tsx": $47, + "./routes/island_instance_multiple/injected.tsx": $48, + "./routes/island_instance_multiple/partial.tsx": $49, + "./routes/island_instance_multiple/partial_both.tsx": $50, + "./routes/island_instance_nested/index.tsx": $51, + "./routes/island_instance_nested/injected.tsx": $52, + "./routes/island_instance_nested/partial.tsx": $53, + "./routes/island_instance_nested/replace.tsx": $54, + "./routes/island_props/index.tsx": $55, + "./routes/island_props/injected.tsx": $56, + "./routes/island_props/partial.tsx": $57, + "./routes/island_props_signals/index.tsx": $58, + "./routes/island_props_signals/injected.tsx": $59, + "./routes/island_props_signals/partial.tsx": $60, + "./routes/keys/index.tsx": $61, + "./routes/keys/injected.tsx": $62, + "./routes/keys/swap.tsx": $63, + "./routes/keys_components/index.tsx": $64, + "./routes/keys_components/injected.tsx": $65, + "./routes/keys_components/swap.tsx": $66, + "./routes/keys_confusion/index.tsx": $67, + "./routes/keys_dom/index.tsx": $68, + "./routes/keys_dom/injected.tsx": $69, + "./routes/keys_dom/swap.tsx": $70, + "./routes/keys_outside/index.tsx": $71, + "./routes/loading/index.tsx": $72, + "./routes/loading/injected.tsx": $73, + "./routes/loading/update.tsx": $74, + "./routes/missing_partial/index.tsx": $75, + "./routes/missing_partial/injected.tsx": $76, + "./routes/missing_partial/update.tsx": $77, + "./routes/mode/append.tsx": $78, + "./routes/mode/index.tsx": $79, + "./routes/mode/injected.tsx": $80, + "./routes/mode/prepend.tsx": $81, + "./routes/mode/replace.tsx": $82, + "./routes/no_islands/index.tsx": $83, + "./routes/no_islands/injected.tsx": $84, + "./routes/no_islands/update.tsx": $85, + "./routes/no_partial_response/index.tsx": $86, + "./routes/no_partial_response/injected.tsx": $87, + "./routes/no_partial_response/update.tsx": $88, + "./routes/partial_slot_inside_island.tsx": $89, + "./routes/relative_link/index.tsx": $90, + "./routes/scroll_restoration/index.tsx": $91, + "./routes/scroll_restoration/injected.tsx": $92, + "./routes/scroll_restoration/update.tsx": $93, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture_partials/routes/client_nav_both/_layout.tsx b/tests/fixture_partials/routes/client_nav_both/_layout.tsx new file mode 100644 index 00000000..f614581d --- /dev/null +++ b/tests/fixture_partials/routes/client_nav_both/_layout.tsx @@ -0,0 +1,50 @@ +import { AppProps } from "$fresh/server.ts"; +import { Partial } from "$fresh/runtime.ts"; +import { Fader } from "../../islands/Fader.tsx"; + +export default function AppLayout({ Component }: AppProps) { + return ( + + + + + fresh title + + + + + + + +
+

+ + Page A + +

+

+ + Page B + +

+

+ + Page C + +

+
+ +
+      
+    
+  );
+}
diff --git a/tests/fixture_partials/routes/client_nav_both/index.tsx b/tests/fixture_partials/routes/client_nav_both/index.tsx
new file mode 100644
index 00000000..bf717a6a
--- /dev/null
+++ b/tests/fixture_partials/routes/client_nav_both/index.tsx
@@ -0,0 +1,21 @@
+import { Partial } from "$fresh/runtime.ts";
+import { RouteConfig } from "$fresh/server.ts";
+import CounterA from "../../islands/CounterA.tsx";
+import { Fader } from "../../islands/Fader.tsx";
+
+export const config: RouteConfig = {
+  skipAppWrapper: true,
+};
+
+export default function ModeDemo() {
+  return (
+    
+ + +

Initial content

+ +
+
+
+ ); +} diff --git a/tests/fixture_partials/routes/client_nav_both/page-a.tsx b/tests/fixture_partials/routes/client_nav_both/page-a.tsx new file mode 100644 index 00000000..94fcfa0b --- /dev/null +++ b/tests/fixture_partials/routes/client_nav_both/page-a.tsx @@ -0,0 +1,17 @@ +import { RouteConfig } from "$fresh/server.ts"; +import CounterA from "../../islands/CounterA.tsx"; +import { Fader } from "../../islands/Fader.tsx"; + +export const config: RouteConfig = { + skipAppWrapper: true, +}; + +export default function PageA() { + return ( + +

Page A

+

asdfasdf asdf asdf

+ +
+ ); +} diff --git a/tests/fixture_partials/routes/client_nav_both/page-b.tsx b/tests/fixture_partials/routes/client_nav_both/page-b.tsx new file mode 100644 index 00000000..70468dc2 --- /dev/null +++ b/tests/fixture_partials/routes/client_nav_both/page-b.tsx @@ -0,0 +1,20 @@ +import { RouteConfig } from "$fresh/server.ts"; +import { Fader } from "../../islands/Fader.tsx"; +import CounterB from "../../islands/CounterB.tsx"; + +export const config: RouteConfig = { + skipAppWrapper: true, +}; + +export default function PageB() { + return ( + + something before +
+

Page B

+
+ +

asdfasdf asdf asdf

+
+ ); +} diff --git a/tests/fixture_partials/routes/client_nav_both/page-c.tsx b/tests/fixture_partials/routes/client_nav_both/page-c.tsx new file mode 100644 index 00000000..577849e7 --- /dev/null +++ b/tests/fixture_partials/routes/client_nav_both/page-c.tsx @@ -0,0 +1,17 @@ +import { RouteConfig } from "$fresh/server.ts"; +import { Fader } from "../../islands/Fader.tsx"; + +export const config: RouteConfig = { + skipAppWrapper: true, +}; + +export default function PageB() { + return ( + +

Page C

+ +

asdfasdf asdf asdf

+
+
+ ); +} diff --git a/tests/partials_test.ts b/tests/partials_test.ts index 99a29c6b..3a2cb3e8 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -15,7 +15,8 @@ import { } from "./test_utils.ts"; async function assertLogs(page: Page, expected: string[]) { - await waitForText(page, "#logs", expected.join("\n") + "\n"); + const text = expected.length > 0 ? expected.join("\n") + "\n" : ""; + await waitForText(page, "#logs", text); } Deno.test("injects server content with no islands present", async () => { @@ -852,6 +853,62 @@ Deno.test("allow opting out of client navigation", async () => { ); }); +Deno.test("allow opting out of client navigation if parent opted in", async () => { + await withPageName( + "./tests/fixture_partials/main.ts", + async (page, address) => { + const initialUrl = `${address}/client_nav_both`; + await page.goto(initialUrl); + await page.waitForSelector(".island"); + + await page.click(".island-a button"); + await waitForText(page, ".output-a", "1"); + + // Go to page B + await page.click(".page-b-link"); + await page.waitForSelector(".island-b"); + await assertLogs(page, ["mount Counter B"]); + + await page.click(".island-b button"); + await waitForText(page, ".output-b", "1"); + await assertLogs(page, ["mount Counter B", "update Counter B"]); + + // Go to page C + await page.click(".page-c-link"); + await page.waitForSelector(".page-c-text"); + await assertLogs(page, []); + + // Go back to B + await page.goBack(); + await page.waitForSelector(".island-b"); + await assertLogs(page, ["mount Counter B"]); + + // Check that island is interactive + await page.click(".island-b button"); + await waitForText(page, ".output-b", "1"); + + // Go back to A + await page.goBack(); + await page.waitForSelector(".island-a"); + await assertLogs(page, ["mount Counter A"]); + + // Check that island is interactive + await page.click(".island-a button"); + await waitForText(page, ".output-a", "1"); + await assertNoPageComments(page); + + // Go forward to B + await page.goForward(); + await page.waitForSelector(".island-b"); + await assertLogs(page, ["mount Counter B"]); + + // Check that island is interactive + await page.click(".island-b button"); + await waitForText(page, ".output-b", "1"); + }, + ); +}); + Deno.test("restore scroll position", async () => { await withPageName( "./tests/fixture_partials/main.ts",