forked from QwikDev/qwik
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add "Streaming/deferred loaders" cookbook (QwikDev#5879)
- Loading branch information
Showing
4 changed files
with
78 additions
and
0 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
packages/docs/src/routes/demo/cookbook/streaming-deferred-loaders/index.tsx
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,27 @@ | ||
import { Resource, component$ } from '@builder.io/qwik'; | ||
import { routeLoader$ } from '@builder.io/qwik-city'; | ||
|
||
export const useMyData = routeLoader$(() => { | ||
return async () => { | ||
await delay(4_000); | ||
return 'MyData ' + Math.random(); | ||
}; | ||
}); | ||
|
||
const delay = (timeout: number) => { | ||
return new Promise((res) => setTimeout(res, timeout)); | ||
}; | ||
|
||
export default component$(() => { | ||
const myData = useMyData(); | ||
return ( | ||
<> | ||
<div>BEFORE</div> | ||
<Resource | ||
value={myData} | ||
onResolved={(data) => <div>DATA: {data}</div>} | ||
/> | ||
<div>AFTER</div> | ||
</> | ||
); | ||
}); |
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
49 changes: 49 additions & 0 deletions
49
packages/docs/src/routes/docs/cookbook/streaming-deferred-loaders/index.mdx
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,49 @@ | ||
--- | ||
title: Cookbook | Streaming/deferred loaders | ||
contributors: | ||
- gioboa | ||
updated_at: '2024-02-23T01:00:00Z' | ||
created_at: '2024-02-23T01:00:00Z' | ||
--- | ||
|
||
import CodeSandbox, { CodeFile } from '../../../../components/code-sandbox/index.tsx'; | ||
|
||
# Streaming/deferred loaders | ||
|
||
When you use `routeLoader$` the default behavior is to wait for it to complete before rendering our component(s). | ||
However, there is a way to render the DOM up to the point where `routeLoader$` is used and wait for it to complete. | ||
By returning an asynchronous function from our `routeLoader$` we can stream/defer the rendering to provide immediate visual feedback. | ||
|
||
<CodeFile src="/src/routes/demo/cookbook/streaming-deferred-loaders/index.tsx" > | ||
```tsx | ||
import { Resource, component$ } from '@builder.io/qwik'; | ||
import { routeLoader$ } from '@builder.io/qwik-city'; | ||
|
||
export const useMyData = routeLoader$(() => { | ||
return async () => { | ||
await delay(4_000); | ||
return 'MyData ' + Math.random(); | ||
}; | ||
}); | ||
|
||
const delay = (timeout: number) => { | ||
return new Promise((res) => setTimeout(res, timeout)); | ||
}; | ||
|
||
export default component$(() => { | ||
const myData = useMyData(); | ||
return ( | ||
<> | ||
<div>BEFORE</div> | ||
<Resource | ||
value={myData} | ||
onResolved={(data) => <div>DATA: {data}</div>} | ||
/> | ||
<div>AFTER</div> | ||
</> | ||
); | ||
}); | ||
``` | ||
</CodeFile> | ||
|
||
<CodeSandbox src="/src/routes/demo/cookbook/streaming-deferred-loaders/" /> |
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