@@ -11,12 +11,19 @@ import { singleBlog, listRelated } from '../../actions/blog'
1111import { mergeStyles } from '../../helper/mergeStyles'
1212import DisqusThread from '../../components/DisqusThread'
1313import { 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+
299313export default SingleBlog
0 commit comments