-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add lazy data load example with isDataRequest check [skip ci]
- Loading branch information
Showing
5 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
12 changes: 12 additions & 0 deletions
12
apps/playground/src/routes/(play)/utilities/lazy/+page.server.ts
This file contains 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 @@ | ||
// https://geoffrich.net/posts/conditionally-stream-data/ | ||
export const load = async ({ isDataRequest, fetch }) => { | ||
const promise: Promise<{ | ||
hello: string; | ||
}> = fetch('/api/greeting').then((res) => res.json()); | ||
|
||
return { | ||
greeting: { | ||
promise: isDataRequest ? promise : await promise | ||
} | ||
}; | ||
}; |
11 changes: 11 additions & 0 deletions
11
apps/playground/src/routes/(play)/utilities/lazy/+page.svelte
This file contains 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 lang="ts"> | ||
export let data; | ||
</script> | ||
|
||
{#await data.greeting.promise} | ||
loading... | ||
{:then value} | ||
<pre>{JSON.stringify(value, null, 2)}</pre> | ||
{:catch error} | ||
ERROR : {error} | ||
{/await} |
This file contains 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,8 @@ | ||
import { json } from '@sveltejs/kit'; | ||
|
||
export const GET = async () => { | ||
await new Promise((res) => setTimeout(res, 1500)); | ||
return json({ | ||
hello: 'world' | ||
}); | ||
}; |
This file contains 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