Skip to content

Commit 31d679a

Browse files
fix: wire up embedded examples correctly (#286)
* fix: wire up embedded examples correctly also cleans up some code in the embed Svelte component * Apply suggestions from code review Co-authored-by: Rich Harris <[email protected]> --------- Co-authored-by: Rich Harris <[email protected]>
1 parent 1879de1 commit 31d679a

File tree

4 files changed

+25
-41
lines changed

4 files changed

+25
-41
lines changed

apps/svelte.dev/src/routes/(authed)/playground/[id]/embed/+page.svelte

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,23 @@
11
<script lang="ts">
22
import { browser } from '$app/environment';
3-
import { afterNavigate } from '$app/navigation';
3+
import { afterNavigate, replaceState } from '$app/navigation';
44
import { theme } from '@sveltejs/site-kit/stores';
55
import { Repl } from '@sveltejs/repl';
66
import { mapbox_setup } from '../../../../../config.js';
77
import { onMount } from 'svelte';
88
99
let { data } = $props();
1010
11-
let version = $state(data.version);
1211
let repl = $state() as Repl;
1312
14-
function update_query_string(version: string) {
15-
const params = [];
16-
17-
if (version !== 'latest') params.push(`version=${version}`);
18-
19-
const url =
20-
params.length > 0
21-
? `/playground/${data.gist.id}/embed?${params.join('&')}`
22-
: `/playground/${data.gist.id}/embed`;
23-
24-
history.replaceState({}, 'x', url);
25-
}
26-
27-
$effect(() => {
28-
update_query_string(version);
29-
});
30-
3113
onMount(() => {
3214
if (data.version !== 'local') {
33-
fetch(`https://unpkg.com/svelte@${data.version || 'next'}/package.json`)
15+
fetch(`https://unpkg.com/svelte@${data.version}/package.json`)
3416
.then((r) => r.json())
3517
.then((pkg) => {
36-
version = pkg.version;
18+
if (pkg.version !== data.version) {
19+
replaceState(`/playground/${data.gist.id}/embed?version=${pkg.version}`, {});
20+
}
3721
});
3822
}
3923
});
@@ -44,11 +28,10 @@
4428
});
4529
});
4630
47-
const svelteUrl = $derived(
48-
browser && version === 'local'
31+
const svelteUrl =
32+
browser && data.version === 'local'
4933
? `${location.origin}/playground/local`
50-
: `https://unpkg.com/svelte@${version}`
51-
);
34+
: `https://unpkg.com/svelte@${data.version}`;
5235
5336
const relaxed = $derived(data.gist.relaxed || (data.user && data.user.id === data.gist.owner));
5437
</script>

apps/svelte.dev/src/routes/(authed)/playground/api/[id].json/+server.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ export async function GET({ params }) {
8484
}
8585

8686
export async function entries() {
87-
const { get_examples_list } = await import('$lib/server/examples/index.js');
88-
8987
return get_examples_list(examples_data)
9088
.map(({ examples }) => examples)
9189
.flatMap((val) => val.map(({ slug }) => ({ id: slug })));

apps/svelte.dev/src/routes/(authed)/playground/embed/+page.server.js

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { redirect } from '@sveltejs/kit';
2+
3+
export function load({ url }) {
4+
if (url.searchParams.has('example')) {
5+
redirect(308, construct_embed_url(url.searchParams, 'example'));
6+
} else if (url.searchParams.has('gist')) {
7+
redirect(308, construct_embed_url(url.searchParams, 'gist'));
8+
} else {
9+
redirect(308, '/playground/hello-world/embed');
10+
}
11+
}
12+
13+
function construct_embed_url(searchParams: URLSearchParams, param: string) {
14+
const cleaned_params = new URLSearchParams(searchParams);
15+
cleaned_params.delete(param);
16+
return `/playground/${searchParams.get(param)}/embed?${cleaned_params.toString()}`;
17+
}

0 commit comments

Comments
 (0)