Skip to content

Commit 3b85544

Browse files
authored
fix: collapse doubled section prefix in docs .md cross-links (#3005)
Part of apify/apify-web#6665 `@signalwire/docusaurus-plugin-llms-txt@1.2.2` repeats the section prefix in the `.md` files and `llms*.txt` it generates for sites served under a sub-path, so roughly 6,700 links under `/sdk/{js,python}`, `/api/client/{js,python}` and `/cli` 404. Two changes at our edge: - A 301 in `nginx.conf` collapsing the doubled prefix, so those links resolve in one hop. Verified against the real GitHub Pages origins: 1 redirect, 200, correct `text/markdown`. - The same substitution in `joinLlmsFiles.mjs`, so the root `llms-full.txt` is correct in its own text. Verified against the real production file: 6,664 doubled links to 0, nothing else touched. `checkLlmsSize.mjs` now fails the build if any come back. The sources are fixed separately in apify/apify-cli#1455, apify/apify-sdk-js#737, apify/apify-sdk-python#1135, apify/apify-client-js#1072 and apify/apify-client-python#1070. Drop the `joinLlmsFiles.mjs` substitution once all five have deployed. Keep the redirect: the doubled URLs stay in search indexes and agent caches long after the sources are fixed. It only collapses a repeated same-section prefix. The plugin also prefixes cross-section links (`/sdk/js/cli/docs/...`), 5 of them in the root `llms-full.txt`, which neither this nor the source fix addresses.
1 parent 9241a90 commit 3b85544

4 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎.github/workflows/test.yaml‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,18 @@ jobs:
230230
assert_header "http://localhost:8080/cli/llms.txt" "Content-Type" "text/markdown"
231231
assert_header "http://localhost:8080/cli/llms-full.txt" "Content-Type" "text/markdown"
232232
233+
echo "🧪 Checking doubled section prefix redirect..."
234+
# Regression tests for the llms-txt plugin workaround in nginx.conf.
235+
# Doubled prefixes redirect to the canonical URL and keep the query
236+
# string. Remove together with the workaround.
237+
assert_status "http://localhost:8080/sdk/js/sdk/js/docs/introduction/quick-start" "301"
238+
assert_header "http://localhost:8080/sdk/js/sdk/js/docs/introduction/quick-start" "Location" "^Location: http://[^/]*/sdk/js/docs/introduction/quick-start$"
239+
assert_header "http://localhost:8080/sdk/js/sdk/js/docs/introduction/quick-start.md?x=1" "Location" "^Location: http://[^/]*/sdk/js/docs/introduction/quick-start[.]md[?]x=1$"
240+
assert_header "http://localhost:8080/sdk/python/sdk/python/docs/changelog" "Location" "^Location: http://[^/]*/sdk/python/docs/changelog$"
241+
assert_header "http://localhost:8080/api/client/js/api/client/js/docs/changelog" "Location" "^Location: http://[^/]*/api/client/js/docs/changelog$"
242+
assert_header "http://localhost:8080/api/client/python/api/client/python/docs/changelog" "Location" "^Location: http://[^/]*/api/client/python/docs/changelog$"
243+
assert_header "http://localhost:8080/cli/cli/docs/changelog" "Location" "^Location: http://[^/]*/cli/docs/changelog$"
244+
233245
echo "✅ All Nginx header checks passed."
234246
235247
- name: Verify per-page .md navigation headers

‎nginx.conf‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ server {
6363
rewrite ^(.+)/$ $1$is_args$args? redirect;
6464
}
6565

66+
# @signalwire/docusaurus-plugin-llms-txt 1.2.2 repeats the section prefix in the
67+
# .md files and llms*.txt it generates for sites served under a sub-path, so links
68+
# such as /sdk/python/sdk/python/docs/... 404. The five sub-sites still pin ^1.2.2.
69+
# This redirect makes those links resolve in one hop. Keep it after the sub-sites
70+
# stop generating them: the doubled URLs stay in search indexes and agent caches.
71+
# It only collapses a repeated same-section prefix. The plugin also prefixes
72+
# cross-section links (/sdk/js/cli/docs/...), which this cannot detect.
73+
location ~ "^/(sdk/js|sdk/python|api/client/js|api/client/python|cli)/\1(/.*)?$" {
74+
return 301 /$1$2$is_args$args;
75+
}
76+
6677
location / {
6778
set $rewrite_condition "$serve_markdown$has_no_extension";
6879
set $proxy_path $request_uri;

‎scripts/checkLlmsSize.mjs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ if (llmsChars === null || llmsFullChars === null) {
2727
process.exit(1);
2828
}
2929

30+
// Guards the substitution in joinLlmsFiles.mjs. The sub-sites generate links with their
31+
// section prefix repeated; if the substitution stops matching, the joined files silently
32+
// fill up with URLs that only resolve through the redirect in nginx.conf.
33+
const DOUBLED_SECTION_PREFIX =
34+
/https:\/\/docs\.apify\.com\/(sdk\/js|sdk\/python|api\/client\/js|api\/client\/python|cli)\/\1\//g;
35+
36+
for (const filePath of [llmsPath, llmsFullPath]) {
37+
const matches = (await fs.readFile(filePath, 'utf8')).match(DOUBLED_SECTION_PREFIX);
38+
if (matches) {
39+
console.error(`\nERROR: ${filePath} has ${matches.length} links with a doubled section prefix`);
40+
process.exitCode = 1;
41+
}
42+
}
43+
3044
console.log(`llms.txt: ${llmsChars.toLocaleString()} characters`);
3145
console.log(`llms-full.txt: ${llmsFullChars.toLocaleString()} characters`);
3246

‎scripts/joinLlmsFiles.mjs‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,18 @@ async function joinFiles() {
253253
console.log('Wrote llms-full.txt to build/');
254254
}
255255

256+
// WORKAROUND: @signalwire/docusaurus-plugin-llms-txt 1.2.2 doubles the section prefix in
257+
// cross-links on sites served under a sub-path, so the SDK/client/CLI sections we fetch above
258+
// arrive with links such as /sdk/python/sdk/python/docs/... Fixed upstream in 2.0.0-alpha.6;
259+
// those sites still pin ^1.2.2. Remove once they stop generating the doubled prefix.
260+
const DOUBLED_SECTION_PREFIX =
261+
/(https:\/\/docs\.apify\.com\/(sdk\/js|sdk\/python|api\/client\/js|api\/client\/python|cli))\/\2\//g;
262+
256263
async function sanitizeFile(filePath) {
257264
const content = await fs.readFile(filePath, 'utf8');
258-
const sanitizedContent = content.replace(/<[^>]*>/g, ''); // Remove HTML tags
265+
const sanitizedContent = content
266+
.replace(/<[^>]*>/g, '') // Remove HTML tags
267+
.replace(DOUBLED_SECTION_PREFIX, '$1/');
259268
await fs.writeFile(filePath, sanitizedContent, 'utf8');
260269
console.log(`Sanitized ${filePath}`);
261270
}

0 commit comments

Comments
 (0)