Skip to content

Instantly share code, notes, and snippets.

@neongreen
Created September 27, 2022 13:42
Show Gist options
  • Save neongreen/9c888f98110842feede1bc2dc2d3d522 to your computer and use it in GitHub Desktop.
Save neongreen/9c888f98110842feede1bc2dc2d3d522 to your computer and use it in GitHub Desktop.
import type { NextPage, NextPageContext } from 'next'
import Head from 'next/head'
import React from 'react'
import Link from 'next/link'
import _ from 'lodash'
import { PreloadContext, WithPreload } from 'lib/link-preload'
import { isNextExport } from 'lib/export'
import { prefetchCards, useCards } from '@lib/queries/cards'
type Props = {
boards?: ListCardsData
}
async function preload(context: PreloadContext): Promise<void> {
await prefetchCards(context.queryClient, { onlyTopLevel: true })
}
async function getInitialProps(context: NextPageContext): Promise<SuperJSONResult> {
const props: Props = {}
// Server-side, we want to fetch the data so that we can SSR the page. Client-side, we assume the data is either
// already preloaded or will be loaded in the component itself, so we don't fetch anything.
if (typeof window === 'undefined') {
if (!isNextExport(context)) {
const session = await getSession(context)
await serverListCards(session, { onlyTopLevel: true })
.then(result => { if (result.success) props.boards = result.data })
}
}
return serialize(props)
}
const Boards: WithPreload<NextPage<SuperJSONResult>> = (serializedInitialProps) => {
const { data: session } = useSession()
const userId = session?.userId ?? null
const initialProps = deserialize<Props>(serializedInitialProps)
...
}
Boards.getInitialProps = getInitialProps
Boards.preload = preload
export default Boards
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment