Skip to content

Commit e446b9b

Browse files
committed
work around Vercel's route limitation
1 parent 8456aec commit e446b9b

File tree

2 files changed

+64
-30
lines changed

2 files changed

+64
-30
lines changed

apps/svelte.dev/src/routes/e/[code]/+page.server.ts

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,38 @@
1-
import { index, remove_section } from '$lib/server/content';
1+
import { remove_section } from '$lib/server/content';
22
import { error, redirect } from '@sveltejs/kit';
33

44
// All links to warnings/errors from the Svelte compiler/runtime go through this page in order to have stable references
55
// (i.e. we can move codes between warnings/errors/different pages or even adjust codes without breaking links).
6-
const reference = index['docs/svelte/reference'].children.filter(
7-
(child) => child.slug.endsWith('-errors') || child.slug.endsWith('-warnings')
8-
);
96

10-
export const prerender = true;
11-
12-
export function entries() {
13-
return reference.flatMap((page) =>
14-
[...page.body.matchAll(/(^|\n)### (\w+)/g)].map(([, , code]) => ({ code }))
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/codes.json').then((r) =>
23+
r.json()
1524
);
16-
}
17-
18-
export function load({ params }) {
19-
// Since codes are not top level section we gotta jump through some hoops to get the right hash
20-
for (const page of reference) {
21-
const idx = page.body.indexOf(`### ${params.code}`);
22-
if (idx === -1) continue;
2325

24-
const slug_idx = page.body.lastIndexOf('\n## ', idx);
25-
26-
if (slug_idx > 0 || page.body.startsWith(`## `)) {
27-
const title = page.body.slice(slug_idx + 4, page.body.indexOf('\n', slug_idx + 4)).trim();
28-
const slug = page.sections.find((s) => s.title === title)?.slug;
29-
30-
if (!slug) {
31-
throw new Error(
32-
`Internal Error: Could not find previous title for code ${params.code} on ${page.slug}`
33-
);
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+
}
3435
}
35-
36-
redirect(307, `/${remove_section(page.slug)}#${slug}-${params.code}`);
37-
} else {
38-
redirect(307, `/${remove_section(page.slug)}#${params.code}`);
3936
}
4037
}
4138

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
const reference = index['docs/svelte/reference'].children.filter(
6+
(child) => child.slug.endsWith('-errors') || child.slug.endsWith('-warnings')
7+
);
8+
9+
// Since codes are not top level section we gotta jump through some hoops to get the right hash
10+
11+
const codes: Record<string, Record<string, string[]>> = {};
12+
13+
for (const page of reference) {
14+
const grouped: Record<string, string[]> = {};
15+
const sections = page.body.split(/(^|\n)## /g).slice(1);
16+
17+
for (const section of sections) {
18+
const lines = section.slice(section.startsWith('\n') ? 1 : 0).split('\n');
19+
const h2 = lines.shift()?.trim();
20+
21+
const h3_titles = lines
22+
.filter((line) => line.startsWith('### '))
23+
.map((line) => line.slice(4).trim());
24+
25+
if (h3_titles.length > 0) {
26+
grouped[page.sections.find((s) => s.title === h2)?.slug ?? ''] = h3_titles;
27+
}
28+
}
29+
30+
codes[page.slug] = grouped;
31+
}
32+
33+
export const prerender = true;
34+
35+
export function GET() {
36+
return json(codes);
37+
}

0 commit comments

Comments
 (0)