Next.js

Create a project

npx create-next-app@latest

Data fetching

Static vs. dynamic rendering

App Router

Routing and layouts

import Link from 'next/link'
 
export default function Page() {
    return <Link href="/dashboard">Dashboard</Link>
}

Route Handlers

export async function GET(request: Request) {
    /* ... */
}

Pages Router

Warning

These hooks cannot be used in the App component (_app.js).

getStaticProps

export async function getStaticProps(context) {
  return {
    props: {}, // will be passed to the page component as props
  }
}

getServerSideProps

// context includes things like req, query, params
export const getServerSideProps = async (context: GetServerSidePropsContext) => {
	return {
		props: {
			name: context.query.name
		},
	}
}

export default function Page({ name }: Props) {
    return (
        <Text>Hello {name}!</Text>
    )
}

Get type for server-side props

type Props = InferGetServerSidePropsType<typeof getServerSideProps>

getInitialProps

useEffect runs in the browser only

Disable build failure on TypeScript errors

typescript: {
    ignoreBuildErrors: true
}

See also