Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit c2ffe80

Browse files
committedMar 20, 2019
initial commit
0 parents  commit c2ffe80

File tree

8 files changed

+194
-0
lines changed

8 files changed

+194
-0
lines changed
 

‎.nowignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
README.md
2+
.next
3+
node_modules

‎README.md

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
# Next.js
2+
3+
In this example we will be deploying a simple "Hello World" example with Next.js.
4+
5+
### Getting started with Next.js
6+
7+
- Create a `pages` folder with an `index.js` file with the following code:
8+
9+
```jsx
10+
import Link from "next/link";
11+
import Header from "../components/header";
12+
13+
export default () => (
14+
<main>
15+
<Header />
16+
<section>
17+
<Link href="/about">
18+
<a>Go to About Me</a>
19+
</Link>
20+
</section>
21+
</main>
22+
);
23+
```
24+
25+
- Now lets create an `about.js` file inside the `pages` folder with the following code:
26+
27+
```jsx
28+
import { Component } from "react";
29+
import Link from "next/link";
30+
import Header from "../components/header";
31+
32+
class AboutPage extends Component {
33+
static getInitialProps() {
34+
const isServer = typeof window === "undefined";
35+
return { isServer };
36+
}
37+
38+
render() {
39+
return (
40+
<main>
41+
<Header />
42+
<section>
43+
<p>
44+
This is another page of the SSR example, you accessed it{" "}
45+
<strong>{this.props.isServer ? "server" : "client"} side</strong>.
46+
</p>
47+
<p>
48+
You can reload to see how the page change.
49+
</p>
50+
<Link href="/">
51+
<a>Go to Home</a>
52+
</Link>
53+
</section>
54+
</main>
55+
);
56+
}
57+
}
58+
59+
export default AboutPage;
60+
```
61+
62+
- As you might noticed we have a component that is shared by both `index.js` and `about.js` files, let's create that one now. Create a folder named `components` with a file named `header.js` in it and add the following code:
63+
64+
```jsx
65+
export default () => (
66+
<header>
67+
<h1>Next.js Example</h1>
68+
</header>
69+
);
70+
```
71+
72+
- Finally in order for Next.js to be deployed we could either have a `next.config.js` or a `package.json`, for this example we are just going to create a `next.config.js` with the following code:
73+
74+
```js
75+
module.exports = {
76+
target: 'serverless'
77+
}
78+
```
79+
80+
### Deploy with Now
81+
82+
First we need to create a `now.json` configuration file to instruct Now how to build the project.
83+
84+
For this example we will be using our newest version [Now 2.0](https://zeit.co/now).
85+
86+
By adding the `version` key to the `now.json` file, we can specify which Now Platform version to use.
87+
88+
We also need to define each builders we would like to use. [Builders](https://zeit.co/docs/v2/deployments/builders/overview/) are modules that take a deployment's source and return an output, consisting of [either static files or dynamic Lambdas](https://zeit.co/docs/v2/deployments/builds/#sources-and-outputs).
89+
90+
In this case we are going to use `@now/next` to build and deploy our Next.js application selecting the `next.config.js` as our entry point. We will also define a name for our project (optional).
91+
92+
```json
93+
{
94+
"version": 2,
95+
"name": "nextjs",
96+
"builds": [
97+
{ "src": "next.config.js", "use": "@now/next" }
98+
]
99+
}
100+
```
101+
102+
Visit our [documentation](https://zeit.co/docs/v2/deployments/configuration) for more information on the `now.json` configuration file.
103+
104+
We are now ready to deploy the app.
105+
106+
```
107+
now
108+
```

‎components/header.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function Header() {
2+
return (
3+
<header>
4+
<h1>Next.js Example on Now 2.0</h1>
5+
</header>
6+
);
7+
}
8+
9+
export default Header;

‎next.config.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
target: 'serverless'
3+
}

‎now.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"version": 2,
3+
"name": "nextjs",
4+
"builds": [
5+
{
6+
"src": "package.json",
7+
"use": "@now/next"
8+
}
9+
]
10+
}

‎package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"scripts": {
3+
"build": "next build",
4+
"dev": "next",
5+
"now-build": "next build"
6+
},
7+
"dependencies": {
8+
"next": "^8.0.0",
9+
"react": "^16.7.0",
10+
"react-dom": "^16.7.0"
11+
}
12+
}

‎pages/about.js

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Component } from "react";
2+
import Link from "next/link";
3+
import Header from "../components/header";
4+
5+
class AboutPage extends Component {
6+
static getInitialProps() {
7+
const isServer = typeof window === "undefined";
8+
return { isServer };
9+
}
10+
11+
render() {
12+
return (
13+
<main>
14+
<Header />
15+
<section>
16+
<p>
17+
This is another page of the SSR example, you accessed it{" "}
18+
<strong>{this.props.isServer ? "server" : "client"} side</strong>.
19+
</p>
20+
<p>
21+
You can reload to see how the page change.
22+
</p>
23+
<Link href="/">
24+
<a>Go to Home</a>
25+
</Link>
26+
</section>
27+
</main>
28+
);
29+
}
30+
}
31+
32+
export default AboutPage;

‎pages/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Link from "next/link";
2+
import Header from "../components/header";
3+
4+
function Index() {
5+
return (
6+
<main>
7+
<Header />
8+
<section>
9+
<Link href="/about">
10+
<a>Go to About Me</a>
11+
</Link>
12+
</section>
13+
</main>
14+
);
15+
}
16+
17+
export default Index;

0 commit comments

Comments
 (0)
Please sign in to comment.