Skip to content

Commit

Permalink
chore(lottery): use getServerProps (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts committed Feb 27, 2020
1 parent 6b53559 commit 0afbb3c
Showing 1 changed file with 19 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/pages/lottery.tsx
Expand Up @@ -13,6 +13,25 @@ type Props = {
id: string
}

type ServerProps = {
props: Props
}

export async function unstable_getServerProps({
res
}: NextPageContext): Promise<ServerProps> {
res?.setHeader('cache-control', 'max-age=0, private')

const ids = await getFortunes().catch((): string[] => [])
const id = ids[Math.floor(Math.random() * ids.length)]

if (!id) throw new TypeError("Fortune doesn't exist.")

return {
props: { id }
}
}

const LotteryPage: NextPage<Props> = ({ id }) => {
const router = useRouter()

Expand Down Expand Up @@ -44,17 +63,4 @@ const LotteryPage: NextPage<Props> = ({ id }) => {
)
}

LotteryPage.getInitialProps = async ({
res
}: NextPageContext): Promise<Props> => {
const ids = await getFortunes().catch((): string[] => [])
const id = ids[Math.floor(Math.random() * ids.length)]

if (!id) throw new TypeError("Fortune doesn't exist.")

res?.setHeader('cache-control', 'max-age=0, private')

return { id }
}

export default LotteryPage

0 comments on commit 0afbb3c

Please sign in to comment.