Skip to content

Commit cb9b128

Browse files
committed
chore: separate tutorial URLs into svelte and kit
- gives us more leeway with duplicated slugs if needed later on - makes URL a bit more organized - will make it easier to set dedicated headers for the SvelteKit tutorial (#301) - fixes a bug with redirects not being picked up due to prerendering not coming across old slugs
1 parent 2550097 commit cb9b128

29 files changed

+59
-36
lines changed

apps/svelte.dev/src/routes/content.json/+server.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { index, docs as _docs } from '$lib/server/content';
22
import { json } from '@sveltejs/kit';
33
import { markedTransform, normalizeSlugify, removeMarkdown } from '@sveltejs/site-kit/markdown';
44
import type { Block } from '@sveltejs/site-kit/search';
5+
import { get_slug } from '../tutorial/[...slug]/content.server';
56

67
export const prerender = true;
78

@@ -17,13 +18,12 @@ function get_href(parts: string[]) {
1718

1819
async function content() {
1920
const blocks: Block[] = [];
20-
const breadcrumbs: string[] = [];
2121
const docs = Object.values(_docs.pages).concat(
22-
index.tutorial.children.flatMap((topic) =>
23-
topic.children.flatMap((section) =>
24-
section.children.map((entry) => ({
25-
...entry,
26-
slug: `tutorial/${entry.slug.split('/').pop()}`
22+
index.tutorial.children.flatMap((part) =>
23+
part.children.flatMap((chapter) =>
24+
chapter.children.map((exercise) => ({
25+
...exercise,
26+
slug: get_slug(part, exercise)
2727
}))
2828
)
2929
)

apps/svelte.dev/src/routes/tutorial/+page.js

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { redirect } from '@sveltejs/kit';
2+
import { load_exercise, parts } from './content.server';
3+
4+
export const prerender = true;
5+
6+
const redirects = new Map([
7+
['reactive-assignments', 'svelte/state'],
8+
['reactive-declarations', 'svelte/derived-state'],
9+
['reactive-statements', 'svelte/effects'],
10+
['updating-arrays-and-objects', 'svelte/deep-state'],
11+
['event-modifiers', 'svelte/capturing'],
12+
['dom-event-forwarding', 'svelte/spreading-events']
13+
]);
14+
15+
export async function load({ params }) {
16+
if (!params.slug || params.slug === 'svelte') redirect(307, '/tutorial/svelte/welcome-to-svelte');
17+
if (params.slug === 'kit') redirect(307, '/tutorial/introducing-sveltekit');
18+
19+
const r = redirects.get(params.slug);
20+
if (r) redirect(307, r);
21+
if (!params.slug.includes('/')) redirect(307, `/tutorial/svelte/${params.slug}`);
22+
23+
return {
24+
exercise: await load_exercise(params.slug)
25+
};
26+
}
27+
28+
export function entries() {
29+
// These are not findable by the router, but we need to know about them for redirects
30+
31+
// Old tutorial/... to new tutorial/svelte/...
32+
const entries = parts
33+
.filter((part) => !part.slug.includes('sveltekit'))
34+
.flatMap((part) => part.chapters)
35+
.flatMap((chapter) => {
36+
return chapter.exercises.map((exercise) => {
37+
const slug = exercise.slug.split('/').pop()!;
38+
return { slug: redirects.get(slug) || slug };
39+
});
40+
});
41+
42+
// So that redirects from these URLs to /tutorial/<svelte/kit>/... work
43+
entries.push({ slug: 'svelte' }, { slug: 'kit' });
44+
45+
return entries;
46+
}

apps/svelte.dev/src/routes/tutorial/[slug]/content.server.ts renamed to apps/svelte.dev/src/routes/tutorial/[...slug]/content.server.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ let prev: null | { slug: string; title: string } = null;
3333

3434
let files: Record<string, string> = {};
3535

36+
export function get_slug(part: Document, exercise: Document) {
37+
const topic = part.slug.split('/').pop()!.includes('sveltekit') ? 'kit' : 'svelte';
38+
return `tutorial/${topic}/${exercise.slug.split('/').pop()}`;
39+
}
40+
3641
export const parts: PartStub[] = index.tutorial.children.map((part) => {
3742
return {
3843
slug: part.slug,
@@ -42,7 +47,7 @@ export const parts: PartStub[] = index.tutorial.children.map((part) => {
4247
slug: chapter.slug.split('/').pop()!,
4348
title: chapter.metadata.title,
4449
exercises: chapter.children.map((exercise) => {
45-
const slug = exercise.slug.split('/').pop()!;
50+
const slug = get_slug(part, exercise).slice('tutorial/'.length);
4651

4752
const stub: ExerciseStub = {
4853
slug,
@@ -200,6 +205,7 @@ const default_renderer: Partial<Renderer> = {
200205

201206
export async function load_exercise(slug: string): Promise<Exercise> {
202207
if (!(slug in lookup)) {
208+
console.log(slug, Object.keys(lookup));
203209
error(404, 'No such tutorial found');
204210
}
205211

apps/svelte.dev/src/routes/tutorial/[slug]/+page.server.js

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)