@@ -30,52 +30,99 @@ config:
30
30
// sdk.ts
31
31
import { GraphQLClient } from 'graphql-request'
32
32
import { getSdkWithHooks } from './graphql'
33
+ import { getJwt } from './jwt'
33
34
34
- const sdk = getSdkWithHooks(
35
- new GraphQLClient(`${process.env.API_URL}/graphql`, {
36
- cache : typeof window === 'object' ? 'default' : 'no-cache',
37
- })
38
- )
35
+ const sdk = () => {
36
+ const headers : Record<string, string> = { 'Content-Type': 'application/json' }
37
+ const jwt = getJwt()
38
+ if (jwt) {
39
+ headers.Authorization = `Bearer ${jwt}`
40
+ }
41
+ return getSdkWithHooks(
42
+ new GraphQLClient(`${process.env.NEXT_PUBLIC_API_URL}`, {
43
+ headers,
44
+ })
45
+ )
46
+ }
39
47
40
48
export default sdk
41
49
```
42
50
43
51
``` tsx
44
- // Article.tsx
52
+ // pages/posts/[slug].tsx
53
+ import { GetStaticProps , GetStaticPropsResult } from ' next'
54
+ import ErrorPage from ' next/error'
55
+ import { useRouter } from ' next/router'
56
+ import Article from ' ../components/Article'
45
57
import sdk from ' ../sdk'
46
58
47
- type StaticPropsParams = { slug: string }
48
- export const getStaticProps: GetStaticProps <StaticProps , StaticPropsParams > = async ({
59
+ type StaticParams = { slug: string }
60
+ type StaticProps = StaticParams & {
61
+ initialData: {
62
+ articleBySlug: NonNullable <GetArticleQuery [' articleBySlug' ]>
63
+ }
64
+ }
65
+ type ArticlePageProps = StaticProps & { preview? : boolean }
66
+
67
+ export const getStaticProps: GetStaticProps <StaticProps , StaticParams > = async ({
49
68
params ,
50
69
preview = false ,
70
+ previewData
51
71
}) => {
52
72
if (! params ) {
53
73
throw new Error (' Parameter is invalid!' )
54
74
}
55
75
56
- const { articleBySlug : article } = await sdk .GetArticle ({
76
+ const { articleBySlug : article } = await sdk () .GetArticle ({
57
77
slug: params .slug ,
58
78
})
59
79
60
80
if (! article ) {
61
81
throw new Error (' Article is not found!' )
62
82
}
63
83
64
- return {
65
- props: {
66
- slug: params .slug ,
67
- preview ,
68
- initialData: {
69
- articleBySlug: article
70
- }
84
+ const props: ArticlePageProps = {
85
+ slug: params .slug ,
86
+ preview ,
87
+ initialData: {
88
+ articleBySlug: article
71
89
}
72
90
}
91
+
92
+ return {
93
+ props: preview
94
+ ? {
95
+ ... props ,
96
+ ... previewData ,
97
+ }
98
+ : props ,
99
+ }
73
100
})
74
101
75
- export const Article = ({ slug , initialData , preview }: ArticleProps ): JSX .Element => {
76
- const { data : { article } } = sdk .useGetArticle (
77
- ' UniqueKeyForTheRequest' , { slug }, { initialData }
102
+ export const ArticlePage = ({ slug , initialData , preview }: ArticlePageProps ): JSX .Element => {
103
+ const router = useRouter ()
104
+ const { data : { article }, mutate : mutateArticle } = sdk ().useGetArticle (
105
+ ` UniqueKeyForTheRequest/${slug } ` , { slug }, { initialData }
106
+ )
107
+
108
+ if (! router .isFallback && ! article ) {
109
+ return <ErrorPage statusCode = { 404 } />
110
+ }
111
+
112
+ // because of typescript problem
113
+ if (! article ) {
114
+ throw new Error (' Article is not found!' )
115
+ }
116
+
117
+ return (
118
+ <Layout preview = { preview } >
119
+ <>
120
+ <Head >
121
+ <title >{ article .title } </title >
122
+ </Head >
123
+ <Article article = { article } />
124
+ </>
125
+ </Layout >
78
126
)
79
- // ...
80
127
}
81
128
```
0 commit comments