Skip to content

Commit 1ebd7b7

Browse files
authored
fix(docs): order llms-full.txt by the navigation tree (#201)
Fixes #196 source.getPages(LANG) hands back pages in whatever order the loader finished reading them — stable within one build, not across builds. Two consecutive builds of an untouched tree emitted this file with the whole configure/ group and the whole deploy/ group swapped: the same 79 pages, different bytes, nothing in the repository moved. The repair is not "sort it". Unlike sitemap.xml, where entry order carries nothing to a crawler, the order pages appear in here is read — this file is one long document. Alphabetical would be reproducible and worse than the accident it replaced, scattering build/, deploy/ and operate/ through a flat A-to-Z list. source.getPageTree(LANG) is meta.json order, which is the site navigation, which is the order a reader already knows — and it is what llms.txt already walks, so the index and the full text now agree on sequence instead of contradicting each other. Reproducibility follows from fixing the meaning rather than the reverse. Membership decided by the tree rather than the file system now, matching the sidebar and llms.txt: a page no meta.json lists is absent from all three together rather than from two of them quietly. An unresolvable node throws rather than being filtered, so a page can never drop out silently. Verified: two pre-fix builds differ (a6b849f1 vs 80f7471b), two post-fix builds are identical (e84953ee). Membership unchanged — 79 page blocks, 93 #-lines, 798 headings, 646 link targets, 685243 bytes, and the sorted-line-multiset digest 35c58ede on all four builds, which only a whole-block permutation leaves fixed. The 79-page set confirmed three independent ways before the change was trusted.
1 parent 453cdba commit 1ebd7b7

1 file changed

Lines changed: 60 additions & 11 deletions

File tree

‎apps/docs/app/llms-full.txt/route.ts‎

Lines changed: 60 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { flattenTree } from 'fumadocs-core/page-tree';
12
import { i18n } from '@/lib/i18n';
23
import { SITE_URL } from '@/lib/seo';
34
import { getLLMText, source } from '@/lib/source';
@@ -7,12 +8,14 @@ export const revalidate = false;
78
/**
89
* English only, by `AGENTS.md` rule 1 — English is the single source of truth
910
* and every `*.<locale>.mdx` is a derived artifact. This is the same pin
10-
* `llms.txt` carries, for the same reason: `source.getPages()` filters by
11-
* locale **only when a language is passed** — called bare it returns every
12-
* language's page set concatenated, so this route used to emit all 335 pages
13-
* across 7 locales (4.6 MB, the same page appearing up to seven times) instead
14-
* of the 79 English ones. The locale text stays reachable per page through the
15-
* `.mdx` rewrite and the locale URLs announced at the end of `llms.txt`.
11+
* `llms.txt` carries, for the same reason: `source.getPages()` and
12+
* `source.getPageTree()` alike filter by locale **only when a language is
13+
* passed** — called bare they list every language, which is how this route
14+
* once emitted all 335 pages across 7 locales (4.6 MB, the same page appearing
15+
* up to seven times) instead of the 79 English ones. Which of the two this
16+
* route enumerates has changed; that the argument is mandatory has not. The
17+
* locale text stays reachable per page through the `.mdx` rewrite and the
18+
* locale URLs announced at the end of `llms.txt`.
1619
*/
1720
const LANG = i18n.defaultLanguage;
1821

@@ -71,17 +74,63 @@ function absoluteLinks(text: string, pageUrl: string): string {
7174
return lines.join('\n');
7275
}
7376

77+
/**
78+
* Every English page, in the order the site navigation puts them.
79+
*
80+
* `source.getPages(LANG)` — what this route used to enumerate — hands back
81+
* pages in whatever order the loader finished reading them. That is stable
82+
* within one build and not across builds: two consecutive builds of an
83+
* untouched tree emitted this file with the whole `configure/` group and the
84+
* whole `deploy/` group swapped, so the bytes changed for the same 79 pages
85+
* and nothing in the repository had moved.
86+
*
87+
* The repair is not "sort it". Unlike `sitemap.xml`, where entry order carries
88+
* nothing to a crawler, **the order pages appear in here is read** — this file
89+
* is one long document. So a deterministic order is necessary and not
90+
* sufficient: alphabetical would be reproducible and *worse* than the accident
91+
* it replaced, scattering `build/`, `deploy/` and `operate/` through a flat
92+
* A-to-Z list.
93+
*
94+
* `source.getPageTree(LANG)` is `meta.json` order, which is the site
95+
* navigation, which is the order a reader of these pages already knows. It is
96+
* also what `llms.txt` already walks, so the index and the full text now agree
97+
* on sequence instead of contradicting each other. Reproducibility follows
98+
* from fixing the meaning rather than the other way round.
99+
*
100+
* Membership is unchanged. `flattenTree` yields a folder's index page followed
101+
* by its children, recursively, and every one of the 79 English pages is
102+
* reachable from a `meta.json`. What decides membership is now the tree rather
103+
* than the file system — the same rule the sidebar and `llms.txt` already
104+
* follow, so a page that no `meta.json` lists is absent from all three
105+
* together rather than from two of them quietly.
106+
*/
107+
function navigationPages() {
108+
return flattenTree(source.getPageTree(LANG).children).map((node) => {
109+
const page = source.getNodePage(node, LANG);
110+
111+
// Unreachable unless fumadocs hands back a page node whose `$ref` resolves
112+
// to nothing. Loud on purpose: the alternative is a page dropping out of
113+
// this file without a trace, which is a worse defect than the ordering one
114+
// this function exists to fix.
115+
if (!page) {
116+
throw new Error(
117+
`llms-full.txt: page tree node ${String(node.$id ?? node.url)} resolves to no page`,
118+
);
119+
}
120+
121+
return page;
122+
});
123+
}
124+
74125
export async function GET() {
75126
// Each page is rewritten against its own URL rather than the joined body
76127
// against the site root: `./objectql` means something different on
77128
// `/docs/reference/cel` than it does three sections away, and a page whose
78129
// text left a code fence unclosed cannot then leak that state into the next
79130
// page's links.
80-
const scan = source
81-
.getPages(LANG)
82-
.map(async (page) =>
83-
absoluteLinks(await getLLMText(page), `${SITE_URL}${page.url}`),
84-
);
131+
const scan = navigationPages().map(async (page) =>
132+
absoluteLinks(await getLLMText(page), `${SITE_URL}${page.url}`),
133+
);
85134
const scanned = await Promise.all(scan);
86135

87136
return new Response(scanned.join('\n\n'));

0 commit comments

Comments
 (0)