Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions packages/fresh/src/runtime/shared_internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export const enum UrlMatchKind {
export function matchesUrl(
current: string,
needle: string,
currentSearch?: string,
currentSearch = "",
): UrlMatchKind {
const needleUrl = new URL(needle, "http://localhost");
let href = needleUrl.pathname;
Expand All @@ -60,12 +60,8 @@ export function matchesUrl(
}

if (current === href) {
// If the link has query params, only mark as current if they match
if (
needleSearch && currentSearch !== undefined &&
needleSearch !== currentSearch
) {
return UrlMatchKind.Ancestor;
if (needleSearch !== currentSearch) {
return UrlMatchKind.None;
}
return UrlMatchKind.Current;
} else if (current.startsWith(href + "/") || href === "/") {
Expand Down
71 changes: 54 additions & 17 deletions packages/fresh/tests/active_links_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
buildProd,
Doc,
parseHtml,
waitForText,
withBrowserApp,
} from "./test_utils.tsx";

Expand Down Expand Up @@ -84,7 +85,7 @@ Deno.test({
});

Deno.test({
name: "active links - query params differentiate current vs ancestor",
name: "active links - only the same path and query is current",
fn: async () => {
function TabView() {
return (
Expand All @@ -105,28 +106,64 @@ Deno.test({

const server = new FakeServer(app.handler());

// When visiting /tabs?sort=name, only that link is "current"
let res = await server.get("/tabs?sort=name");
let doc = parseHtml(await res.text());

// Exact match including query params
assertSelector(doc, `a[href='/tabs?sort=name'][data-current]`);
assertSelector(doc, `a[href='/tabs?sort=name'][aria-current="page"]`);
assertSelector(
doc,
`a[href='/tabs?sort=name'][data-current][aria-current="page"]`,
);
assertNotSelector(doc, "a[href='/tabs'][aria-current]");
assertNotSelector(doc, "a[href='/tabs?sort=price'][aria-current]");

// Different query params → ancestor, not current
assertNotSelector(doc, `a[href='/tabs?sort=price'][data-current]`);
assertSelector(doc, `a[href='/tabs?sort=price'][data-ancestor]`);

// Link without query params matches regardless
assertSelector(doc, `a[href='/tabs'][data-current]`);

// When visiting /tabs (no query), all links on same path are current
res = await server.get("/tabs");
doc = parseHtml(await res.text());
assertSelector(doc, `a[href='/tabs'][data-current]`);
// Links with query params → ancestor since current URL has no query
assertSelector(doc, `a[href='/tabs?sort=name'][data-ancestor]`);
assertSelector(doc, `a[href='/tabs?sort=price'][data-ancestor]`);
assertSelector(doc, `a[href='/tabs'][data-current][aria-current="page"]`);
assertNotSelector(doc, "a[href='/tabs?sort=name'][aria-current]");
assertNotSelector(doc, "a[href='/tabs?sort=price'][aria-current]");
},
});

Deno.test({
name:
"active links - query changes update current links on client navigation",
fn: async () => {
const app = testApp().get("/tabs", (ctx) => {
const sort = ctx.url.searchParams.get("sort") ?? "all";
return ctx.render(
<Doc>
<div f-client-nav>
<a href="/tabs">All</a>
<a href="/tabs?sort=name">Sort by name</a>
<a href="/tabs?sort=price">Sort by price</a>
<Partial name="results">
<p class="sort">{sort}</p>
</Partial>
</div>
</Doc>,
);
});

await withBrowserApp(app, async (page, address) => {
await page.goto(`${address}/tabs`, { waitUntil: "load" });
await page.locator<HTMLAnchorElement>("a[href='/tabs']").evaluate((
link,
) => link.setAttribute("data-original-link", ""));
await page.locator("a[href='/tabs?sort=name']").click();
await waitForText(page, ".sort", "name");

const doc = parseHtml(await page.content());
assertSelector(doc, "a[href='/tabs'][data-original-link]");
assertSelector(
doc,
`a[href='/tabs?sort=name'][data-current][aria-current="page"]`,
);
assertNotSelector(
doc,
"a[href='/tabs']:is([aria-current], [data-current], [data-ancestor])",
);
assertNotSelector(doc, "a[href='/tabs?sort=price'][aria-current]");
});
},
});

Expand Down