"use client"; // Client fetch for a site's shipped /search-aliases.json (the global dictionary // merged with per-site overrides at compose time). Mirrors summariesCache / // subsCache. A missing file (older bundle, or a dev server without the composed // asset) resolves to an empty list rather than an error — alias suggestions are // purely additive, so their absence must never break search. import { useQuery } from "@tanstack/react-query"; import { ArchiveHttpError } from "../lib/archive/reader"; import { readerFor } from "../lib/archive/readers"; import { coerceAliasConfig, type SearchAlias } from "../lib/searchAliases"; const EMPTY: SearchAlias[] = []; // ANY status the archive itself returned resolves empty — a 404 because the // site ships no dictionary, but a 500 or a 403 the same way. That is the exact // behaviour of the `if (!r.ok) return []` this replaced, kept deliberately: // alias suggestions are additive, so a server that answers badly should cost // the chip, not the search. // // A TRANSPORT failure is different and is rethrown, because it is not an answer // at all. react-query then retries it rather than caching "this site has no // aliases" for the session — the query below has staleTime: Infinity, so a // swallowed blip would be permanent. export async function fetchAliases(origin = ""): Promise { try { return coerceAliasConfig(await readerFor(origin).readAliasConfig()).aliases; } catch (err) { if (err instanceof ArchiveHttpError) return []; throw err; } } export function useSearchAliases(origin = ""): SearchAlias[] { const { data } = useQuery({ queryKey: ["search-aliases", origin], queryFn: () => fetchAliases(origin), staleTime: Infinity, // static per export build }); return data ?? EMPTY; }