Skip to content

Commit 9b99248

Browse files
authored
prevent pages from clobbering each other (#213)
* handle redirects in layout * throw error if pages conflict with each other
1 parent 3006ee0 commit 9b99248

File tree

4 files changed

+18
-15
lines changed

4 files changed

+18
-15
lines changed
File renamed without changes.

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,6 @@ function create_docs() {
6060
return slug.replace(/\/[^/]+(\/[^/]+)$/g, '$1');
6161
}
6262

63-
function remove_docs(slugs: string) {
64-
return slugs.replace(/^docs\//, '');
65-
}
66-
6763
let docs: {
6864
/** The top level entries/packages: svelte/kit/etc. Key is the topic */
6965
topics: Record<string, Document>;
@@ -74,7 +70,7 @@ function create_docs() {
7470
for (const topic of index.docs.children) {
7571
const pkg = topic.slug.split('/')[1];
7672
const sections = topic.children;
77-
const transformed_topic: Document = (docs.topics[remove_docs(topic.slug)] = {
73+
const transformed_topic: Document = (docs.topics[topic.slug] = {
7874
...topic,
7975
children: []
8076
});
@@ -90,7 +86,12 @@ function create_docs() {
9086

9187
for (const page of pages) {
9288
const slug = remove_section(page.slug);
93-
const transformed_page: Document = (docs.pages[remove_docs(slug)] = {
89+
90+
if (Object.hasOwn(docs.pages, slug)) {
91+
throw new Error(`${docs.pages[slug].file} conflicts with ${page.file}`);
92+
}
93+
94+
const transformed_page: Document = (docs.pages[slug] = {
9495
...page,
9596
slug,
9697
next: page.next?.slug.startsWith(`docs/${pkg}/`)
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,22 @@
11
import { docs } from '$lib/server/content';
2+
import { redirect } from '@sveltejs/kit';
23
import { error } from '@sveltejs/kit';
34

45
export const prerender = true;
56

67
export async function load({ params }) {
7-
const page = docs.topics[params.path.split('/')[0]];
8+
const topic = params.path.split('/')[0];
9+
const document = docs.topics[`docs/${topic}`];
810

9-
if (!page) {
11+
if (!document) {
1012
error(404, 'Not found');
1113
}
1214

15+
if (params.path === topic) {
16+
redirect(307, `/${document.children[0].children[0].slug}`);
17+
}
18+
1319
return {
14-
sections: page.children
20+
sections: document.children
1521
};
1622
}

apps/svelte.dev/src/routes/docs/[...path]/+page.server.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { docs } from '$lib/server/content';
22
import { render_content } from '$lib/server/renderer';
3-
import { error, redirect } from '@sveltejs/kit';
3+
import { error } from '@sveltejs/kit';
44

55
export async function load({ params }) {
6-
const document = docs.pages[params.path];
6+
const document = docs.pages[`docs/${params.path}`];
77

88
if (!document) {
9-
const topic = docs.topics[params.path];
10-
if (topic) {
11-
redirect(307, `/${topic.children[0].children[0].slug}`);
12-
}
139
error(404);
1410
}
1511

0 commit comments

Comments
 (0)