Skip to content

Commit 15492cc

Browse files
authored
chore: add redirects from old URLs (#72)
- hash-based onMount for Svelte 3 docs - inside handle hook for paths from Svelte 4 docs closes #56
1 parent bd4cd06 commit 15492cc

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed

apps/svelte.dev/src/hooks.server.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,37 @@
1+
import { redirect } from '@sveltejs/kit';
2+
3+
const mappings = new Map([
4+
['/docs/getting-started', '/docs/svelte/getting-started'],
5+
['/docs/svelte-components', '/docs/svelte/component-fundamentals'],
6+
['/docs/basic-markup', '/docs/svelte/basic-markup'],
7+
['/docs/logic-blocks', '/docs/svelte/control-flow'],
8+
['/docs/special-tags', '/docs/svelte/basic-markup'], // TODO: find a new home for some of these?
9+
['/docs/element-directives', '/docs/svelte/basic-markup'],
10+
['/docs/component-directives', '/docs/svelte/component-fundamentals'],
11+
['/docs/special-elements', '/docs/svelte/special-elements'],
12+
['/docs/svelte-action', '/docs/svelte/svelte-action'],
13+
['/docs/svelte-motion', '/docs/svelte/svelte-motion'],
14+
['/docs/svelte-store', '/docs/svelte/svelte-store'],
15+
['/docs/svelte-transition', '/docs/svelte/svelte-transition'],
16+
['/docs/svelte-animate', '/docs/svelte/svelte-animate'],
17+
['/docs/svelte-easing', '/docs/svelte/svelte-easing'],
18+
['/docs/faq', '/docs/svelte/faq'],
19+
['/docs/accessibility-warnings', '/docs/svelte/accessibility-warnings'], // TODO: this doesn't exist yet
20+
['/docs/typescript', '/docs/svelte/typescript'],
21+
['/docs/custom-elements-api', '/docs/svelte/custom-elements'],
22+
['/docs/typescript', '/docs/svelte/typescript']
23+
]);
24+
125
/** @type {import('@sveltejs/kit').Handle} */
226
export async function handle({ event, resolve }) {
27+
// Best effort to redirect from Svelte 4 docs to new docs
28+
if (event.url.pathname.startsWith('/docs')) {
29+
const destination = mappings.get(event.url.pathname);
30+
if (destination) {
31+
redirect(307, destination); // TODO make 301 once we're sure
32+
}
33+
}
34+
335
const response = await resolve(event, {
436
preload: ({ type }) => type === 'js' || type === 'css' || type === 'font'
537
});

apps/svelte.dev/src/routes/docs/+page.svelte

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,61 @@
1+
<script lang="ts">
2+
import { goto } from '$app/navigation';
3+
import { page } from '$app/stores';
4+
import { onMount } from 'svelte';
5+
6+
// The old Svelte 3 docs used one giant docs page for everything, with hashes marking sections.
7+
// This mapping does a best effort to redirect to the new docs.
8+
const mappings = [
9+
['before-we-begin', 'overview'],
10+
['getting-started', 'getting-started'],
11+
['component-format', 'component-fundamentals'],
12+
['template-syntax-if', 'control-flow'],
13+
['template-syntax-each', 'control-flow'],
14+
['template-syntax-await', 'control-flow'],
15+
['template-syntax-key', 'control-flow'],
16+
['template-syntax-element-directives', 'basic-markup'],
17+
['template-syntax-element-directives-bind', 'bindings'],
18+
['template-syntax-element-directives-class', 'styles-and-classes'],
19+
['template-syntax-element-directives-style', 'styles-and-classes'],
20+
['template-syntax-element-directives-use-action', 'actions'],
21+
['template-syntax-element-directives-transition-fn', 'transitions-and-animations'],
22+
['template-syntax-element-directives-in-fn-out-fn', 'transitions-and-animations'],
23+
['template-syntax-element-directives-animate-fn', 'transitions-and-animations'],
24+
['template-syntax-component-directives', 'component fundamentals'],
25+
['template-syntax-svelte', 'special-elements'],
26+
['template-syntax', 'basic-markup'],
27+
['run-time-svelte-store', 'svelte-store'],
28+
['run-time-svelte-motion', 'svelte-motion'],
29+
['run-time-svelte-transition', 'svelte-transition'],
30+
['run-time-svelte-animate', 'svelte-animate'],
31+
['run-time-svelte-easing', 'svelte-easing'],
32+
['run-time-client-side-component-api', 'imperative-component-api'],
33+
['run-time-custom-element-api', 'custom-elements'],
34+
['run-time-server-side-component-api', 'imperative-component-api'],
35+
['run-time-svelte', 'svelte'],
36+
['run-time', 'svelte'],
37+
['compile-time', 'svelte-compiler'],
38+
['accessibility-warnings', 'TODO']
39+
];
40+
41+
function get_url_to_redirect_to() {
42+
const hash = $page.url.hash.replace(/^#/i, '');
43+
44+
if (!hash) return;
45+
46+
for (const [old_id, new_id] of mappings) {
47+
if (hash.startsWith(old_id)) return `/docs/svelte/${new_id}`;
48+
}
49+
}
50+
51+
onMount(() => {
52+
const redirect = get_url_to_redirect_to();
53+
if (redirect) {
54+
goto(redirect, { replaceState: true });
55+
}
56+
});
57+
</script>
58+
159
<div class="page">
260
<div>
361
<h2>Svelte</h2>

0 commit comments

Comments
 (0)