Skip to content

Commit ece6a14

Browse files
committed
feat: add documentation
1 parent 7d6c937 commit ece6a14

File tree

1 file changed

+69
-0
lines changed
  • packages/docs/src/routes/docs/(qwikrouter)/route-loader

1 file changed

+69
-0
lines changed

packages/docs/src/routes/docs/(qwikrouter)/route-loader/index.mdx

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ contributors:
1414
- mjschwanitz
1515
- adamdbradley
1616
- gioboa
17+
- Varixo
1718
updated_at: '2023-12-15T11:00:00Z'
1819
created_at: '2023-03-20T23:45:13Z'
1920
---
@@ -124,6 +125,74 @@ export default component$(() => {
124125

125126
The above example shows two `routeLoader$`s being used in the same file. A generic `useLoginStatus` loader is used to check if the user is logged in, and a more specific `useCurrentUser` loader is used to retrieve the user data.
126127

128+
## Loaders data serialization
129+
130+
By default, route loader data is not serialized and sent to the client. Instead, it is discarded after server-side rendering (SSR). If a component on the client requires the data, it will be refetched (lazy-loaded).
131+
132+
You can customize this behavior using the `serializationStrategy` option in the `routeLoader$()`. This option accepts one of three values:
133+
- `never` (Default): The data is never serialized. It is discarded after SSR and refetched on the client when needed.
134+
- `always`: The data is always serialized and included in the initial HTML response. It is immediately available on the client without needing to be refetched.
135+
- `auto`: Qwik automatically decides whether to serialize the data based on its size. If the data is small, it will be serialized. If it exceeds a certain threshold, it will be discarded and lazy-loaded on the client as needed.
136+
137+
```tsx title="src/routes/product/[user]/index.tsx"
138+
import { routeLoader$ } from '@qwik.dev/router';
139+
140+
export const useProductDetails = routeLoader$(async (requestEvent) => {
141+
const res = await fetch(
142+
`https://.../products/${requestEvent.params.productId}`
143+
);
144+
const product = await res.json();
145+
return product;
146+
}, {
147+
serializationStrategy: 'always', // 'never' | 'always' | 'auto'
148+
});
149+
```
150+
151+
### Handling loading state for lazy loaded route loader data
152+
153+
When using lazy-loaded data, you can handle the loading state in your Qwik Components by checking if the data is available. If not, you can render a loading indicator or placeholder content.
154+
155+
```tsx title="src/routes/product/[user]/index.tsx"
156+
import { component$, useSignal } from '@qwik.dev/core';
157+
import { routeLoader$ } from '@qwik.dev/router';
158+
159+
export const useProductDetails = routeLoader$(async (requestEvent) => {
160+
const res = await fetch(
161+
`https://.../products/${requestEvent.params.productId}`
162+
);
163+
const product = await res.json();
164+
return product;
165+
});
166+
167+
export default component$(() => {
168+
const data = useProductDetails();
169+
const condition = useSignal(false);
170+
return (
171+
<>
172+
Product name: {data.value.product.name}
173+
<button onClick$={() => (condition.value = !condition.value)}>
174+
Toggle
175+
</button>
176+
{condition.value ? <Child /> : <div />}
177+
</>
178+
);
179+
});
180+
181+
// Will use lazy loaded data
182+
export const Child = component$(() => {
183+
const data = useProductDetails();
184+
return (
185+
<div>
186+
{data.loading ? (
187+
<p>Loading product details...</p>
188+
) : (
189+
<p>Product description: {data.value.product.description}</p>
190+
)}
191+
</div>
192+
);
193+
});
194+
```
195+
127196
## RequestEvent
128197

129198
Just like [middleware](/docs/middleware/) or [endpoint](/docs/endpoints/) `onRequest` and `onGet`, `routeLoader$`s have access to the [`RequestEvent`](/docs/middleware#requestevent) API which includes information about the current HTTP request.

0 commit comments

Comments
 (0)