Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding new 'args' config #37

Closed
wants to merge 13 commits into from
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ const {
revalidate // function to trigger a validate manually
} = useSWR(
key, // a unique key for the data
fn, // Promise returning function to fetch your data
fn?, // Promise returning function to fetch your data
deps? = [], // Array of deps to be passed to `fn`
swrOptions? = {
suspense: false, // enabled React Suspense mode
revalidateOnFocus: true, // auto revalidate when window gets focused
Expand Down
6 changes: 6 additions & 0 deletions examples/fetch-with-credentials/libs/fetch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import fetch from 'isomorphic-unfetch'

export default async function (...args) {
const res = await fetch(...args)
return await res.json()
}
16 changes: 16 additions & 0 deletions examples/fetch-with-credentials/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "fetch-with-credentials",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"isomorphic-unfetch": "3.0.0",
"next": "9.1.1",
"@zeit/swr": "link:../.."
},
"scripts": {
"dev": "next",
"start": "next start",
"build": "next build"
}
}
23 changes: 23 additions & 0 deletions examples/fetch-with-credentials/pages/[user]/[repo].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import Link from 'next/link'
import fetch from '../../libs/fetch'

import useSWR from '@zeit/swr'

export default () => {
const id = typeof window !== 'undefined' ? window.location.pathname.slice(1) : ''
const { data } = useSWR('/api/data?id=' + id, fetch)

return <div style={{ textAlign: 'center' }}>
<h1>{id}</h1>
{
data ? <div>
<p>forks: {data.forks_count}</p>
<p>stars: {data.stargazers_count}</p>
<p>watchers: {data.watchers}</p>
</div> : 'loading...'
}
<br />
<br />
<Link href="/"><a>Back</a></Link>
</div>
}
23 changes: 23 additions & 0 deletions examples/fetch-with-credentials/pages/api/data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import fetch from 'isomorphic-unfetch'

const projects = [
'facebook/flipper', 'vuejs/vuepress', 'rust-lang/rust', 'zeit/next.js'
]

export default (req, res) => {
if (req.query.id) {
// a slow endpoint for getting repo data
fetch(`https://github.com/gitapi/repos/${req.query.id}`)
.then(res => res.json())
.then(data => {
setTimeout(() => {
res.json(data)
}, 2000)
})

return
}
setTimeout(() => {
res.json(projects)
}, 2000)
}
19 changes: 19 additions & 0 deletions examples/fetch-with-credentials/pages/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import Link from 'next/link'
import fetch from '../libs/fetch'

import useSWR from '@zeit/swr'

export default () => {
const { data } = useSWR('/api/data', fetch, ['/api/data', {credentials: 'include'}])

return <div style={{ textAlign: 'center' }}>
<h1>Trending Projects</h1>
<div>
{
data ? data.map(project =>
<p key={project}><Link href='/[user]/[repo]' as={`/${project}`}><a>{project}</a></Link></p>
) : 'loading...'
}
</div>
</div>
}
Loading