|
| 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 | +``` |
0 commit comments