Skip to content

Commit

Permalink
handle redirects in layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Oct 2, 2024
1 parent c345928 commit ad8bae9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 8 deletions.
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[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
};
}
6 changes: 1 addition & 5 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];

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 ad8bae9

Please sign in to comment.