···11+import * as React from "react";
22+import ListItem from "./ListItem";
33+import { User } from "../interfaces";
44+55+type Props = {
66+ items: User[];
77+};
88+99+const List = ({ items }: Props) => (
1010+ <ul>
1111+ {items.map((item) => (
1212+ <li key={item.id}>
1313+ <ListItem data={item} />
1414+ </li>
1515+ ))}
1616+ </ul>
1717+);
1818+1919+export default List;
+16
components/ListDetail.tsx
···11+import * as React from "react";
22+33+import { User } from "../interfaces";
44+55+type ListDetailProps = {
66+ item: User;
77+};
88+99+const ListDetail = ({ item: user }: ListDetailProps) => (
1010+ <div>
1111+ <h1>Detail for {user.name}</h1>
1212+ <p>ID: {user.id}</p>
1313+ </div>
1414+);
1515+1616+export default ListDetail;
+16
components/ListItem.tsx
···11+import React from "react";
22+import Link from "next/link";
33+44+import { User } from "../interfaces";
55+66+type Props = {
77+ data: User;
88+};
99+1010+const ListItem = ({ data }: Props) => (
1111+ <Link href="/users/[id]" as={`/users/${data.id}`}>
1212+ {data.id}:{data.name}
1313+ </Link>
1414+);
1515+1616+export default ListItem;
+10
interfaces/index.ts
···11+// You can include shared interfaces/types in a separate file
22+// and then use them in any component by importing them. For
33+// example, to import the interface below do:
44+//
55+// import { User } from 'path/to/interfaces';
66+77+export type User = {
88+ id: number;
99+ name: string;
1010+};
+14
pages/about.tsx
···11+import Link from "next/link";
22+import Layout from "../components/Layout";
33+44+const AboutPage = () => (
55+ <Layout title="About | Next.js + TypeScript Example">
66+ <h1>About</h1>
77+ <p>This is the about page</p>
88+ <p>
99+ <Link href="/">Go home</Link>
1010+ </p>
1111+ </Layout>
1212+);
1313+1414+export default AboutPage;
···11+import { GetStaticProps, GetStaticPaths } from "next";
22+33+import { User } from "../../interfaces";
44+import { sampleUserData } from "../../utils/sample-data";
55+import Layout from "../../components/Layout";
66+import ListDetail from "../../components/ListDetail";
77+88+type Props = {
99+ item?: User;
1010+ errors?: string;
1111+};
1212+1313+const StaticPropsDetail = ({ item, errors }: Props) => {
1414+ if (errors) {
1515+ return (
1616+ <Layout title="Error | Next.js + TypeScript Example">
1717+ <p>
1818+ <span style={{ color: "red" }}>Error:</span> {errors}
1919+ </p>
2020+ </Layout>
2121+ );
2222+ }
2323+2424+ return (
2525+ <Layout
2626+ title={`${
2727+ item ? item.name : "User Detail"
2828+ } | Next.js + TypeScript Example`}
2929+ >
3030+ {item && <ListDetail item={item} />}
3131+ </Layout>
3232+ );
3333+};
3434+3535+export default StaticPropsDetail;
3636+3737+export const getStaticPaths: GetStaticPaths = async () => {
3838+ // Get the paths we want to pre-render based on users
3939+ const paths = sampleUserData.map((user) => ({
4040+ params: { id: user.id.toString() },
4141+ }));
4242+4343+ // We'll pre-render only these paths at build time.
4444+ // { fallback: false } means other routes should 404.
4545+ return { paths, fallback: false };
4646+};
4747+4848+// This function gets called at build time on server-side.
4949+// It won't be called on client-side, so you can even do
5050+// direct database queries.
5151+export const getStaticProps: GetStaticProps = async ({ params }) => {
5252+ try {
5353+ const id = params?.id;
5454+ const item = sampleUserData.find((data) => data.id === Number(id));
5555+ // By returning { props: item }, the StaticPropsDetail component
5656+ // will receive `item` as a prop at build time
5757+ return { props: { item } };
5858+ } catch (err: any) {
5959+ return { props: { errors: err.message } };
6060+ }
6161+};
+35
pages/users/index.tsx
···11+import { GetStaticProps } from "next";
22+import Link from "next/link";
33+44+import { User } from "../../interfaces";
55+import { sampleUserData } from "../../utils/sample-data";
66+import Layout from "../../components/Layout";
77+import List from "../../components/List";
88+99+type Props = {
1010+ items: User[];
1111+};
1212+1313+const WithStaticProps = ({ items }: Props) => (
1414+ <Layout title="Users List | Next.js + TypeScript Example">
1515+ <h1>Users List</h1>
1616+ <p>
1717+ Example fetching data from inside <code>getStaticProps()</code>.
1818+ </p>
1919+ <p>You are currently on: /users</p>
2020+ <List items={items} />
2121+ <p>
2222+ <Link href="/">Go home</Link>
2323+ </p>
2424+ </Layout>
2525+);
2626+2727+export const getStaticProps: GetStaticProps = async () => {
2828+ // Example for including static props in a Next.js function component page.
2929+ // Don't forget to include the respective types for any props passed into
3030+ // the component.
3131+ const items: User[] = sampleUserData;
3232+ return { props: { items } };
3333+};
3434+3535+export default WithStaticProps;
+145
types/cache-life.d.ts
···11+// Type definitions for Next.js cacheLife configs
22+33+declare module 'next/cache' {
44+ export { unstable_cache } from 'next/dist/server/web/spec-extension/unstable-cache'
55+ export {
66+ updateTag,
77+ revalidateTag,
88+ revalidatePath,
99+ refresh,
1010+ } from 'next/dist/server/web/spec-extension/revalidate'
1111+ export { unstable_noStore } from 'next/dist/server/web/spec-extension/unstable-no-store'
1212+1313+1414+ /**
1515+ * Cache this `"use cache"` for a timespan defined by the `"default"` profile.
1616+ * ```
1717+ * stale: 300 seconds (5 minutes)
1818+ * revalidate: 900 seconds (15 minutes)
1919+ * expire: never
2020+ * ```
2121+ *
2222+ * This cache may be stale on clients for 5 minutes before checking with the server.
2323+ * If the server receives a new request after 15 minutes, start revalidating new values in the background.
2424+ * It lives for the maximum age of the server cache. If this entry has no traffic for a while, it may serve an old value the next request.
2525+ */
2626+ export function cacheLife(profile: "default"): void
2727+2828+ /**
2929+ * Cache this `"use cache"` for a timespan defined by the `"seconds"` profile.
3030+ * ```
3131+ * stale: 30 seconds
3232+ * revalidate: 1 seconds
3333+ * expire: 60 seconds (1 minute)
3434+ * ```
3535+ *
3636+ * This cache may be stale on clients for 30 seconds before checking with the server.
3737+ * If the server receives a new request after 1 seconds, start revalidating new values in the background.
3838+ * If this entry has no traffic for 1 minute it will expire. The next request will recompute it.
3939+ */
4040+ export function cacheLife(profile: "seconds"): void
4141+4242+ /**
4343+ * Cache this `"use cache"` for a timespan defined by the `"minutes"` profile.
4444+ * ```
4545+ * stale: 300 seconds (5 minutes)
4646+ * revalidate: 60 seconds (1 minute)
4747+ * expire: 3600 seconds (1 hour)
4848+ * ```
4949+ *
5050+ * This cache may be stale on clients for 5 minutes before checking with the server.
5151+ * If the server receives a new request after 1 minute, start revalidating new values in the background.
5252+ * If this entry has no traffic for 1 hour it will expire. The next request will recompute it.
5353+ */
5454+ export function cacheLife(profile: "minutes"): void
5555+5656+ /**
5757+ * Cache this `"use cache"` for a timespan defined by the `"hours"` profile.
5858+ * ```
5959+ * stale: 300 seconds (5 minutes)
6060+ * revalidate: 3600 seconds (1 hour)
6161+ * expire: 86400 seconds (1 day)
6262+ * ```
6363+ *
6464+ * This cache may be stale on clients for 5 minutes before checking with the server.
6565+ * If the server receives a new request after 1 hour, start revalidating new values in the background.
6666+ * If this entry has no traffic for 1 day it will expire. The next request will recompute it.
6767+ */
6868+ export function cacheLife(profile: "hours"): void
6969+7070+ /**
7171+ * Cache this `"use cache"` for a timespan defined by the `"days"` profile.
7272+ * ```
7373+ * stale: 300 seconds (5 minutes)
7474+ * revalidate: 86400 seconds (1 day)
7575+ * expire: 604800 seconds (1 week)
7676+ * ```
7777+ *
7878+ * This cache may be stale on clients for 5 minutes before checking with the server.
7979+ * If the server receives a new request after 1 day, start revalidating new values in the background.
8080+ * If this entry has no traffic for 1 week it will expire. The next request will recompute it.
8181+ */
8282+ export function cacheLife(profile: "days"): void
8383+8484+ /**
8585+ * Cache this `"use cache"` for a timespan defined by the `"weeks"` profile.
8686+ * ```
8787+ * stale: 300 seconds (5 minutes)
8888+ * revalidate: 604800 seconds (1 week)
8989+ * expire: 2592000 seconds (1 month)
9090+ * ```
9191+ *
9292+ * This cache may be stale on clients for 5 minutes before checking with the server.
9393+ * If the server receives a new request after 1 week, start revalidating new values in the background.
9494+ * If this entry has no traffic for 1 month it will expire. The next request will recompute it.
9595+ */
9696+ export function cacheLife(profile: "weeks"): void
9797+9898+ /**
9999+ * Cache this `"use cache"` for a timespan defined by the `"max"` profile.
100100+ * ```
101101+ * stale: 300 seconds (5 minutes)
102102+ * revalidate: 2592000 seconds (1 month)
103103+ * expire: 31536000 seconds (365 days)
104104+ * ```
105105+ *
106106+ * This cache may be stale on clients for 5 minutes before checking with the server.
107107+ * If the server receives a new request after 1 month, start revalidating new values in the background.
108108+ * If this entry has no traffic for 365 days it will expire. The next request will recompute it.
109109+ */
110110+ export function cacheLife(profile: "max"): void
111111+112112+ /**
113113+ * Cache this `"use cache"` using a custom timespan.
114114+ * ```
115115+ * stale: ... // seconds
116116+ * revalidate: ... // seconds
117117+ * expire: ... // seconds
118118+ * ```
119119+ *
120120+ * This is similar to Cache-Control: max-age=`stale`,s-max-age=`revalidate`,stale-while-revalidate=`expire-revalidate`
121121+ *
122122+ * If a value is left out, the lowest of other cacheLife() calls or the default, is used instead.
123123+ */
124124+ export function cacheLife(profile: {
125125+ /**
126126+ * This cache may be stale on clients for ... seconds before checking with the server.
127127+ */
128128+ stale?: number,
129129+ /**
130130+ * If the server receives a new request after ... seconds, start revalidating new values in the background.
131131+ */
132132+ revalidate?: number,
133133+ /**
134134+ * If this entry has no traffic for ... seconds it will expire. The next request will recompute it.
135135+ */
136136+ expire?: number
137137+ }): void
138138+139139+140140+ import { cacheTag } from 'next/dist/server/use-cache/cache-tag'
141141+ export { cacheTag }
142142+143143+ export const unstable_cacheTag: typeof cacheTag
144144+ export const unstable_cacheLife: typeof cacheLife
145145+}