Skip to content

Commit f31f59e

Browse files
author
Rich Harris
committed
streaming data
1 parent 1d175a0 commit f31f59e

File tree

24 files changed

+236
-0
lines changed

24 files changed

+236
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
title: Streaming data
3+
path: /blog/welcome
4+
---
5+
6+
Ordinarily, SvelteKit will load all the data your page needs _before_ rendering it. In general this provides a better user experience than showing loading spinners and skeleton UI everywhere. But sometimes you know that some data will be slow to load, and that it would be better to render the rest of the page instead of waiting for it. In these cases, we can return promises from `load` functions.
7+
8+
In this exercise, we've added comments to the blog from [earlier](page-data), via a `getComments` function in `src/lib/server/data.js` with a simulated delay.
9+
10+
Update `src/routes/blog/[slug]/+page.server.js` to load the comments:
11+
12+
```js
13+
/// file: src/routes/blog/[slug]/+page.server.js
14+
import { error } from '@sveltejs/kit';
15+
import * as db from '$lib/server/data.js';
16+
17+
export function load({ params }) {
18+
const post = db.getPost(params.slug);
19+
20+
if (!post) throw error(404);
21+
22+
return {
23+
post,
24+
+++ promises: {
25+
comments: db.getComments(params.slug)
26+
}+++
27+
};
28+
}
29+
```
30+
31+
> If `comments` is a top-level property of the returned object, SvelteKit will automatically await it. For that reason, we must nest it inside an object. Here, we've called that object `promises`, but the name is not important.
32+
33+
Inside `src/routes/blog/[slug]/+page.svelte` we can now use an [`{#await ...}](await-blocks) block to render placeholder UI while the data loads:
34+
35+
```svelte
36+
/// file: src/routes/blog/[slug]/+page.svelte
37+
<script>
38+
export let data;
39+
</script>
40+
41+
<h1>{data.post.title}</h1>
42+
<div>{@html data.post.content}</div>
43+
44+
+++<h2>Comments</h2>
45+
46+
{#await data.promises.comments}
47+
<p>loading comments...</p>
48+
{:then comments}
49+
{#each comments as comment}
50+
<p><strong>{comment.author}</strong> {comment.content}</p>
51+
{/each}
52+
{:catch}
53+
<p>failed to load comments</p>
54+
{/await}+++
55+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
export function getSummaries() {
2+
return posts.map((post) => ({
3+
slug: post.slug,
4+
title: post.title
5+
}));
6+
}
7+
8+
export function getPost(slug) {
9+
const post = posts.find((post) => post.slug === slug);
10+
11+
if (post) {
12+
return {
13+
slug: post.slug,
14+
title: post.title,
15+
content: post.content
16+
};
17+
}
18+
}
19+
20+
export async function getComments(slug) {
21+
// simulate delay
22+
await new Promise((fulfil) => setTimeout(fulfil, 1000));
23+
24+
const post = posts.find((post) => post.slug === slug);
25+
return post?.comments;
26+
}
27+
28+
const posts = [
29+
{
30+
slug: 'welcome',
31+
title:
32+
'Welcome to the Aperture Science computer-aided enrichment center',
33+
content:
34+
'<p>We hope your brief detention in the relaxation vault has been a pleasant one.</p><p>Your specimen has been processed and we are now ready to begin the test proper.</p>',
35+
comments: [
36+
{
37+
author: 'GLaDOS',
38+
content: "This cake is great! It's so delicious and moist!"
39+
},
40+
{
41+
author: 'Doug',
42+
content: 'The cake is a lie'
43+
}
44+
]
45+
},
46+
47+
{
48+
slug: 'safety',
49+
title: 'Safety notice',
50+
content:
51+
'<p>While safety is one of many Enrichment Center Goals, the Aperture Science High Energy Pellet, seen to the left of the chamber, can and has caused permanent disabilities, such as vaporization. Please be careful.</p>',
52+
comments: [
53+
{
54+
author: 'Cave',
55+
content: "Science isn't about WHY, it's about WHY NOT!"
56+
}
57+
]
58+
},
59+
60+
{
61+
slug: 'cake',
62+
title: 'This was a triumph',
63+
content: "<p>I'm making a note here: HUGE SUCCESS.</p>",
64+
comments: [
65+
{
66+
author: 'GLaDOS',
67+
content: "It's hard to overstate my satisfaction."
68+
},
69+
{
70+
author: 'GLaDOS',
71+
content: 'Aperture Science. We do what we must because we can.'
72+
}
73+
]
74+
}
75+
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<nav>
2+
<a href="/">home</a>
3+
<a href="/blog">blog</a>
4+
</nav>
5+
6+
<slot />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<p>home</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import * as db from '$lib/server/data.js';
2+
3+
export function load() {
4+
return {
5+
summaries: db.getSummaries()
6+
};
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>blog</h1>
6+
7+
<ul>
8+
{#each data.summaries as { slug, title }}
9+
<li><a href="/blog/{slug}">{title}</a></li>
10+
{/each}
11+
</ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<div class="layout">
6+
<main>
7+
<slot />
8+
</main>
9+
10+
<aside>
11+
<h2>More posts</h2>
12+
<ul>
13+
{#each data.summaries as { slug, title }}
14+
<li>
15+
<a href="/blog/{slug}">{title}</a>
16+
</li>
17+
{/each}
18+
</ul>
19+
</aside>
20+
</div>
21+
22+
<style>
23+
@media (min-width: 640px) {
24+
.layout {
25+
display: grid;
26+
gap: 2em;
27+
grid-template-columns: 1fr 16em;
28+
}
29+
}
30+
</style>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { error } from '@sveltejs/kit';
2+
import * as db from '$lib/server/data.js';
3+
4+
export function load({ params }) {
5+
const post = db.getPost(params.slug);
6+
7+
if (!post) throw error(404);
8+
9+
return {
10+
post
11+
};
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>{data.post.title}</h1>
6+
<div>{@html data.post.content}</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { error } from '@sveltejs/kit';
2+
import * as db from '$lib/server/data.js';
3+
4+
export function load({ params }) {
5+
const post = db.getPost(params.slug);
6+
7+
if (!post) throw error(404);
8+
9+
return {
10+
post,
11+
promises: {
12+
comments: db.getComments(params.slug)
13+
}
14+
};
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script>
2+
export let data;
3+
</script>
4+
5+
<h1>{data.post.title}</h1>
6+
<div>{@html data.post.content}</div>
7+
8+
<h2>Comments</h2>
9+
10+
{#await data.promises.comments}
11+
<p>loading comments...</p>
12+
{:then comments}
13+
{#each comments as comment}
14+
<p><strong>{comment.author}</strong> {comment.content}</p>
15+
{/each}
16+
{:catch}
17+
<p>failed to load comments</p>
18+
{/await}

0 commit comments

Comments
 (0)