Skip to content

Commit

Permalink
fix: add lazy data load example with isDataRequest check [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
xmlking committed Sep 16, 2023
1 parent 9de4d40 commit e514b5a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion apps/playground/src/lib/links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export const menuNavLinks: Record<string, Array<{ title: string; list: List }>>
label: 'Modals',
keywords: 'overlay, dialog, notification, alert, confirm, prompt, multiple, form, list, embed, video'
},
{ href: '/utilities/popups', label: 'Popups', keywords: 'menu, tooltip, overlay, dropdown, combobox, drop, down, select' },
{ href: '/utilities/lazy', label: 'Lazy', keywords: 'menu, tooltip, overlay, dropdown, combobox, drop, down, select' },
{ href: '/utilities/toasts', label: 'Toasts', keywords: 'overlay, snack, snackbar, bar, action, alert, notification' },
{
href: '/utilities/table-of-contents',
Expand Down
12 changes: 12 additions & 0 deletions apps/playground/src/routes/(play)/utilities/lazy/+page.server.ts
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 apps/playground/src/routes/(play)/utilities/lazy/+page.svelte
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}
8 changes: 8 additions & 0 deletions apps/playground/src/routes/api/greeting/+server.ts
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'
});
};
2 changes: 2 additions & 0 deletions docs/awesome-sveltekit.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ Add ORIGIN Env Varaible i.e., `ORIGIN=http://localhost:3000 node build/index.js`

- [https://joyofcode.xyz/](https://joyofcode.xyz/)
- [Exploring Auth.js](https://medium.com/@jibla/auth-js-exploration-1b6c27cf076f)
- [Svelte Reviewed](https://dev.to/somedood/svelte-reviewed-a-masterclass-on-empowerment-2544)
- [Conditionally stream data in SvelteKit](https://geoffrich.net/posts/conditionally-stream-data/)

## Community

Expand Down

0 comments on commit e514b5a

Please sign in to comment.