Skip to content

Commit d8534a1

Browse files
author
bruce
committed
useSWR in article page
1 parent 22218c4 commit d8534a1

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
"devDependencies": {
4646
"duplicate-package-checker-webpack-plugin": "^3.0.0",
4747
"express": "^4.17.1",
48-
"globby": "^11.0.3",
48+
"globby": "^11.0.4",
4949
"prettier": "^2.3.1",
5050
"webpack-ant-icon-loader": "^1.0.8"
5151
}

pages/blogs/[id].jsx

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,19 @@ import { singleBlog, listRelated } from '../../actions/blog'
1111
import { mergeStyles } from '../../helper/mergeStyles'
1212
import DisqusThread from '../../components/DisqusThread'
1313
import { singleCategory } from '../../actions/category'
14-
import { useSWR } from 'swr'
14+
import useSWR from 'swr'
1515

16-
const SingleBlog = ({ blog }) => {
16+
const SingleBlog = ({ initialBlog, id }) => {
1717
const [related, setRelated] = useState([])
1818
const [voices, setVoices] = useState([])
1919
const [selectedVoice, setSelectedVoice] = useState('')
20+
const { data: blog } = useSWR(
21+
[`${process.env.NEXT_PUBLIC_API}/blog/`, id],
22+
(url, id) => fetch(url, { id }).then((r = r.json())),
23+
{
24+
initialData: initialBlog,
25+
}
26+
)
2027

2128
const loadRelated = () => {
2229
listRelated({ blog }).then((data) => {
@@ -286,14 +293,21 @@ export async function getStaticPaths() {
286293
}
287294
}
288295

289-
export async function getStaticProps({ params }) {
290-
return singleBlog(params.id).then((data) => {
291-
if (data.error) {
292-
console.log(data.error)
293-
} else {
294-
return { props: { blog: data }, revalidate: 1 }
295-
}
296+
function initBlog(id) {
297+
return new Promise((resolve, reject) => {
298+
singleBlog(id).then((data) => {
299+
if (data.error) {
300+
reject(data.error)
301+
} else {
302+
resolve(data)
303+
}
304+
})
296305
})
297306
}
298307

308+
export async function getStaticProps({ params }) {
309+
const initialBlog = await initBlog(params.id)
310+
return { props: { initialBlog, id: params.id } }
311+
}
312+
299313
export default SingleBlog

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,10 +1207,10 @@ glob-to-regexp@^0.4.1:
12071207
resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.1.tgz#c75297087c851b9a578bd217dd59a92f59fe546e"
12081208
integrity sha512-lkX1HJXwyMcprw/5YUZc2s7DrpAiHB21/V+E1rHUrVNokkvB6bqMzT0VfV6/86ZNabt1k14YOIaT7nDvOX3Iiw==
12091209

1210-
globby@^11.0.3:
1211-
version "11.0.3"
1212-
resolved "https://registry.yarnpkg.com/globby/-/globby-11.0.3.tgz#9b1f0cb523e171dd1ad8c7b2a9fb4b644b9593cb"
1213-
integrity sha512-ffdmosjA807y7+lA1NM0jELARVmYul/715xiILEjo3hBLPTcirgQNnXECn5g3mtR8TOLCVbkfua1Hpen25/Xcg==
1210+
globby@^11.0.4:
1211+
version "11.0.4"
1212+
resolved "https://registry.yarnpkg.com/globby/-/globby-11.0.4.tgz#2cbaff77c2f2a62e71e9b2813a67b97a3a3001a5"
1213+
integrity sha512-9O4MVG9ioZJ08ffbcyVYyLOJLk5JQ688pJ4eMGLpdWLHq/Wr1D9BlriLQyL0E+jbkuePVZXYFj47QM/v093wHg==
12141214
dependencies:
12151215
array-union "^2.1.0"
12161216
dir-glob "^3.0.1"

0 commit comments

Comments
 (0)