|
| 1 | +# next.js |
| 2 | + |
| 3 | +`Next.js` is a minimalistic framework for server-rendered React applications. |
| 4 | + |
| 5 | +## How to use |
| 6 | + |
| 7 | +The file-system is the main API. Every `.js` file becomes a route that gets automatically processed and rendered. |
| 8 | + |
| 9 | +Populate `pages/index.js` inside your project: |
| 10 | + |
| 11 | +``` |
| 12 | +import React from 'react' |
| 13 | +export default () => ( |
| 14 | + <div>Welcome to next.js!</div> |
| 15 | +) |
| 16 | +``` |
| 17 | + |
| 18 | +and then just run `next` and go to `http://localhost:3000` |
| 19 | + |
| 20 | +So far, we get: |
| 21 | + |
| 22 | +- Automatic transpilation and bundling (with webpack and babel) |
| 23 | +- Hot code reloading |
| 24 | +- Server rendering and indexing |
| 25 | + |
| 26 | +### Bundling (code splitting) |
| 27 | + |
| 28 | +Every `import` you declare gets bundled and served with each page |
| 29 | + |
| 30 | +``` |
| 31 | +import React from 'react' |
| 32 | +import cowsay from 'cowsay' |
| 33 | +export default () => ( |
| 34 | + <pre>{ cowsay('hi there!') }</pre> |
| 35 | +) |
| 36 | +``` |
| 37 | + |
| 38 | +That means pages never load unneccessary code! |
| 39 | + |
| 40 | +### CSS |
| 41 | + |
| 42 | +We use [Aphrodite](https://github.com/Khan/aphrodite) to provide a great built-in solution for CSS modularization |
| 43 | + |
| 44 | +``` |
| 45 | +import React from 'react' |
| 46 | +import { css, StyleSheet } from 'next/css' |
| 47 | +
|
| 48 | +export default () => { |
| 49 | + <div className={ css(styles.main) }> |
| 50 | + Hello world |
| 51 | + </div> |
| 52 | +}) |
| 53 | +
|
| 54 | +const styles = StyleSheet.create({ |
| 55 | + main: { |
| 56 | + background: 'red', |
| 57 | + '@media (max-width: 600px)': { |
| 58 | + background: 'blue' |
| 59 | + } |
| 60 | + } |
| 61 | +}) |
| 62 | +``` |
| 63 | + |
| 64 | +### `<head>` side effects |
| 65 | + |
| 66 | +We expose a built-in component for appending elements to the `<head>` of the page. |
| 67 | + |
| 68 | +``` |
| 69 | +import React from 'react' |
| 70 | +import Head from 'next/head' |
| 71 | +export default () => ( |
| 72 | + <Head> |
| 73 | + <title>My page title</title> |
| 74 | + <meta name="viewport" content="initial-scale=1.0, width=device-width" /> |
| 75 | + </Head> |
| 76 | + <p>Hello world!</p> |
| 77 | +) |
| 78 | +``` |
| 79 | + |
| 80 | +### Stateful components |
| 81 | + |
| 82 | +When state, lifecycle hooks or initial data population you can export a `React.Component`: |
| 83 | + |
| 84 | +``` |
| 85 | +import React from 'react' |
| 86 | +export default class extends React.Component { |
| 87 | + async getInitialProps ({ isServer, req }) { |
| 88 | + return isServer |
| 89 | + ? { userAgent: req.headers.userAgent } |
| 90 | + : { userAgent: navigator.userAgent } |
| 91 | + } |
| 92 | +
|
| 93 | + render () { |
| 94 | + return <div> |
| 95 | + Hello World {this.props.userAgent} |
| 96 | + </div> |
| 97 | + } |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +### Routing |
| 102 | + |
| 103 | +Client-side transitions between routes are enabled via a `<Link>` component |
| 104 | + |
| 105 | +#### pages/index.js |
| 106 | + |
| 107 | +``` |
| 108 | +import React from 'react' |
| 109 | +import Link from 'next/link' |
| 110 | +export default () => ( |
| 111 | + <div>Click <Link href="/about"><a>here</a></Link> to read more</div> |
| 112 | +) |
| 113 | +``` |
| 114 | + |
| 115 | +#### pages/about.js |
| 116 | + |
| 117 | +``` |
| 118 | +import React from 'react' |
| 119 | +export default () => ( |
| 120 | + <p>Welcome to About!</p> |
| 121 | +) |
| 122 | +``` |
| 123 | + |
| 124 | +Client-side routing behaves exactly like the native UA: |
| 125 | + |
| 126 | +1. The component is fetched |
| 127 | +2. If it defines `getInitialProps`, data is fetched. If an error occurs, `_error.js` is rendered |
| 128 | +3. After 1 and 2 complete, `pushState` is performed and the new component rendered |
| 129 | + |
| 130 | +Each top-level component receives a `url` property with the following API: |
| 131 | + |
| 132 | +- `path` - `String` of the current path excluding the query string |
| 133 | +- `query` - `Object` with the parsed query string. Defaults to `{}` |
| 134 | +- `push(url)` - performs a `pushState` call associated with the current component |
| 135 | +- `replace(url)` - performs a `replaceState` call associated with the current component |
| 136 | +- `pushTo(url)` - performs a `pushState` call that renders the new `url`. This is equivalent to following a `<Link>` |
| 137 | +- `replaceTo(url)` - performs a `replaceState` call that renders the new `url` |
| 138 | + |
| 139 | +### Error handling |
| 140 | + |
| 141 | +404 or 500 errors are handled both client and server side by a default component `error.js`. If you wish to override it, define a `_error.js`: |
| 142 | + |
| 143 | +``` |
| 144 | +import React from 'react' |
| 145 | +export default ({ statusCode }) => ( |
| 146 | + <p>An error { statusCode } occurred</p> |
| 147 | +) |
| 148 | +``` |
| 149 | + |
| 150 | +### Production deployment |
| 151 | + |
| 152 | +To deploy, run: |
| 153 | + |
| 154 | +``` |
| 155 | +next build |
| 156 | +next start |
| 157 | +``` |
| 158 | + |
| 159 | +For example, to deploy with `now` a `package.json` like follows is recommended: |
| 160 | + |
| 161 | +``` |
| 162 | +{ |
| 163 | + "name": "my-app", |
| 164 | + "dependencies": { |
| 165 | + "next": "latest" |
| 166 | + }, |
| 167 | + "scripts": { |
| 168 | + "dev": "next", |
| 169 | + "build": "next build", |
| 170 | + "start": "next start" |
| 171 | + } |
| 172 | +} |
| 173 | +``` |
0 commit comments