Skip to content

Commit 335f89d

Browse files
sergiodxashuding
authored andcommitted
Add Server Side Render Example (#132)
* Add Server Side Render Example * Change SSR example to use pokeapi to get the data
1 parent 813da18 commit 335f89d

File tree

5 files changed

+136
-0
lines changed

5 files changed

+136
-0
lines changed

examples/server-render/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Server Render
2+
3+
## One-Click Deploy
4+
5+
Deploy your own SWR project with ZEIT Now.
6+
7+
[![Deploy with ZEIT Now](https://zeit.co/button)](https://zeit.co/new/project?template=https://github.com/zeit/swr/tree/master/examples/server-render)
8+
9+
## How to Use
10+
11+
Download the example:
12+
13+
```bash
14+
curl https://codeload.github.com/zeit/swr/tar.gz/master | tar -xz --strip=2 swr-master/examples/server-render
15+
cd server-render
16+
```
17+
18+
Install it and run:
19+
20+
```bash
21+
yarn
22+
yarn dev
23+
# or
24+
npm install
25+
npm run dev
26+
```
27+
28+
Deploy it to the cloud with [now](https://zeit.co/home) ([download](https://zeit.co/download))
29+
30+
```
31+
now
32+
```
33+
34+
## The Idea behind the Example
35+
36+
This examples show how to combine Next.js getInitialProps with the SWR `initialData` option to support Server-Side Rendering.
37+
38+
The application will fetch the data server-side and then receive it as props, that data will be passed as `initialData` to SWR, once the application starts client-side SWR will revalidate it against the API and update the DOM, if it's required, with the new data.
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import fetch from 'isomorphic-unfetch'
2+
3+
export default async function (...args) {
4+
const res = await fetch(...args)
5+
return await res.json()
6+
}

examples/server-render/package.json

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "server-render",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"dependencies": {
7+
"isomorphic-unfetch": "3.0.0",
8+
"next": "9.1.1",
9+
"react": "16.11.0",
10+
"react-dom": "16.11.0",
11+
"swr": "latest"
12+
},
13+
"scripts": {
14+
"dev": "next",
15+
"start": "next start",
16+
"build": "next build"
17+
}
18+
}
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Link from 'next/link'
2+
import fetcher from '../libs/fetcher'
3+
4+
import useSWR from 'swr'
5+
6+
const getURL = pokemon => `https://pokeapi.co/api/v2/pokemon/${pokemon}`
7+
8+
export default function Pokemon({ pokemon, initialData }) {
9+
const { data } = useSWR(getURL(pokemon), fetcher, { initialData })
10+
11+
return (
12+
<div>
13+
<h1>{pokemon}</h1>
14+
{data ? (
15+
<div>
16+
<figure>
17+
<img src={data.sprites.front_default} />
18+
</figure>
19+
<p>height: {data.height}</p>
20+
<p>weight: {data.weight}</p>
21+
<ul>
22+
{data.types.map(({ type }) => (
23+
<li key={type.name}>{type.name}</li>
24+
))}
25+
</ul>
26+
</div>
27+
) : (
28+
'loading...'
29+
)}
30+
<br />
31+
<br />
32+
<Link href="/">
33+
<a>Back</a>
34+
</Link>
35+
</div>
36+
)
37+
}
38+
39+
Pokemon.getInitialProps = async ({ query }) => {
40+
const data = await fetcher(getURL(query.pokemon))
41+
return { initialData: data, pokemon: query.pokemon }
42+
}

examples/server-render/pages/index.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import Link from 'next/link'
2+
import fetcher from '../libs/fetcher'
3+
4+
import useSWR from 'swr'
5+
6+
const URL = 'https://pokeapi.co/api/v2/pokemon/'
7+
8+
export default function Home({ initialData }) {
9+
const { data } = useSWR(URL, fetcher, { initialData })
10+
11+
return (
12+
<div style={{ textAlign: 'center' }}>
13+
<h1>Trending Projects</h1>
14+
<div>
15+
{data && data.results
16+
? data.results.map(pokemon => (
17+
<p key={pokemon.name}>
18+
<Link href="/[pokemon]" as={`/${pokemon.name}`}>
19+
<a>{pokemon.name}</a>
20+
</Link>
21+
</p>
22+
))
23+
: 'loading...'}
24+
</div>
25+
</div>
26+
)
27+
}
28+
29+
Home.getInitialProps = async () => {
30+
const data = await fetcher(URL)
31+
return { initialData: data }
32+
}

0 commit comments

Comments
 (0)