|
1 | 1 | import { ReactElement, useMemo } from "react"; |
2 | 2 |
|
3 | | -import type { GetServerSideProps } from "next"; |
| 3 | +import type { GetStaticPaths, GetStaticProps } from "next"; |
4 | 4 | import { useRouter } from "next/router"; |
5 | 5 |
|
6 | 6 | import { INDEXER_API_ENDPOINT_URL } from "@/common/_config"; |
@@ -51,12 +51,34 @@ CampaignPage.getLayout = function getLayout(page: ReactElement) { |
51 | 51 | return <CampaignLayout>{page}</CampaignLayout>; |
52 | 52 | }; |
53 | 53 |
|
54 | | -export const getServerSideProps: GetServerSideProps<CampaignPageProps> = async (context) => { |
55 | | - const { campaignId } = context.params as { campaignId?: string }; |
56 | | - const { res } = context; |
| 54 | +export const getStaticPaths: GetStaticPaths = async () => { |
| 55 | + try { |
| 56 | + const response = await fetch(`https://dev.potlock.io/api/v1/campaigns?page_size=200`, { |
| 57 | + headers: { "content-type": "application/json" }, |
| 58 | + }); |
| 59 | + |
| 60 | + if (!response.ok) { |
| 61 | + return { paths: [], fallback: "blocking" }; |
| 62 | + } |
| 63 | + |
| 64 | + const payload = (await response.json()) as { |
| 65 | + results?: { on_chain_id: number | string }[]; |
| 66 | + data?: { on_chain_id: number | string }[]; |
| 67 | + }; |
57 | 68 |
|
58 | | - // Cache SSR response at the edge to avoid repeated slow requests |
59 | | - res.setHeader("Cache-Control", "s-maxage=300, stale-while-revalidate=900"); |
| 69 | + const campaigns = payload.results ?? payload.data ?? []; |
| 70 | + |
| 71 | + return { |
| 72 | + paths: campaigns.map((c) => ({ params: { campaignId: String(c.on_chain_id) } })), |
| 73 | + fallback: "blocking", |
| 74 | + }; |
| 75 | + } catch { |
| 76 | + return { paths: [], fallback: "blocking" }; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +export const getStaticProps: GetStaticProps<CampaignPageProps> = async (context) => { |
| 81 | + const { campaignId } = context.params as { campaignId?: string }; |
60 | 82 | const parsedCampaignId = campaignId ? parseInt(campaignId) : undefined; |
61 | 83 |
|
62 | 84 | const fallbackSeo: SeoProps = { |
@@ -98,9 +120,9 @@ export const getServerSideProps: GetServerSideProps<CampaignPageProps> = async ( |
98 | 120 | image: campaign?.cover_image_url ?? fallbackSeo.image, |
99 | 121 | }; |
100 | 122 |
|
101 | | - return { props: { seo } }; |
| 123 | + return { props: { seo }, revalidate: 300 }; |
102 | 124 | } catch { |
103 | | - return { props: { seo: fallbackSeo } }; |
| 125 | + return { props: { seo: fallbackSeo }, revalidate: 60 }; |
104 | 126 | } finally { |
105 | 127 | clearTimeout(timeoutId); |
106 | 128 | } |
|
0 commit comments