Skip to content

Commit 3915059

Browse files
rafinskipgb-pmcg
andauthored
try pre-building a smaller amount of pages with static paths (#388) (#390)
* try pre-building a smaller amount of pages with static paths * Remove unused imports Co-authored-by: Rafael Ventura <[email protected]> Co-authored-by: Phil Bain <[email protected]>
1 parent 468c0d1 commit 3915059

File tree

2 files changed

+41
-9
lines changed

2 files changed

+41
-9
lines changed

pages/executive/[proposal-id].tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,11 +457,12 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {
457457

458458
export const getStaticPaths: GetStaticPaths = async () => {
459459
const proposals = await getGithubExecutives(DEFAULT_NETWORK.network);
460+
const MAX = 10;
460461

461-
const paths = proposals.map(proposal => `/executive/${proposal.key}`);
462+
const paths = proposals.slice(0, MAX).map(proposal => `/executive/${proposal.key}`);
462463

463464
return {
464465
paths,
465-
fallback: true
466+
fallback: 'blocking'
466467
};
467468
};

pages/polling/[poll-hash].tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from 'react';
2-
import { GetServerSideProps } from 'next';
2+
import { GetStaticPaths, GetStaticProps } from 'next';
33
import Link from 'next/link';
44
import ErrorPage from 'next/error';
55
import dynamic from 'next/dynamic';
@@ -23,7 +23,7 @@ import { Icon } from '@makerdao/dai-ui-icons';
2323
import { fetchJson } from 'lib/fetchJson';
2424
import { isActivePoll } from 'modules/polling/helpers/utils';
2525
import { formatDateWithTime } from 'lib/datetime';
26-
import { isDefaultNetwork, isSupportedNetwork } from 'modules/web3/helpers/networks';
26+
import { isDefaultNetwork } from 'modules/web3/helpers/networks';
2727

2828
// api
2929
import { Poll } from 'modules/polling/types';
@@ -53,6 +53,7 @@ import { useActiveWeb3React } from 'modules/web3/hooks/useActiveWeb3React';
5353
import { fetchPollBySlug } from 'modules/polling/api/fetchPollBy';
5454
import { DEFAULT_NETWORK } from 'modules/web3/constants/networks';
5555
import { ErrorBoundary } from 'modules/app/components/ErrorBoundary';
56+
import { getPolls } from 'modules/polling/api/fetchPolls';
5657

5758
const editMarkdown = content => {
5859
// hide the duplicate proposal title
@@ -393,16 +394,46 @@ export default function PollPage({ poll: prefetchedPoll }: { poll?: Poll }): JSX
393394
);
394395
}
395396

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;
400401

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+
}
402421

403422
return {
423+
revalidate: 30, // allow revalidation every 30 seconds
404424
props: {
405425
poll
406426
}
407427
};
408428
};
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

Comments
 (0)