Unverified Commit 30870b44 authored by Luis Alvarez D's avatar Luis Alvarez D Committed by GitHub

[Examples] Add SWR to api-routes and api-routes-middleware (#11385)

* Updated the api-routes-middleware example

* Updated the api-routes example
parent bf0f9da9
...@@ -8,10 +8,10 @@ ...@@ -8,10 +8,10 @@
}, },
"dependencies": { "dependencies": {
"cookie": "0.4.0", "cookie": "0.4.0",
"isomorphic-unfetch": "3.0.0",
"next": "latest", "next": "latest",
"react": "^16.8.6", "react": "^16.8.6",
"react-dom": "^16.8.6" "react-dom": "^16.8.6",
"swr": "0.1.18"
}, },
"license": "ISC" "license": "ISC"
} }
import cookies from '../../utils/cookies' import cookies from '../../utils/cookies'
const handler = (req, res) => { const handler = (req, res) => {
// The cookie middleware will add the `set-cookie` header
res.cookie('Next.js', 'api-middleware!') res.cookie('Next.js', 'api-middleware!')
res.end('Hello Next.js middleware!') // Return the `set-cookie` header so we can display it in the browser and show that it works!
res.end(res.getHeader('Set-Cookie'))
} }
export default cookies(handler) export default cookies(handler)
import fetch from 'isomorphic-unfetch' import useSWR from 'swr'
const Index = ({ cookie }) => <div>{`Cookie from response: ${cookie}`}</div> const fetcher = url => fetch(url).then(res => res.text())
export async function getServerSideProps() { export default function Index() {
const response = await fetch('http://localhost:3000/api/cookies') const { data, error } = useSWR('/api/cookies', fetcher)
const cookie = response.headers.get('set-cookie')
return { props: { cookie } } if (error) return <div>Failed to load</div>
} if (!data) return <div>Loading...</div>
export default Index return <div>{`Cookie from response: "${data}"`}</div>
}
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
}, },
"dependencies": { "dependencies": {
"next": "latest", "next": "latest",
"node-fetch": "2.6.0",
"react": "^16.8.6", "react": "^16.8.6",
"react-dom": "^16.8.6" "react-dom": "^16.8.6",
"swr": "0.1.18"
}, },
"license": "ISC" "license": "ISC"
} }
import useSWR from 'swr'
import Person from '../components/Person' import Person from '../components/Person'
import fetch from 'node-fetch'
const Index = ({ people }) => ( const fetcher = url => fetch(url).then(res => res.json())
<ul>
{people.map((p, i) => (
<Person key={i} person={p} />
))}
</ul>
)
export async function getServerSideProps() { export default function Index() {
const response = await fetch('http://localhost:3000/api/people') const { data, error } = useSWR('/api/people', fetcher)
const people = await response.json()
return { props: { people } } if (error) return <div>Failed to load</div>
} if (!data) return <div>Loading...</div>
export default Index return (
<ul>
{data.map((p, i) => (
<Person key={i} person={p} />
))}
</ul>
)
}
import fetch from 'node-fetch' import { useRouter } from 'next/router'
import useSWR from 'swr'
const Person = ({ data, status }) => const fetcher = async url => {
status === 200 ? ( const res = await fetch(url)
const data = await res.json()
if (res.status !== 200) {
throw new Error(data.message)
}
return data
}
export default function Person() {
const { query } = useRouter()
const { data, error } = useSWR(
() => query.id && `/api/people/${query.id}`,
fetcher
)
if (error) return <div>{error.message}</div>
if (!data) return <div>Loading...</div>
return (
<table> <table>
<thead> <thead>
<tr> <tr>
...@@ -26,20 +46,5 @@ const Person = ({ data, status }) => ...@@ -26,20 +46,5 @@ const Person = ({ data, status }) =>
</tr> </tr>
</tbody> </tbody>
</table> </table>
) : (
<p>{data.message}</p>
) )
export async function getServerSideProps({ params }) {
const response = await fetch(`http://localhost:3000/api/people/${params.id}`)
const data = await response.json()
return {
props: {
data,
status: response.status,
},
}
} }
export default Person
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment