1
1
import { useEffect , useState } from 'react' ;
2
- import { GetServerSideProps } from 'next' ;
2
+ import { GetStaticPaths , GetStaticProps } from 'next' ;
3
3
import Link from 'next/link' ;
4
4
import ErrorPage from 'next/error' ;
5
5
import dynamic from 'next/dynamic' ;
@@ -23,7 +23,7 @@ import { Icon } from '@makerdao/dai-ui-icons';
23
23
import { fetchJson } from 'lib/fetchJson' ;
24
24
import { isActivePoll } from 'modules/polling/helpers/utils' ;
25
25
import { formatDateWithTime } from 'lib/datetime' ;
26
- import { isDefaultNetwork , isSupportedNetwork } from 'modules/web3/helpers/networks' ;
26
+ import { isDefaultNetwork } from 'modules/web3/helpers/networks' ;
27
27
28
28
// api
29
29
import { Poll } from 'modules/polling/types' ;
@@ -53,6 +53,7 @@ import { useActiveWeb3React } from 'modules/web3/hooks/useActiveWeb3React';
53
53
import { fetchPollBySlug } from 'modules/polling/api/fetchPollBy' ;
54
54
import { DEFAULT_NETWORK } from 'modules/web3/constants/networks' ;
55
55
import { ErrorBoundary } from 'modules/app/components/ErrorBoundary' ;
56
+ import { getPolls } from 'modules/polling/api/fetchPolls' ;
56
57
57
58
const editMarkdown = content => {
58
59
// hide the duplicate proposal title
@@ -393,16 +394,46 @@ export default function PollPage({ poll: prefetchedPoll }: { poll?: Poll }): JSX
393
394
) ;
394
395
}
395
396
396
- export const getServerSideProps : GetServerSideProps = async ( context ) : Promise < any > => {
397
- const slug = context . query [ 'poll-hash' ] as string ;
398
- const network = context . query [ 'network' ] as string ;
399
- const networkToFetch = network && isSupportedNetwork ( network ) ? network : DEFAULT_NETWORK . network ;
397
+ // export const getServerSideProps: GetServerSideProps = async (context): Promise<any> => {
398
+ // const slug = context.query['poll-hash'] as string;
399
+ // const network = context.query['network'] as string;
400
+ // const networkToFetch = network && isSupportedNetwork(network) ? network : DEFAULT_NETWORK.network;
400
401
401
- const poll = await fetchPollBySlug ( slug , networkToFetch ) ;
402
+ // const poll = await fetchPollBySlug(slug, networkToFetch);
403
+
404
+ // return {
405
+ // props: {
406
+ // poll
407
+ // }
408
+ // };
409
+ // };
410
+
411
+ export const getStaticProps : GetStaticProps = async ( { params } ) => {
412
+ // fetch poll contents at build-time if on the default network
413
+ const pollSlug = params ?. [ 'poll-hash' ] as string ;
414
+ // invariant(pollSlug, 'getStaticProps poll hash not found in params');
415
+
416
+ const poll = await fetchPollBySlug ( pollSlug , DEFAULT_NETWORK . network ) ;
417
+
418
+ if ( ! poll ) {
419
+ return { revalidate : 30 , props : { poll : null } } ;
420
+ }
402
421
403
422
return {
423
+ revalidate : 30 , // allow revalidation every 30 seconds
404
424
props : {
405
425
poll
406
426
}
407
427
} ;
408
428
} ;
429
+
430
+ export const getStaticPaths : GetStaticPaths = async ( ) => {
431
+ const pollsResponse = await getPolls ( ) ;
432
+ const MAX = 10 ;
433
+ const paths = pollsResponse . polls . slice ( 0 , MAX ) . map ( p => `/polling/${ p . slug } ` ) ;
434
+
435
+ return {
436
+ paths,
437
+ fallback : 'blocking'
438
+ } ;
439
+ } ;
0 commit comments