This repository was archived by the owner on Aug 5, 2025. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork238
Streaming#352
Draft
Rich-Harris wants to merge1 commit intomainChoose a base branch fromstreaming
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Streaming#352
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
55 changes: 55 additions & 0 deletionscontent/tutorial/04-advanced-sveltekit/05-advanced-loading/04-streaming/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| --- | ||
| title: Streaming data | ||
| path: /blog/welcome | ||
| --- | ||
| 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. | ||
| 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. | ||
| Update `src/routes/blog/[slug]/+page.server.js` to load the comments: | ||
| ```js | ||
| /// file: src/routes/blog/[slug]/+page.server.js | ||
| import { error } from '@sveltejs/kit'; | ||
| import * as db from '$lib/server/data.js'; | ||
| export function load({ params }) { | ||
| const post = db.getPost(params.slug); | ||
| if (!post) throw error(404); | ||
| return { | ||
| post, | ||
| +++promises: { | ||
| comments: db.getComments(params.slug) | ||
| }+++ | ||
| }; | ||
| } | ||
| ``` | ||
| > 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. | ||
| Inside `src/routes/blog/[slug]/+page.svelte` we can now use an [`{#await ...}`](await-blocks) block to render placeholder UI while the data loads: | ||
| ```svelte | ||
| /// file: src/routes/blog/[slug]/+page.svelte | ||
| <script> | ||
| export let data; | ||
| </script> | ||
| <h1>{data.post.title}</h1> | ||
| <div>{@html data.post.content}</div> | ||
| +++<h2>Comments</h2> | ||
| {#await data.promises.comments} | ||
| <p>loading comments...</p> | ||
| {:then comments} | ||
| {#each comments as comment} | ||
| <p><strong>{comment.author}</strong> {comment.content}</p> | ||
| {/each} | ||
| {:catch} | ||
| <p>failed to load comments</p> | ||
| {/await}+++ | ||
| ``` |
75 changes: 75 additions & 0 deletions...orial/04-advanced-sveltekit/05-advanced-loading/04-streaming/app-a/src/lib/server/data.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| export function getSummaries() { | ||
| return posts.map((post) => ({ | ||
| slug: post.slug, | ||
| title: post.title | ||
| })); | ||
| } | ||
| export function getPost(slug) { | ||
| const post = posts.find((post) => post.slug === slug); | ||
| if (post) { | ||
| return { | ||
| slug: post.slug, | ||
| title: post.title, | ||
| content: post.content | ||
| }; | ||
| } | ||
| } | ||
| export async function getComments(slug) { | ||
| // simulate delay | ||
| await new Promise((fulfil) => setTimeout(fulfil, 1000)); | ||
| const post = posts.find((post) => post.slug === slug); | ||
| return post?.comments; | ||
| } | ||
| const posts = [ | ||
| { | ||
| slug: 'welcome', | ||
| title: | ||
| 'Welcome to the Aperture Science computer-aided enrichment center', | ||
| content: | ||
| '<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>', | ||
| comments: [ | ||
| { | ||
| author: 'GLaDOS', | ||
| content: "This cake is great! It's so delicious and moist!" | ||
| }, | ||
| { | ||
| author: 'Doug', | ||
| content: 'The cake is a lie' | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| slug: 'safety', | ||
| title: 'Safety notice', | ||
| content: | ||
| '<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>', | ||
| comments: [ | ||
| { | ||
| author: 'Cave', | ||
| content: "Science isn't about WHY, it's about WHY NOT!" | ||
| } | ||
| ] | ||
| }, | ||
| { | ||
| slug: 'cake', | ||
| title: 'This was a triumph', | ||
| content: "<p>I'm making a note here: HUGE SUCCESS.</p>", | ||
| comments: [ | ||
| { | ||
| author: 'GLaDOS', | ||
| content: "It's hard to overstate my satisfaction." | ||
| }, | ||
| { | ||
| author: 'GLaDOS', | ||
| content: 'Aperture Science. We do what we must because we can.' | ||
| } | ||
| ] | ||
| } | ||
| ]; |
6 changes: 6 additions & 0 deletions...al/04-advanced-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <nav> | ||
| <a href="/">home</a> | ||
| <a href="/blog">blog</a> | ||
| </nav> | ||
| <slot /> |
1 change: 1 addition & 0 deletions...rial/04-advanced-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| <p>home</p> |
7 changes: 7 additions & 0 deletions...vanced-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/blog/+layout.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import * as db from '$lib/server/data.js'; | ||
| export function load() { | ||
| return { | ||
| summaries: db.getSummaries() | ||
| }; | ||
| } |
11 changes: 11 additions & 0 deletions...04-advanced-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/blog/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <script> | ||
| export let data; | ||
| </script> | ||
| <h1>blog</h1> | ||
| <ul> | ||
| {#each data.summaries as { slug, title }} | ||
| <li><a href="/blog/{slug}">{title}</a></li> | ||
| {/each} | ||
| </ul> |
30 changes: 30 additions & 0 deletions...ed-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/blog/[slug]/+layout.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| <script> | ||
| export let data; | ||
| </script> | ||
| <div class="layout"> | ||
| <main> | ||
| <slot /> | ||
| </main> | ||
| <aside> | ||
| <h2>More posts</h2> | ||
| <ul> | ||
| {#each data.summaries as { slug, title }} | ||
| <li> | ||
| <a href="/blog/{slug}">{title}</a> | ||
| </li> | ||
| {/each} | ||
| </ul> | ||
| </aside> | ||
| </div> | ||
| <style> | ||
| @media (min-width: 640px) { | ||
| .layout { | ||
| display: grid; | ||
| gap: 2em; | ||
| grid-template-columns: 1fr 16em; | ||
| } | ||
| } | ||
| </style> |
12 changes: 12 additions & 0 deletions...d-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/blog/[slug]/+page.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { error } from '@sveltejs/kit'; | ||
| import * as db from '$lib/server/data.js'; | ||
| export function load({ params }) { | ||
| const post = db.getPost(params.slug); | ||
| if (!post) throw error(404); | ||
| return { | ||
| post | ||
| }; | ||
| } |
6 changes: 6 additions & 0 deletions...nced-sveltekit/05-advanced-loading/04-streaming/app-a/src/routes/blog/[slug]/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <script> | ||
| export let data; | ||
| </script> | ||
| <h1>{data.post.title}</h1> | ||
| <div>{@html data.post.content}</div> |
15 changes: 15 additions & 0 deletions...d-sveltekit/05-advanced-loading/04-streaming/app-b/src/routes/blog/[slug]/+page.server.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| import { error } from '@sveltejs/kit'; | ||
| import * as db from '$lib/server/data.js'; | ||
| export function load({ params }) { | ||
| const post = db.getPost(params.slug); | ||
| if (!post) throw error(404); | ||
| return { | ||
| post, | ||
| promises: { | ||
| comments: db.getComments(params.slug) | ||
| } | ||
| }; | ||
| } |
18 changes: 18 additions & 0 deletions...nced-sveltekit/05-advanced-loading/04-streaming/app-b/src/routes/blog/[slug]/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <script> | ||
| export let data; | ||
| </script> | ||
| <h1>{data.post.title}</h1> | ||
| <div>{@html data.post.content}</div> | ||
| <h2>Comments</h2> | ||
| {#await data.promises.comments} | ||
| <p>loading comments...</p> | ||
| {:then comments} | ||
| {#each comments as comment} | ||
| <p><strong>{comment.author}</strong> {comment.content}</p> | ||
| {/each} | ||
| {:catch} | ||
| <p>failed to load comments</p> | ||
| {/await} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.