Skip to content

Commit 43c1190

Browse files
shaswatsaxenaLuis Alvarez
andauthored
Updated with-typescript example to SSG (vercel#11081)
* Update with-typescript example to SSG * Fixed usage of localhost inside SSG methods Co-authored-by: Luis Alvarez <[email protected]>
1 parent bf93ca4 commit 43c1190

File tree

7 files changed

+53
-78
lines changed

7 files changed

+53
-78
lines changed

examples/with-typescript/components/Layout.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const Layout: React.FunctionComponent<Props> = ({
2828
|{' '}
2929
<Link href="/users">
3030
<a>Users List</a>
31-
</Link>
31+
</Link>{' '}
32+
| <a href="/api/users">Users API</a>
3233
</nav>
3334
</header>
3435
{children}

examples/with-typescript/pages/about.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import * as React from 'react'
1+
import React from 'react'
22
import Link from 'next/link'
33
import Layout from '../components/Layout'
44

examples/with-typescript/pages/api/users/[id].ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import * as React from 'react'
21
import Link from 'next/link'
32
import Layout from '../components/Layout'
4-
import { NextPage } from 'next'
53

6-
const IndexPage: NextPage = () => {
7-
return (
8-
<Layout title="Home | Next.js + TypeScript Example">
9-
<h1>Hello Next.js 👋</h1>
10-
<p>
11-
<Link href="/about">
12-
<a>About</a>
13-
</Link>
14-
</p>
15-
</Layout>
16-
)
17-
}
4+
const IndexPage = () => (
5+
<Layout title="Home | Next.js + TypeScript Example">
6+
<h1>Hello Next.js 👋</h1>
7+
<p>
8+
<Link href="/about">
9+
<a>About</a>
10+
</Link>
11+
</p>
12+
</Layout>
13+
)
1814

1915
export default IndexPage

examples/with-typescript/pages/users/[id].tsx

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
1-
import * as React from 'react'
2-
import { NextPageContext } from 'next'
1+
import React from 'react'
2+
import { GetStaticProps, GetStaticPaths } from 'next'
33

44
import { User } from '../../interfaces'
5+
import { sampleUserData } from '../../utils/sample-data'
56
import Layout from '../../components/Layout'
67
import ListDetail from '../../components/ListDetail'
7-
import { sampleFetchWrapper } from '../../utils/sample-api'
88

99
type Props = {
1010
item?: User
1111
errors?: string
1212
}
1313

14-
class InitialPropsDetail extends React.Component<Props> {
15-
static getInitialProps = async ({ query }: NextPageContext) => {
16-
try {
17-
const { id } = query
18-
const item = await sampleFetchWrapper(
19-
`http://localhost:3000/api/users/${Array.isArray(id) ? id[0] : id}`
20-
)
21-
return { item }
22-
} catch (err) {
23-
return { errors: err.message }
24-
}
25-
}
26-
14+
export default class StaticPropsDetail extends React.Component<Props> {
2715
render() {
2816
const { item, errors } = this.props
2917

@@ -49,4 +37,28 @@ class InitialPropsDetail extends React.Component<Props> {
4937
}
5038
}
5139

52-
export default InitialPropsDetail
40+
export const getStaticPaths: GetStaticPaths = async () => {
41+
// Get the paths we want to pre-render based on users
42+
const paths = sampleUserData.map(user => ({
43+
params: { id: user.id.toString() },
44+
}))
45+
46+
// We'll pre-render only these paths at build time.
47+
// { fallback: false } means other routes should 404.
48+
return { paths, fallback: false }
49+
}
50+
51+
// This function gets called at build time on server-side.
52+
// It won't be called on client-side, so you can even do
53+
// direct database queries.
54+
export const getStaticProps: GetStaticProps = async ({ params }) => {
55+
try {
56+
const id = params?.id
57+
const item = sampleUserData.find(data => data.id === Number(id))
58+
// By returning { props: item }, the StaticPropsDetail component
59+
// will receive `item` as a prop at build time
60+
return { props: { item } }
61+
} catch (err) {
62+
return { props: { errors: err.message } }
63+
}
64+
}
Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
import { NextPage } from 'next'
1+
import { GetStaticProps } from 'next'
22
import Link from 'next/link'
33

4+
import { User } from '../../interfaces'
5+
import { sampleUserData } from '../../utils/sample-data'
46
import Layout from '../../components/Layout'
57
import List from '../../components/List'
6-
import { User } from '../../interfaces'
7-
import { sampleFetchWrapper } from '../../utils/sample-api'
88

99
type Props = {
1010
items: User[]
11-
pathname: string
1211
}
1312

14-
const WithInitialProps: NextPage<Props> = ({ items, pathname }) => (
13+
const WithStaticProps = ({ items }: Props) => (
1514
<Layout title="Users List | Next.js + TypeScript Example">
1615
<h1>Users List</h1>
1716
<p>
18-
Example fetching data from inside <code>getInitialProps()</code>.
17+
Example fetching data from inside <code>getStaticProps()</code>.
1918
</p>
20-
<p>You are currently on: {pathname}</p>
19+
<p>You are currently on: /users</p>
2120
<List items={items} />
2221
<p>
2322
<Link href="/">
@@ -27,15 +26,12 @@ const WithInitialProps: NextPage<Props> = ({ items, pathname }) => (
2726
</Layout>
2827
)
2928

30-
WithInitialProps.getInitialProps = async ({ pathname }) => {
31-
// Example for including initial props in a Next.js function component page.
29+
export const getStaticProps: GetStaticProps = async () => {
30+
// Example for including static props in a Next.js function component page.
3231
// Don't forget to include the respective types for any props passed into
3332
// the component.
34-
const items: User[] = await sampleFetchWrapper(
35-
'http://localhost:3000/api/users'
36-
)
37-
38-
return { items, pathname }
33+
const items: User[] = sampleUserData
34+
return { props: { items } }
3935
}
4036

41-
export default WithInitialProps
37+
export default WithStaticProps

examples/with-typescript/utils/sample-api.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)