Skip to content

Commit 2f51473

Browse files
authored
feat: add stable route for all warning/error codes (#965)
* feat: add stable route for all warning/error codes This establishes a new route `e/[code]` which serves as a stable reference for all of our warning/error codes. All links to warnings/errors from the Svelte compiler/runtime should go through this route in order to have stable references. That way we can move codes between warnings/errors/different pages or even adjust codes without breaking links. Part of sveltejs/svelte#11305 * prerender entries * work around Vercel's route limitation * another workaround
1 parent 337799e commit 2f51473

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-4
lines changed

apps/svelte.dev/src/lib/server/content.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ export const blog_posts = index.blog.children
6060
return a.date < b.date ? 1 : -1;
6161
});
6262

63+
export function remove_section(slug: string) {
64+
return slug.replace(/\/[^/]+(\/[^/]+)$/g, '$1');
65+
}
66+
6367
/**
6468
* Create docs index, which is basically the same structure as the original index
6569
* but with adjusted slugs (the section part is omitted for cleaner URLs), separated
6670
* topics/pages and next/prev adjusted so that they don't point to different topics.
6771
*/
6872
function create_docs() {
69-
function remove_section(slug: string) {
70-
return slug.replace(/\/[^/]+(\/[^/]+)$/g, '$1');
71-
}
72-
7373
let docs: {
7474
/** The top level entries/packages: svelte/kit/etc. Key is the topic */
7575
topics: Record<string, Document>;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { remove_section } from '$lib/server/content';
2+
import { error, redirect } from '@sveltejs/kit';
3+
4+
// All links to warnings/errors from the Svelte compiler/runtime go through this page in order to have stable references
5+
// (i.e. we can move codes between warnings/errors/different pages or even adjust codes without breaking links).
6+
7+
// Right now we can't prerender this because we would hit a "too many routes" error on Vercel,
8+
// for which we need to implement https://github.com/sveltejs/kit/issues/9032
9+
// const reference = index['docs/svelte/reference'].children.filter(
10+
// (child) => child.slug.endsWith('-errors') || child.slug.endsWith('-warnings')
11+
// );
12+
//
13+
// export const prerender = true;
14+
//
15+
// export function entries() {
16+
// return reference.flatMap((page) =>
17+
// [...page.body.matchAll(/(^|\n)### (\w+)/g)].map(([, , code]) => ({ code }))
18+
// );
19+
// }
20+
21+
export async function load({ params, fetch }) {
22+
const codes: Record<string, Record<string, string[]>> = await fetch('/e/tmp/codes.json').then(
23+
(r) => r.json()
24+
);
25+
26+
for (const url of Object.keys(codes)) {
27+
const page = codes[url];
28+
for (const [h2, h3] of Object.entries(page)) {
29+
if (h3.includes(params.code)) {
30+
if (h2) {
31+
redirect(307, `/${remove_section(url)}#${h2}-${params.code}`);
32+
} else {
33+
redirect(307, `/${remove_section(url)}#${params.code}`);
34+
}
35+
}
36+
}
37+
}
38+
39+
error(404, 'Not found');
40+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { index } from '$lib/server/content';
2+
import { json } from '@sveltejs/kit';
3+
4+
// Temporary workaround for the problem described in [code]/+page.server.ts
5+
// In a nested folder because of https://github.com/sveltejs/kit/issues/12778
6+
7+
const reference = index['docs/svelte/reference'].children.filter(
8+
(child) => child.slug.endsWith('-errors') || child.slug.endsWith('-warnings')
9+
);
10+
11+
// Since codes are not top level section we gotta jump through some hoops to get the right hash
12+
13+
const codes: Record<string, Record<string, string[]>> = {};
14+
15+
for (const page of reference) {
16+
const grouped: Record<string, string[]> = {};
17+
const sections = page.body.split(/(^|\n)## /g).slice(1);
18+
19+
for (const section of sections) {
20+
const lines = section.slice(section.startsWith('\n') ? 1 : 0).split('\n');
21+
const h2 = lines.shift()?.trim();
22+
23+
const h3_titles = lines
24+
.filter((line) => line.startsWith('### '))
25+
.map((line) => line.slice(4).trim());
26+
27+
if (h3_titles.length > 0) {
28+
grouped[page.sections.find((s) => s.title === h2)?.slug ?? ''] = h3_titles;
29+
}
30+
}
31+
32+
codes[page.slug] = grouped;
33+
}
34+
35+
export const prerender = true;
36+
37+
export function GET() {
38+
return json(codes);
39+
}

0 commit comments

Comments
 (0)