-
Notifications
You must be signed in to change notification settings - Fork 27.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Examples] Add SWR to api-routes and api-routes-middleware (#11385)
* Updated the api-routes-middleware example * Updated the api-routes example
- Loading branch information
Luis Alvarez D
authored
Apr 7, 2020
1 parent
bf0f9da
commit 30870b4
Showing
6 changed files
with
52 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
import cookies from '../../utils/cookies' | ||
|
||
const handler = (req, res) => { | ||
// The cookie middleware will add the `set-cookie` header | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
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() { | ||
const response = await fetch('http://localhost:3000/api/cookies') | ||
const cookie = response.headers.get('set-cookie') | ||
export default function Index() { | ||
const { data, error } = useSWR('/api/cookies', fetcher) | ||
|
||
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
import useSWR from 'swr' | ||
import Person from '../components/Person' | ||
import fetch from 'node-fetch' | ||
|
||
const Index = ({ people }) => ( | ||
<ul> | ||
{people.map((p, i) => ( | ||
<Person key={i} person={p} /> | ||
))} | ||
</ul> | ||
) | ||
const fetcher = url => fetch(url).then(res => res.json()) | ||
|
||
export async function getServerSideProps() { | ||
const response = await fetch('http://localhost:3000/api/people') | ||
const people = await response.json() | ||
export default function Index() { | ||
const { data, error } = useSWR('/api/people', fetcher) | ||
|
||
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> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters