Active links: a query-less link stays aria-current="page" after navigating to the same path with a query (two current links)

Open Beginner friendly
#3,920 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
76/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
deno, typescript

Research direction

Start at matchesUrl() in packages/fresh/src/runtime/shared_internal.ts, which is the single place the comparison is decided; the client caller is updateLinks() in packages/fresh/src/runtime/client/partials.ts, and the server pass uses the same helper, so one fix covers both. Read how needleSearch/currentSearch are derived (missing vs empty string) before changing the branch, then make equal paths return Current only when the searches match and decide whether a query-less link falls back to Ancestor or None as the issue suggests. Reproduce with the /list tab example, and check done by confirming only /list?tab=open carries aria-current="page" on both a client navigation and a direct load, with existing active-link tests still passing.

Written by the indexing model from the issue text.

Description

Summary

After a client navigation to a URL that has a query (/list?tab=open), a link to the same path without a query (/list) is still marked aria-current="page", so two links claim to be the current page. The server-side active-link pass has the same result for a document load of /list?tab=open.

Found on Fresh 2.3.3, after #3755. Current main has the same code.

Cause

matchesUrl() (packages/fresh/src/runtime/shared_internal.ts) compares the search only when the link has one:

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;
  }
  return UrlMatchKind.Current;
}

A link with no query (needleSearch === "") is therefore Current for every query on that path. updateLinks() (runtime/client/partials.ts) then sets data-current and aria-current="page" on it.

Reproducer

// routes/list.tsx
export default function List(ctx) {
  const tab = ctx.url.searchParams.get("tab") ?? "all";
  return (
    <body f-client-nav>
      <nav>
        <a href="/list">All</a>
        <a href="/list?tab=open">Open</a>
        <a href="/list?tab=done">Done</a>
      </nav>
      <Partial name="list"><p>Showing: {tab}</p></Partial>
    </body>
  );
}
  1. Load /list, then click "Open" (a client navigation).
  2. Inspect the links: both <a href="/list"> and <a href="/list?tab=open"> carry aria-current="page" and data-current="true".
  3. Loading /list?tab=open directly gives the same two current links (server pass).

Expected: only /list?tab=open is current. The query-less /list should not be Current when the current URL has a different, non-empty query.

The same thing happens with <a href="/list" f-partial="/partials/list"> tabs that swap one region. #3755's escape hatch (an explicit aria-current on the link) works, but it puts the burden on every app with query-driven tabs or filters.

Related observation

The same rule marks every same-path link with a different query as Ancestor, i.e. aria-current="true": row links such as /list?item=42, pager links (/list?page=2), a drawer's close link. Screen readers announce each of them as "current". These are siblings of the current URL, not ancestors, so None would fit better for a same-path link whose query differs.

Suggested fix

In matchesUrl(), when the paths are equal, return Current only if the searches are equal (treating missing and empty as equal). Otherwise return None, or Ancestor only when the link has no query, if keeping "section" highlighting for a query-less base link is a goal.

Dominant language
TypeScript
Stars
13.8k
Forks
757
Avg merge
3m
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from freshframework/fresh

All issues in freshframework/fresh

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.