|
| 1 | +import { GetStaticPaths, GetStaticProps } from 'next' |
1 | 2 | // import { GetServerSideProps } from 'next' |
| 3 | +import { useRouter } from 'next/router' |
2 | 4 | import { Provider } from 'mobx-react' |
3 | 5 |
|
4 | | -import { ROUTE, THREAD, METRIC } from '@/constant' |
5 | | - |
6 | | -import { |
7 | | - ssrFetchPrepare, |
8 | | - ssrError, |
9 | | - articleSEO, |
10 | | - ssrBaseStates, |
11 | | - ssrGetParam, |
12 | | - refreshIfneed, |
13 | | -} from '@/utils' |
| 6 | +import { ARTICLE_THREAD, METRIC } from '@/constant' |
| 7 | +import { articleSEO, makeGQClient } from '@/utils' |
14 | 8 |
|
15 | 9 | import { useStore } from '@/stores/init' |
16 | 10 |
|
17 | 11 | import GlobalLayout from '@/containers/layout/GlobalLayout' |
18 | 12 | import ArticleDigest from '@/containers/digest/ArticleDigest' |
19 | 13 | import ArticleContent from '@/containers/content/ArticleContent' |
| 14 | +import LavaLampLoading from '@/widgets/Loading/LavaLampLoading' |
20 | 15 |
|
21 | 16 | import { P } from '@/schemas' |
22 | 17 |
|
23 | | -const loader = async (context, opt = {}) => { |
24 | | - const id = ssrGetParam(context, 'id') |
25 | | - const { gqClient, userHasLogin } = ssrFetchPrepare(context, opt) |
26 | | - |
27 | | - // query data |
28 | | - const sessionState = gqClient.request(P.sessionState) |
29 | | - const works = gqClient.request(P.works, { id, userHasLogin }) |
| 18 | +const loader = async (params) => { |
| 19 | + const gqClient = makeGQClient('') |
| 20 | + const { id } = params |
30 | 21 |
|
31 | | - const subscribedCommunities = gqClient.request(P.subscribedCommunities, { |
32 | | - filter: { |
33 | | - page: 1, |
34 | | - size: 30, |
35 | | - }, |
36 | | - }) |
| 22 | + const works = gqClient.request(P.works, { id, userHasLogin: false }) |
37 | 23 |
|
38 | 24 | return { |
39 | | - ...(await sessionState), |
40 | 25 | ...(await works), |
41 | | - ...(await subscribedCommunities), |
42 | 26 | } |
43 | 27 | } |
44 | 28 |
|
45 | | -export const getServerSideProps = async (context) => { |
46 | | - let resp |
47 | | - try { |
48 | | - resp = await loader(context) |
49 | | - const { works, sessionState } = resp |
50 | | - refreshIfneed(sessionState, `/works/${works.id}`, context) |
51 | | - } catch (e) { |
52 | | - console.log('#### error from server: ', e) |
53 | | - return ssrError(context, 'fetch', 500) |
54 | | - } |
| 29 | +export const getStaticPaths: GetStaticPaths = async () => { |
| 30 | + return { paths: [], fallback: true } |
| 31 | +} |
55 | 32 |
|
56 | | - const { works } = resp |
| 33 | +export const getStaticProps: GetStaticProps = async (ctx) => { |
| 34 | + console.log('ctx: ', ctx) |
| 35 | + const { params } = ctx |
57 | 36 |
|
58 | | - const initProps = { |
59 | | - ...ssrBaseStates(resp), |
60 | | - route: { mainPath: ROUTE.WORKS, subPath: works.id }, |
61 | | - viewing: { |
62 | | - works, |
63 | | - activeThread: THREAD.WORKS, |
| 37 | + const resp = await loader(params) |
| 38 | + // console.log('resp: ', resp) |
| 39 | + |
| 40 | + return { |
| 41 | + props: { |
| 42 | + viewing: { |
| 43 | + works: resp.works, |
| 44 | + activeThread: ARTICLE_THREAD.WORKS, |
| 45 | + }, |
64 | 46 | }, |
| 47 | + revalidate: 5, |
65 | 48 | } |
66 | | - |
67 | | - return { props: { errorCode: null, ...initProps } } |
68 | 49 | } |
69 | 50 |
|
70 | 51 | const WorksArticlePage = (props) => { |
71 | 52 | const store = useStore(props) |
| 53 | + |
| 54 | + const { isFallback } = useRouter() |
| 55 | + if (isFallback) return <LavaLampLoading top={20} left={30} /> |
| 56 | + |
72 | 57 | const { viewing } = props |
73 | 58 | const { works } = viewing |
74 | 59 |
|
75 | | - const seoConfig = articleSEO(THREAD.WORKS, works) |
| 60 | + const seoConfig = articleSEO(ARTICLE_THREAD.WORKS, works) |
76 | 61 |
|
77 | 62 | return ( |
78 | 63 | <Provider store={store}> |
|
0 commit comments