Skip to content

Commit 217b338

Browse files
committedAug 1, 2022
added initial static export template
1 parent 635d811 commit 217b338

File tree

7 files changed

+897
-0
lines changed

7 files changed

+897
-0
lines changed
 

‎.gitignore

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
# testing
9+
/coverage
10+
11+
# next.js
12+
/.next/
13+
/out/
14+
15+
# production
16+
/build
17+
18+
# misc
19+
.DS_Store
20+
*.pem
21+
22+
# debug
23+
npm-debug.log*
24+
yarn-debug.log*
25+
yarn-error.log*
26+
.pnpm-debug.log*
27+
28+
# local env files
29+
.env*.local
30+
31+
# vercel
32+
.vercel
33+
34+
# typescript
35+
*.tsbuildinfo
36+
next-env.d.ts

‎README.md

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Static export example
2+
3+
This example show how to export to static HTML files your Next.js application fetching data from an API to generate a dynamic list of pages.
4+
5+
When trying to run `npm start` it will build and export your pages into the `out` folder and serve them on `localhost:5000`.
6+
7+
## Deploy your own
8+
9+
Deploy the example using [Vercel](https://vercel.com?utm_source=github&utm_medium=readme&utm_campaign=next-example) or preview live with [StackBlitz](https://stackblitz.com/github/vercel/next.js/tree/canary/examples/with-static-export)
10+
11+
[![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/git/external?repository-url=https://github.com/vercel/next.js/tree/canary/examples/with-static-export)
12+
13+
## How to use
14+
15+
Execute [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app) with [npm](https://docs.npmjs.com/cli/init), [Yarn](https://yarnpkg.com/lang/en/docs/cli/create/), or [pnpm](https://pnpm.io) to bootstrap the example:
16+
17+
```bash
18+
npx create-next-app --example with-static-export with-static-export-app
19+
```
20+
21+
```bash
22+
yarn create next-app --example with-static-export with-static-export-app
23+
```
24+
25+
```bash
26+
pnpm create next-app --example with-static-export with-static-export-app
27+
```

‎components/post.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Link from 'next/link'
2+
3+
export default function Post({ title, body, id }) {
4+
return (
5+
<article>
6+
<h2>{title}</h2>
7+
<p>{body}</p>
8+
<Link href={`/post/${id}`}>
9+
<a>Read more...</a>
10+
</Link>
11+
</article>
12+
)
13+
}

‎package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"private": true,
3+
"dependencies": {
4+
"next": "latest",
5+
"react": "^17.0.2",
6+
"react-dom": "^17.0.2",
7+
"serve": "11.2.0"
8+
},
9+
"scripts": {
10+
"dev": "next",
11+
"build": "next build",
12+
"preexport": "npm run build",
13+
"export": "next export",
14+
"prestart": "npm run export",
15+
"start": "serve out"
16+
}
17+
}

‎pages/index.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import Head from 'next/head'
2+
3+
import Post from '../components/post'
4+
5+
export async function getStaticProps() {
6+
// fetch list of posts
7+
const response = await fetch(
8+
'https://jsonplaceholder.typicode.com/posts?_page=1'
9+
)
10+
const postList = await response.json()
11+
return {
12+
props: {
13+
postList,
14+
},
15+
}
16+
}
17+
18+
export default function IndexPage({ postList }) {
19+
return (
20+
<main>
21+
<Head>
22+
<title>Home page</title>
23+
</Head>
24+
25+
<h1>List of posts</h1>
26+
27+
<section>
28+
{postList.map((post) => (
29+
<Post {...post} key={post.id} />
30+
))}
31+
</section>
32+
</main>
33+
)
34+
}

‎pages/post/[id].js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import Link from 'next/link'
2+
import Head from 'next/head'
3+
4+
export async function getStaticPaths() {
5+
const response = await fetch(
6+
'https://jsonplaceholder.typicode.com/posts?_page=1'
7+
)
8+
const postList = await response.json()
9+
return {
10+
paths: postList.map((post) => {
11+
return {
12+
params: {
13+
id: `${post.id}`,
14+
},
15+
}
16+
}),
17+
fallback: false,
18+
}
19+
}
20+
21+
export async function getStaticProps({ params }) {
22+
// fetch single post detail
23+
const response = await fetch(
24+
`https://jsonplaceholder.typicode.com/posts/${params.id}`
25+
)
26+
const post = await response.json()
27+
return {
28+
props: post,
29+
}
30+
}
31+
32+
export default function Post({ title, body }) {
33+
return (
34+
<main>
35+
<Head>
36+
<title>{title}</title>
37+
</Head>
38+
39+
<h1>{title}</h1>
40+
41+
<p>{body}</p>
42+
43+
<Link href="/">
44+
<a>Go back to home</a>
45+
</Link>
46+
</main>
47+
)
48+
}

‎yarn.lock

+722
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)
Please sign in to comment.