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

Aperta Adatta ai principianti
#3,920 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
76/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
deno, typescript

Direzione di ricerca

Parti da matchesUrl() in packages/fresh/src/runtime/shared_internal.ts, che è l'unico punto in cui viene deciso il confronto; il chiamante lato client è updateLinks() in packages/fresh/src/runtime/client/partials.ts, e il passaggio lato server usa lo stesso helper, quindi una sola correzione copre entrambi. Leggi come vengono derivati needleSearch/currentSearch (mancante vs stringa vuota) prima di cambiare il branch, poi fai restituire Current ai percorsi uguali solo quando le ricerche coincidono e decidi se un link senza query ricade su Ancestor o None come suggerisce l'issue. Riproduci con l'esempio della tab /list, e verifica che sia fatto confermando che solo /list?tab=open porta aria-current="page" sia in una navigazione client sia in un caricamento diretto, con i test esistenti sui active-link ancora superati.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.

Lingua principale
TypeScript
Stelle
13.8k
Fork
757
Merge medio
3m
PR unite (30g)
2

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di freshframework/fresh

Tutte le issue di freshframework/fresh

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.