From ea91687ea35f083fccec4c9f43f8a8a6a52886dd Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Wed, 4 Oct 2023 15:07:27 +0200 Subject: [PATCH] fix: active links outside partials not updated (#1852) --- src/constants.ts | 2 + src/runtime/active_url.ts | 46 +++- src/runtime/entrypoints/main.ts | 33 ++- src/server/rendering/preact_hooks.ts | 15 +- tests/fixture_partials/fresh.gen.ts | 250 +++++++++--------- .../routes/active_nav_partial/_layout.tsx | 9 + .../routes/active_nav_partial/foo/bar.tsx | 23 ++ .../routes/active_nav_partial/foo/index.tsx | 23 ++ .../routes/active_nav_partial/index.tsx | 23 ++ .../routes/active_nav_partial/island.tsx | 17 ++ tests/partials_test.ts | 32 +++ 11 files changed, 330 insertions(+), 143 deletions(-) create mode 100644 tests/fixture_partials/routes/active_nav_partial/_layout.tsx create mode 100644 tests/fixture_partials/routes/active_nav_partial/foo/bar.tsx create mode 100644 tests/fixture_partials/routes/active_nav_partial/foo/index.tsx create mode 100644 tests/fixture_partials/routes/active_nav_partial/index.tsx create mode 100644 tests/fixture_partials/routes/active_nav_partial/island.tsx diff --git a/src/constants.ts b/src/constants.ts index 69c52b5a..ae57771b 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -3,6 +3,8 @@ export const PARTIAL_ATTR = "f-partial"; export const LOADING_ATTR = "f-loading"; export const CLIENT_NAV_ATTR = "f-client-nav"; export const DATA_KEY_ATTR = "data-fresh-key"; +export const DATA_CURRENT = "data-current"; +export const DATA_ANCESTOR = "data-ancestor"; export const enum PartialMode { REPLACE, diff --git a/src/runtime/active_url.ts b/src/runtime/active_url.ts index c55423f2..60f4418a 100644 --- a/src/runtime/active_url.ts +++ b/src/runtime/active_url.ts @@ -1,26 +1,44 @@ import { VNode } from "preact"; +import { DATA_ANCESTOR, DATA_CURRENT } from "../constants.ts"; + +export const enum UrlMatchKind { + None, + Ancestor, + Current, +} + +export function matchesUrl(current: string, needle: string): UrlMatchKind { + let href = new URL(needle, "http://localhost").pathname; + if (href !== "/" && href.endsWith("/")) { + href = href.slice(0, -1); + } + + if (current !== "/" && current.endsWith("/")) { + current = current.slice(0, -1); + } + + if (current === href) { + return UrlMatchKind.Current; + } else if (current.startsWith(href + "/") || href === "/") { + return UrlMatchKind.Ancestor; + } + + return UrlMatchKind.None; +} /** * Mark active or ancestor link * Note: This function is used both on the server and the client */ -export function setActiveUrl(vnode: VNode, pathname: string) { +export function setActiveUrl(vnode: VNode, pathname: string): void { const props = vnode.props as Record; const hrefProp = props.href; if (typeof hrefProp === "string" && hrefProp.startsWith("/")) { - let href = new URL(hrefProp, "http://localhost").pathname; - if (href !== "/" && href.endsWith("/")) { - href = href.slice(0, -1); - } - - if (pathname !== "/" && pathname.endsWith("/")) { - pathname = pathname.slice(0, -1); - } - - if (pathname === href) { - props["data-current"] = "true"; - } else if (pathname.startsWith(href + "/") || href === "/") { - props["data-ancestor"] = "true"; + const match = matchesUrl(pathname, hrefProp); + if (match === UrlMatchKind.Current) { + props[DATA_CURRENT] = "true"; + } else if (match === UrlMatchKind.Ancestor) { + props[DATA_ANCESTOR] = "true"; } } } diff --git a/src/runtime/entrypoints/main.ts b/src/runtime/entrypoints/main.ts index 9ef4ead1..a981ca42 100644 --- a/src/runtime/entrypoints/main.ts +++ b/src/runtime/entrypoints/main.ts @@ -14,13 +14,15 @@ import { type SerializedState } from "../../server/rendering/fresh_tags.tsx"; import type { Signal } from "@preact/signals"; import { CLIENT_NAV_ATTR, + DATA_ANCESTOR, + DATA_CURRENT, DATA_KEY_ATTR, LOADING_ATTR, PARTIAL_ATTR, PARTIAL_SEARCH_PARAM, PartialMode, } from "../../constants.ts"; -import { setActiveUrl } from "../active_url.ts"; +import { matchesUrl, setActiveUrl, UrlMatchKind } from "../active_url.ts"; function createRootFragment( parent: Element, @@ -553,10 +555,26 @@ function _walkInner( const partialErrorMessage = `Unable to process partial response.`; -async function fetchPartials(url: URL, init?: RequestInit) { +async function fetchPartials(url: URL, realUrl: URL, init?: RequestInit) { url.searchParams.set(PARTIAL_SEARCH_PARAM, "true"); const res = await fetch(url, init); await applyPartials(res); + + // Update links + document.querySelectorAll("a").forEach((link) => { + const match = matchesUrl(realUrl.pathname, link.href); + + if (match === UrlMatchKind.Current) { + link.setAttribute(DATA_CURRENT, "true"); + link.removeAttribute(DATA_ANCESTOR); + } else if (match === UrlMatchKind.Ancestor) { + link.setAttribute(DATA_ANCESTOR, "true"); + link.removeAttribute(DATA_CURRENT); + } else { + link.removeAttribute(DATA_CURRENT); + link.removeAttribute(DATA_ANCESTOR); + } + }); } /** @@ -767,20 +785,21 @@ document.addEventListener("click", async (e) => { e.preventDefault(); + const nextUrl = new URL(el.href); try { // Only add history entry when URL is new. Still apply // the partials because sometimes users click a link to // "refresh" the current page. if (el.href !== window.location.href) { index++; - history.pushState({ index }, "", el.href); + history.pushState({ index }, "", nextUrl.href); } const partialUrl = new URL( - partial ? partial : el.href, + partial ? partial : nextUrl.href, location.origin, ); - await fetchPartials(partialUrl); + await fetchPartials(partialUrl, nextUrl); } finally { if (indicator !== undefined) { indicator.value = false; @@ -804,7 +823,7 @@ addEventListener("popstate", async (e) => { // page navigation. if (partials.has("body")) { const url = new URL(location.href, location.origin); - await fetchPartials(url); + await fetchPartials(url, url); } else { location.reload(); } @@ -819,7 +838,7 @@ document.addEventListener("submit", async (e) => { e.preventDefault(); const url = new URL(partial, location.origin); - await fetchPartials(url); + await fetchPartials(url, new URL(location.href)); } } }); diff --git a/src/server/rendering/preact_hooks.ts b/src/server/rendering/preact_hooks.ts index 6d4c9746..769ba25f 100644 --- a/src/server/rendering/preact_hooks.ts +++ b/src/server/rendering/preact_hooks.ts @@ -14,7 +14,12 @@ import { Partial, PartialProps } from "../../runtime/Partial.tsx"; import { renderToString } from "preact-render-to-string"; import { RenderState } from "./state.ts"; import { Island } from "../types.ts"; -import { DATA_KEY_ATTR, LOADING_ATTR, PartialMode } from "../../constants.ts"; +import { + CLIENT_NAV_ATTR, + DATA_KEY_ATTR, + LOADING_ATTR, + PartialMode, +} from "../../constants.ts"; import { setActiveUrl } from "../../runtime/active_url.ts"; // See: https://github.com/preactjs/preact/blob/7748dcb83cedd02e37b3713634e35b97b26028fd/src/internal.d.ts#L3C1-L16 @@ -192,10 +197,16 @@ options.vnode = (vnode) => { vnode.type !== "style" && vnode.type !== "script" && vnode.type !== "link" ) { props[DATA_KEY_ATTR] = vnode.key; - } else if (props[LOADING_ATTR]) { + } + + if (props[LOADING_ATTR]) { // Avoid automatic signals unwrapping props[LOADING_ATTR] = { value: props[LOADING_ATTR] }; } + + if (typeof props[CLIENT_NAV_ATTR] === "boolean") { + props[CLIENT_NAV_ATTR] = props[CLIENT_NAV_ATTR] ? "true" : null; + } } else if ( current && typeof vnode.type === "function" && vnode.type !== Fragment && ownerStack.length > 0 diff --git a/tests/fixture_partials/fresh.gen.ts b/tests/fixture_partials/fresh.gen.ts index e156ad7b..93fa50dc 100644 --- a/tests/fixture_partials/fresh.gen.ts +++ b/tests/fixture_partials/fresh.gen.ts @@ -7,66 +7,71 @@ import * as $1 from "./routes/active_nav/foo/bar.tsx"; import * as $2 from "./routes/active_nav/foo/index.tsx"; import * as $3 from "./routes/active_nav/index.tsx"; import * as $4 from "./routes/active_nav/island.tsx"; -import * as $5 from "./routes/client_nav/_layout.tsx"; -import * as $6 from "./routes/client_nav/index.tsx"; -import * as $7 from "./routes/client_nav/injected.tsx"; -import * as $8 from "./routes/client_nav/page-a.tsx"; -import * as $9 from "./routes/client_nav/page-b.tsx"; -import * as $10 from "./routes/client_nav/page-c.tsx"; -import * as $11 from "./routes/client_nav_opt_out/_layout.tsx"; -import * as $12 from "./routes/client_nav_opt_out/index.tsx"; -import * as $13 from "./routes/client_nav_opt_out/injected.tsx"; -import * as $14 from "./routes/client_nav_opt_out/page-a.tsx"; -import * as $15 from "./routes/client_nav_opt_out/page-b.tsx"; -import * as $16 from "./routes/client_nav_opt_out/page-c.tsx"; -import * as $17 from "./routes/form/index.tsx"; -import * as $18 from "./routes/form/injected.tsx"; -import * as $19 from "./routes/form/update.tsx"; -import * as $20 from "./routes/fragment_nav.tsx"; -import * as $21 from "./routes/index.tsx"; -import * as $22 from "./routes/island_instance/index.tsx"; -import * as $23 from "./routes/island_instance/injected.tsx"; -import * as $24 from "./routes/island_instance/partial.tsx"; -import * as $25 from "./routes/island_instance/partial_remove.tsx"; -import * as $26 from "./routes/island_instance/partial_replace.tsx"; -import * as $27 from "./routes/island_instance_multiple/index.tsx"; -import * as $28 from "./routes/island_instance_multiple/injected.tsx"; -import * as $29 from "./routes/island_instance_multiple/partial.tsx"; -import * as $30 from "./routes/island_instance_multiple/partial_both.tsx"; -import * as $31 from "./routes/island_instance_nested/index.tsx"; -import * as $32 from "./routes/island_instance_nested/injected.tsx"; -import * as $33 from "./routes/island_instance_nested/partial.tsx"; -import * as $34 from "./routes/island_instance_nested/replace.tsx"; -import * as $35 from "./routes/island_props/index.tsx"; -import * as $36 from "./routes/island_props/injected.tsx"; -import * as $37 from "./routes/island_props/partial.tsx"; -import * as $38 from "./routes/island_props_signals/index.tsx"; -import * as $39 from "./routes/island_props_signals/injected.tsx"; -import * as $40 from "./routes/island_props_signals/partial.tsx"; -import * as $41 from "./routes/keys/index.tsx"; -import * as $42 from "./routes/keys/injected.tsx"; -import * as $43 from "./routes/keys/swap.tsx"; -import * as $44 from "./routes/keys_components/index.tsx"; -import * as $45 from "./routes/keys_components/injected.tsx"; -import * as $46 from "./routes/keys_components/swap.tsx"; -import * as $47 from "./routes/keys_dom/index.tsx"; -import * as $48 from "./routes/keys_dom/injected.tsx"; -import * as $49 from "./routes/keys_dom/swap.tsx"; -import * as $50 from "./routes/loading/index.tsx"; -import * as $51 from "./routes/loading/injected.tsx"; -import * as $52 from "./routes/loading/update.tsx"; -import * as $53 from "./routes/missing_partial/index.tsx"; -import * as $54 from "./routes/missing_partial/injected.tsx"; -import * as $55 from "./routes/missing_partial/update.tsx"; -import * as $56 from "./routes/mode/append.tsx"; -import * as $57 from "./routes/mode/index.tsx"; -import * as $58 from "./routes/mode/injected.tsx"; -import * as $59 from "./routes/mode/prepend.tsx"; -import * as $60 from "./routes/mode/replace.tsx"; -import * as $61 from "./routes/no_islands/index.tsx"; -import * as $62 from "./routes/no_islands/injected.tsx"; -import * as $63 from "./routes/no_islands/update.tsx"; -import * as $64 from "./routes/partial_slot_inside_island.tsx"; +import * as $5 from "./routes/active_nav_partial/_layout.tsx"; +import * as $6 from "./routes/active_nav_partial/foo/bar.tsx"; +import * as $7 from "./routes/active_nav_partial/foo/index.tsx"; +import * as $8 from "./routes/active_nav_partial/index.tsx"; +import * as $9 from "./routes/active_nav_partial/island.tsx"; +import * as $10 from "./routes/client_nav/_layout.tsx"; +import * as $11 from "./routes/client_nav/index.tsx"; +import * as $12 from "./routes/client_nav/injected.tsx"; +import * as $13 from "./routes/client_nav/page-a.tsx"; +import * as $14 from "./routes/client_nav/page-b.tsx"; +import * as $15 from "./routes/client_nav/page-c.tsx"; +import * as $16 from "./routes/client_nav_opt_out/_layout.tsx"; +import * as $17 from "./routes/client_nav_opt_out/index.tsx"; +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/loading/index.tsx"; +import * as $56 from "./routes/loading/injected.tsx"; +import * as $57 from "./routes/loading/update.tsx"; +import * as $58 from "./routes/missing_partial/index.tsx"; +import * as $59 from "./routes/missing_partial/injected.tsx"; +import * as $60 from "./routes/missing_partial/update.tsx"; +import * as $61 from "./routes/mode/append.tsx"; +import * as $62 from "./routes/mode/index.tsx"; +import * as $63 from "./routes/mode/injected.tsx"; +import * as $64 from "./routes/mode/prepend.tsx"; +import * as $65 from "./routes/mode/replace.tsx"; +import * as $66 from "./routes/no_islands/index.tsx"; +import * as $67 from "./routes/no_islands/injected.tsx"; +import * as $68 from "./routes/no_islands/update.tsx"; +import * as $69 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"; @@ -89,66 +94,71 @@ const manifest = { "./routes/active_nav/foo/index.tsx": $2, "./routes/active_nav/index.tsx": $3, "./routes/active_nav/island.tsx": $4, - "./routes/client_nav/_layout.tsx": $5, - "./routes/client_nav/index.tsx": $6, - "./routes/client_nav/injected.tsx": $7, - "./routes/client_nav/page-a.tsx": $8, - "./routes/client_nav/page-b.tsx": $9, - "./routes/client_nav/page-c.tsx": $10, - "./routes/client_nav_opt_out/_layout.tsx": $11, - "./routes/client_nav_opt_out/index.tsx": $12, - "./routes/client_nav_opt_out/injected.tsx": $13, - "./routes/client_nav_opt_out/page-a.tsx": $14, - "./routes/client_nav_opt_out/page-b.tsx": $15, - "./routes/client_nav_opt_out/page-c.tsx": $16, - "./routes/form/index.tsx": $17, - "./routes/form/injected.tsx": $18, - "./routes/form/update.tsx": $19, - "./routes/fragment_nav.tsx": $20, - "./routes/index.tsx": $21, - "./routes/island_instance/index.tsx": $22, - "./routes/island_instance/injected.tsx": $23, - "./routes/island_instance/partial.tsx": $24, - "./routes/island_instance/partial_remove.tsx": $25, - "./routes/island_instance/partial_replace.tsx": $26, - "./routes/island_instance_multiple/index.tsx": $27, - "./routes/island_instance_multiple/injected.tsx": $28, - "./routes/island_instance_multiple/partial.tsx": $29, - "./routes/island_instance_multiple/partial_both.tsx": $30, - "./routes/island_instance_nested/index.tsx": $31, - "./routes/island_instance_nested/injected.tsx": $32, - "./routes/island_instance_nested/partial.tsx": $33, - "./routes/island_instance_nested/replace.tsx": $34, - "./routes/island_props/index.tsx": $35, - "./routes/island_props/injected.tsx": $36, - "./routes/island_props/partial.tsx": $37, - "./routes/island_props_signals/index.tsx": $38, - "./routes/island_props_signals/injected.tsx": $39, - "./routes/island_props_signals/partial.tsx": $40, - "./routes/keys/index.tsx": $41, - "./routes/keys/injected.tsx": $42, - "./routes/keys/swap.tsx": $43, - "./routes/keys_components/index.tsx": $44, - "./routes/keys_components/injected.tsx": $45, - "./routes/keys_components/swap.tsx": $46, - "./routes/keys_dom/index.tsx": $47, - "./routes/keys_dom/injected.tsx": $48, - "./routes/keys_dom/swap.tsx": $49, - "./routes/loading/index.tsx": $50, - "./routes/loading/injected.tsx": $51, - "./routes/loading/update.tsx": $52, - "./routes/missing_partial/index.tsx": $53, - "./routes/missing_partial/injected.tsx": $54, - "./routes/missing_partial/update.tsx": $55, - "./routes/mode/append.tsx": $56, - "./routes/mode/index.tsx": $57, - "./routes/mode/injected.tsx": $58, - "./routes/mode/prepend.tsx": $59, - "./routes/mode/replace.tsx": $60, - "./routes/no_islands/index.tsx": $61, - "./routes/no_islands/injected.tsx": $62, - "./routes/no_islands/update.tsx": $63, - "./routes/partial_slot_inside_island.tsx": $64, + "./routes/active_nav_partial/_layout.tsx": $5, + "./routes/active_nav_partial/foo/bar.tsx": $6, + "./routes/active_nav_partial/foo/index.tsx": $7, + "./routes/active_nav_partial/index.tsx": $8, + "./routes/active_nav_partial/island.tsx": $9, + "./routes/client_nav/_layout.tsx": $10, + "./routes/client_nav/index.tsx": $11, + "./routes/client_nav/injected.tsx": $12, + "./routes/client_nav/page-a.tsx": $13, + "./routes/client_nav/page-b.tsx": $14, + "./routes/client_nav/page-c.tsx": $15, + "./routes/client_nav_opt_out/_layout.tsx": $16, + "./routes/client_nav_opt_out/index.tsx": $17, + "./routes/client_nav_opt_out/injected.tsx": $18, + "./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/loading/index.tsx": $55, + "./routes/loading/injected.tsx": $56, + "./routes/loading/update.tsx": $57, + "./routes/missing_partial/index.tsx": $58, + "./routes/missing_partial/injected.tsx": $59, + "./routes/missing_partial/update.tsx": $60, + "./routes/mode/append.tsx": $61, + "./routes/mode/index.tsx": $62, + "./routes/mode/injected.tsx": $63, + "./routes/mode/prepend.tsx": $64, + "./routes/mode/replace.tsx": $65, + "./routes/no_islands/index.tsx": $66, + "./routes/no_islands/injected.tsx": $67, + "./routes/no_islands/update.tsx": $68, + "./routes/partial_slot_inside_island.tsx": $69, }, islands: { "./islands/Counter.tsx": $$0, diff --git a/tests/fixture_partials/routes/active_nav_partial/_layout.tsx b/tests/fixture_partials/routes/active_nav_partial/_layout.tsx new file mode 100644 index 00000000..e31f709e --- /dev/null +++ b/tests/fixture_partials/routes/active_nav_partial/_layout.tsx @@ -0,0 +1,9 @@ +import { LayoutProps } from "$fresh/server.ts"; + +export default function Layout({ Component }: LayoutProps) { + return ( +
+ +
+ ); +} diff --git a/tests/fixture_partials/routes/active_nav_partial/foo/bar.tsx b/tests/fixture_partials/routes/active_nav_partial/foo/bar.tsx new file mode 100644 index 00000000..91a39463 --- /dev/null +++ b/tests/fixture_partials/routes/active_nav_partial/foo/bar.tsx @@ -0,0 +1,23 @@ +import { Partial } from "$fresh/runtime.ts"; + +export default function Page() { + return ( +
+ +

/active_nav_partial/foo/bar

+
+

+ /active_nav_partial/foo/bar +

+

+ /active_nav_partial/foo +

+

+ /active_nav_partial +

+

+ / +

+
+ ); +} diff --git a/tests/fixture_partials/routes/active_nav_partial/foo/index.tsx b/tests/fixture_partials/routes/active_nav_partial/foo/index.tsx new file mode 100644 index 00000000..b0f9ab7e --- /dev/null +++ b/tests/fixture_partials/routes/active_nav_partial/foo/index.tsx @@ -0,0 +1,23 @@ +import { Partial } from "$fresh/runtime.ts"; + +export default function Page() { + return ( +
+ +

/active_nav_partial/foo

+
+

+ /active_nav_partial/foo/bar +

+

+ /active_nav_partial/foo +

+

+ /active_nav_partial +

+

+ / +

+
+ ); +} diff --git a/tests/fixture_partials/routes/active_nav_partial/index.tsx b/tests/fixture_partials/routes/active_nav_partial/index.tsx new file mode 100644 index 00000000..467fa73d --- /dev/null +++ b/tests/fixture_partials/routes/active_nav_partial/index.tsx @@ -0,0 +1,23 @@ +import { Partial } from "$fresh/runtime.ts"; + +export default function Page() { + return ( +
+ +

/active_nav_partial

+
+

+ /active_nav_partial/foo/bar +

+

+ /active_nav_partial/foo +

+

+ /active_nav_partial +

+

+ / +

+
+ ); +} diff --git a/tests/fixture_partials/routes/active_nav_partial/island.tsx b/tests/fixture_partials/routes/active_nav_partial/island.tsx new file mode 100644 index 00000000..52506c92 --- /dev/null +++ b/tests/fixture_partials/routes/active_nav_partial/island.tsx @@ -0,0 +1,17 @@ +import LazyLink from "../../islands/LazyLink.tsx"; + +export default function Page() { + return ( +
+

active nav island

+ +
+ ); +} diff --git a/tests/partials_test.ts b/tests/partials_test.ts index be78f90c..37345bf6 100644 --- a/tests/partials_test.ts +++ b/tests/partials_test.ts @@ -923,5 +923,37 @@ Deno.test("active links without client nav", async () => { ); }); +Deno.test("Updates active links outside of vdom", async () => { + await withPageName( + "./tests/fixture_partials/main.ts", + async (page, address) => { + await page.goto(`${address}/active_nav_partial`); + + let doc = parseHtml(await page.content()); + assertSelector(doc, "a[href='/'][data-ancestor]"); + + // Current + assertNotSelector(doc, "a[href='/active_nav_partial'][data-ancestor]"); + assertSelector(doc, "a[href='/active_nav_partial'][data-current]"); + + // Unrelated links + assertNotSelector( + doc, + "a[href='/active_nav_partial/foo'][data-ancestor]", + ); + assertNotSelector( + doc, + "a[href='/active_nav_partial/foo/bar'][data-ancestor]", + ); + + await page.goto(`${address}/active_nav_partial/foo`); + doc = parseHtml(await page.content()); + assertSelector(doc, "a[href='/active_nav_partial/foo'][data-current]"); + assertSelector(doc, "a[href='/active_nav_partial'][data-ancestor]"); + assertSelector(doc, "a[href='/'][data-ancestor]"); + }, + ); +}); + // TODO Head merging // TODO Children Keys