|
| 1 | +--- |
| 2 | +title: "Streaming" |
| 3 | +--- |
| 4 | + |
| 5 | +In traditional server-rendered applications, the server must fetch all data before rendering and sending the page to the browser. |
| 6 | +If some queries are slow, this delays the initial load. |
| 7 | +**Streaming** solves this by sending the page’s HTML shell immediately and progressively streaming data-dependent sections as they become ready. |
| 8 | + |
| 9 | +When a query is accessed during a server-side render, Solid suspends the UI until the data resolves. |
| 10 | +By default, this suspension affects the entire page. |
| 11 | + |
| 12 | +To control this behavior, you can use suspense boundaries - regions of the component tree defined by a [`<Suspense>` component](/reference/components/suspense). |
| 13 | +It isolates asynchronous behavior to a specific section of the page. |
| 14 | + |
| 15 | +Content inside the boundary is managed by Solid’s concurrency system: if it isn’t ready, the boundary’s fallback UI is shown while the rest of the page renders and streams immediately. |
| 16 | +Once the data resolves, the server streams the final HTML for that section, replacing the fallback and letting users see and interact with most of the page much sooner. |
| 17 | + |
| 18 | +```tsx |
| 19 | +import { Suspense, For } from "solid-js"; |
| 20 | +import { query, createAsync } from "@solidjs/router"; |
| 21 | + |
| 22 | +const getAccountStatsQuery = query(async () => { |
| 23 | + // ... Fetches account statistics. |
| 24 | +}, "accountStats"); |
| 25 | + |
| 26 | +const getRecentTransactionsQuery = query(async () => { |
| 27 | + // ... Fetches a list of recent transactions. |
| 28 | +}, "recentTransactions"); |
| 29 | + |
| 30 | +function Dashboard() { |
| 31 | + const stats = createAsync(() => getAccountStatsQuery()); |
| 32 | + const transactions = createAsync(() => getRecentTransactionsQuery()); |
| 33 | + |
| 34 | + return ( |
| 35 | + <div> |
| 36 | + <h1>Dashboard</h1> |
| 37 | + <Suspense fallback={<p>Loading account stats...</p>}> |
| 38 | + <For each={stats()}> |
| 39 | + {(stat) => ( |
| 40 | + <p> |
| 41 | + {stat.label}: {stat.value} |
| 42 | + </p> |
| 43 | + )} |
| 44 | + </For> |
| 45 | + </Suspense> |
| 46 | + |
| 47 | + <Suspense fallback={<p>Loading recent transactions...</p>}> |
| 48 | + <For each={transactions()}> |
| 49 | + {(transaction) => ( |
| 50 | + <h2> |
| 51 | + {transaction.description} - {transaction.amount} |
| 52 | + </h2> |
| 53 | + )} |
| 54 | + </For> |
| 55 | + </Suspense> |
| 56 | + </div> |
| 57 | + ); |
| 58 | +} |
| 59 | +``` |
| 60 | + |
| 61 | +For example, each `<Suspense>` component creates its own independent boundary. |
| 62 | +The server can stream the heading `<h1>Dashboard</h1>` immediately, while the `stats` and `transactions` are handled separately. |
| 63 | +If the `transactions` query is slow, only its boundary will display a fallback, while `stats` will render as soon as its data is ready. |
| 64 | + |
| 65 | +## When to disable streaming |
| 66 | + |
| 67 | +While streaming is powerful, there are cases where it is better to wait for the data to load on the server. |
| 68 | +In these situations, you can use the `deferStream` option in `createAsync`. |
| 69 | + |
| 70 | +When `deferStream` is set to `true`, the server waits for the query to resolve before sending the initial HTML. |
| 71 | + |
| 72 | +A common reason to disable streaming is for Search Engine Optimization (SEO). |
| 73 | +Some search engine crawlers may not wait for streamed content to load. |
| 74 | +If critical data, such as a page title or meta description, affects SEO, it should be included in the initial server response. |
| 75 | + |
| 76 | +```tsx |
| 77 | +import { query, createAsync } from "@solidjs/router"; |
| 78 | + |
| 79 | +const getArticleQuery = query(async () => { |
| 80 | + // ... Fetches an article. |
| 81 | +}, "article"); |
| 82 | + |
| 83 | +function ArticleHeader() { |
| 84 | + const article = createAsync(() => getArticleQuery(), { |
| 85 | + deferStream: true, |
| 86 | + }); |
| 87 | + |
| 88 | + return <h1>{article()?.title}</h1>; |
| 89 | +} |
| 90 | +``` |
0 commit comments