Skip to content

Commit 91d19e9

Browse files
committed
little addition
1 parent 8a71dd8 commit 91d19e9

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

content/references/concepts/ssr/async-ssr.mdx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ Solid.js offers a similar technique through the use of the `renderToStringAsync`
1818

1919
```jsx
2020
import { renderToStringAsync } from 'solid-js/web'
21+
import {createResource} from 'solid-js'
2122

2223
const App = () => {
23-
const [data, setData] = createSignal(null)
24+
const [data] = createResource(getAsyncData)
2425

25-
onMount(async () => {
26+
async function getAsyncData() {
2627
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
2728
const json = await response.json()
28-
setData(json)
29-
})
29+
30+
return json
31+
}
3032

3133
return (
3234
<div>
3335
<h1>Async SSR</h1>
34-
<p>{data() && data().title}</p>
36+
<p>{data().title}</p>
3537
</div>
3638
)
3739
}
@@ -41,7 +43,7 @@ renderToStringAsync(App).then(html => {
4143
})
4244
```
4345

44-
The above code will render the `App` component on the server and return a promise that resolves to HTML once the asynchronous data has been fetched. The HTML will look something like this:
46+
You'll notice that I didn't make use of a Suspense boundary before accessing the data in the template, that is because in Async SSR Suspense boundaries are not needed because data is prefetched on the server before rendering. The above code will render the `App` component on the server and return a promise that resolves to HTML once the asynchronous data has been fetched. The HTML will look something like this:
4547

4648
```html
4749
<div>
@@ -77,7 +79,7 @@ app.listen(3000, () => {
7779
import {createResource} from 'solid-js'
7880

7981
export default function App() {
80-
const [data, setData] = createResource(() => fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json()))
82+
const [data] = createResource(() => fetch('https://jsonplaceholder.typicode.com/todos/1').then(res => res.json()))
8183

8284
return (
8385
<div>

0 commit comments

Comments
 (0)