Skip to content

Commit

Permalink
prevent pages from clobbering each other (#213)
Browse files Browse the repository at this point in the history
* handle redirects in layout

* throw error if pages conflict with each other
  • Loading branch information
Rich-Harris authored Oct 2, 2024
1 parent 3006ee0 commit 9b99248
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
13 changes: 7 additions & 6 deletions apps/svelte.dev/src/lib/server/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ function create_docs() {
return slug.replace(/\/[^/]+(\/[^/]+)$/g, '$1');
}

function remove_docs(slugs: string) {
return slugs.replace(/^docs\//, '');
}

let docs: {
/** The top level entries/packages: svelte/kit/etc. Key is the topic */
topics: Record<string, Document>;
Expand All @@ -74,7 +70,7 @@ function create_docs() {
for (const topic of index.docs.children) {
const pkg = topic.slug.split('/')[1];
const sections = topic.children;
const transformed_topic: Document = (docs.topics[remove_docs(topic.slug)] = {
const transformed_topic: Document = (docs.topics[topic.slug] = {
...topic,
children: []
});
Expand All @@ -90,7 +86,12 @@ function create_docs() {

for (const page of pages) {
const slug = remove_section(page.slug);
const transformed_page: Document = (docs.pages[remove_docs(slug)] = {

if (Object.hasOwn(docs.pages, slug)) {
throw new Error(`${docs.pages[slug].file} conflicts with ${page.file}`);
}

const transformed_page: Document = (docs.pages[slug] = {
...page,
slug,
next: page.next?.slug.startsWith(`docs/${pkg}/`)
Expand Down
12 changes: 9 additions & 3 deletions apps/svelte.dev/src/routes/docs/[...path]/+layout.server.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
import { docs } from '$lib/server/content';
import { redirect } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';

export const prerender = true;

export async function load({ params }) {
const page = docs.topics[params.path.split('/')[0]];
const topic = params.path.split('/')[0];
const document = docs.topics[`docs/${topic}`];

if (!page) {
if (!document) {
error(404, 'Not found');
}

if (params.path === topic) {
redirect(307, `/${document.children[0].children[0].slug}`);
}

return {
sections: page.children
sections: document.children
};
}
8 changes: 2 additions & 6 deletions apps/svelte.dev/src/routes/docs/[...path]/+page.server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { docs } from '$lib/server/content';
import { render_content } from '$lib/server/renderer';
import { error, redirect } from '@sveltejs/kit';
import { error } from '@sveltejs/kit';

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

if (!document) {
const topic = docs.topics[params.path];
if (topic) {
redirect(307, `/${topic.children[0].children[0].slug}`);
}
error(404);
}

Expand Down

0 comments on commit 9b99248

Please sign in to comment.