Skip to content

Commit 61f5087

Browse files
authored
Kick off the readme
1 parent 9d9c1d6 commit 61f5087

File tree

1 file changed

+221
-0
lines changed

1 file changed

+221
-0
lines changed

README.md

Lines changed: 221 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,221 @@
1+
![Next.js on Netlify Build Plugin](nextonnetlify.png)
2+
3+
<p align="center">
4+
<a aria-label="npm version" href="https://www.npmjs.com/package/next-on-netlify">
5+
<img alt="" src="https://img.shields.io/npm/v/next-on-netlify">
6+
</a>
7+
<a aria-label="MIT License" href="https://img.shields.io/npm/l/next-on-netlify">
8+
<img alt="" src="https://img.shields.io/npm/l/next-on-netlify">
9+
</a>
10+
<a aria-label="npm downloads" href="https://www.npmjs.com/package/next-on-netlify">
11+
<img alt="" src="https://img.shields.io/npm/dt/next-on-netlify">
12+
</a>
13+
<a aria-label="Tested with Cypress" href="https://www.cypress.io/">
14+
<img alt="" src="https://img.shields.io/badge/tested%20with-Cypress-04C38E.svg">
15+
</a>
16+
</p>
17+
18+
19+
`next-on-netlify` is a utility for enabling server-side rendering in Next.js on Netlify. It wraps your application in a tiny compatibility layer, so that pages can use Netlify Functions to be server-side rendered.
20+
21+
**TL;DR: You can enable SSR in your Next.js applications with 3 simple steps, listed [here](#setup)!**
22+
23+
- Demo: https://next-on.netlify.com/
24+
- Example repository: https://github.com/netlify-labs/next-on-netlify-demo
25+
26+
## Table of Contents
27+
28+
- [Installation](#installation)
29+
- [Setup](#setup)
30+
- [1. Set Next.js target to serverless](#1-set-nextjs-target-to-serverless)
31+
- [2. Add postbuild hook](#2-add-postbuild-hook)
32+
- [3. Configure Netlify](#3-configure-netlify)
33+
- [Optional Extras](#optional-extras)
34+
- [Preview Locally](#preview-locally)
35+
- [Custom Netlify Redirects](#custom-netlify-redirects)
36+
- [Custom Netlify Functions](#custom-netlify-functions)
37+
- [Caveats](#caveats)
38+
- [Preview Mode](#preview-mode)
39+
- [Fallbacks for Pages with `getStaticPaths`](#fallbacks-for-pages-with-getstaticpaths)
40+
- [Credits](#credits)
41+
- [Showcase](#showcase)
42+
43+
44+
## Installation
45+
46+
```
47+
npm install --save next-on-netlify
48+
```
49+
50+
## Setup
51+
52+
#### 1. Set Next.js target to serverless
53+
54+
We must build our Next.js app as a serverless app. You can read more about serverless Next.js [here](https://nextjs.org/docs/api-reference/next.config.js/build-target#serverless-target).
55+
56+
It's super simple. Just create a `next.config.js` file and write the following:
57+
58+
```js
59+
// next.config.js
60+
61+
module.exports = {
62+
// Target must be serverless
63+
target: "serverless",
64+
};
65+
```
66+
67+
If binaries are needed in the deployment the following configuration is needed ([Prisma](https://github.com/prisma/prisma) is an example):
68+
69+
```js
70+
// next.config.js
71+
72+
module.exports = {
73+
// Target must be experimental-serverless-trace
74+
// Your build time will be longer with this option
75+
target: "experimental-serverless-trace",
76+
};
77+
```
78+
79+
#### 2. Add postbuild hook
80+
81+
The next-on-netlify package adds the `next-on-netlify` command. When we run this command, some magic happens to prepare our Next.js app for hosting on Netlify\*.
82+
83+
We want the next-on-netlify command to run after we build our NextJS application. So let's add a postbuild hook to our package.json file:
84+
85+
```json
86+
{
87+
"name": "my-nextjs-app",
88+
"scripts": {
89+
"dev": "next",
90+
"build": "next build",
91+
"postbuild": "next-on-netlify"
92+
},
93+
....
94+
}
95+
```
96+
97+
\*If you're curious about the "magic", check out the well-documented [`next-on-netlify.js` file](https://github.com/netlify/next-on-netlify/blob/master/next-on-netlify.js).
98+
99+
#### 3. Configure Netlify
100+
101+
We're almost done! We just have to tell Netlify how to build our Next.js app, where the functions folder is located, and which folder to upload to its CDN. We do that with a `netlify.toml` file and the following instructions:
102+
103+
```toml
104+
[build]
105+
command = "npm run build"
106+
functions = "out_functions"
107+
publish = "out_publish"
108+
```
109+
110+
We're done. Let's deploy 🚀🚀🚀
111+
112+
## Optional Extras
113+
114+
#### Preview Locally
115+
116+
I recommend you still use `next dev` to build and preview your application locally.
117+
118+
But if you want to emulate the Netlify deployment on your computer, you can also run `next-on-netlify` locally and then use `netlify-cli` to preview the result.
119+
120+
First, install the latest version of `netlify-cli` (you can also [look at package.json](https://github.com/netlify/next-on-netlify/blob/master/package.json) to see the version that next-on-netlify has been tested against):
121+
122+
```bash
123+
npm install -g netlify-cli
124+
```
125+
126+
Then, add the following `[dev]` block to your `netlify.toml`:
127+
128+
```toml
129+
# netlify.toml
130+
131+
# [build]
132+
# ...
133+
134+
[dev]
135+
functions = "out_functions"
136+
publish = "out_publish"
137+
# We manually set the framework to static, otherwise Netlify automatically
138+
# detects Next.js and redirects do not work.
139+
# Read more: https://github.com/netlify/cli/blob/master/docs/netlify-dev.md#project-detection
140+
framework = "#static"
141+
```
142+
143+
Lastly, add the following lines to your `.gitignore`:
144+
145+
```shell
146+
# .gitignore
147+
148+
# Files generated by next-on-netlify command
149+
/out_publish/
150+
/out_functions/
151+
```
152+
153+
Now you're all set.
154+
155+
From now on, whenever you want to preview your application locally, just run:
156+
157+
1. `npm run build`: This will run `next build` to build your Next.js app and `next-on-netlify` to prepare your Next.js app for compatibility with Netlify
158+
1. `netlify dev`: This will emulate Netlify on your computer and let you preview your app on `http://localhost:8888`.
159+
160+
#### Custom Netlify Redirects
161+
162+
You can define custom redirects in a `_redirects` and/or in your `netlify.toml` file.
163+
The precedence of these rules are:
164+
165+
- `_redirects`
166+
- `next-on-netlify` redirects
167+
- `netlify.toml`
168+
169+
[Read more about Netlify redirects here](https://docs.netlify.com/routing/redirects/).
170+
171+
#### Custom Netlify Functions
172+
173+
`next-on-netlify` creates one Netlify Function for each of your
174+
SSR pages and API endpoints. It is currently not possible to create custom Netlify Functions. Let me know if you have a need for this feature and we can add it.
175+
176+
## Caveats
177+
178+
### Preview Mode
179+
180+
[Next.js Preview Mode](https://nextjs.org/docs/advanced-features/preview-mode) does not work on pages that are pre-rendered (pages with `getStaticProps`). Netlify currently does not support cookie-based redirects, which are needed for supporting preview mode on pre-rendered pages. Preview mode works correctly on any server-side-rendered pages (pages with `getInitialProps` or `getServerSideProps`). See: [Issue #10](https://github.com/netlify/next-on-netlify/issues/10)
181+
182+
### Fallbacks for Pages with `getStaticPaths`
183+
184+
[Fallback pages](https://nextjs.org/docs/basic-features/data-fetching#fallback-true) behave differently with `next-on-netlify` than they do with Next.js. On Next.js, when navigating to a path that is not defined in `getStaticPaths`, it first displays the fallback page. Next.js then generates the HTML in the background and caches it for future requests.
185+
186+
With `next-on-netlify`, when navigating to a path that is not defined in `getStaticPaths`, it server-side renders the page and sends it directly to the user. The user never sees the fallback page. The page is not cached for future requests.
187+
188+
For more on this, see: [Issue #7](https://github.com/netlify/next-on-netlify/issues/7#issuecomment-636883539)
189+
190+
191+
## Credits
192+
193+
This package is maintained by [Lindsay Levine](https://github.com/lindsaylevine), [Finn Woelm](https://github.com/FinnWoelm), and [Cassidy Williams](https://github.com/cassidoo).
194+
195+
📣 Shoutout to [@mottox2](https://github.com/mottox2) (a pioneer of hosting Next.js on Netlify) and [@danielcondemarin](https://github.com/danielcondemarin) (author of serverless-next.js for AWS). The two were big inspirations for this package.
196+
197+
🙌 Big "thank you" to the following people for their contributions, support, and beta testing:
198+
199+
- [@spencewood](https://github.com/spencewood)
200+
- [@alxhghs](https://github.com/alxhghs)
201+
- [@gamliela](https://github.com/gamliela)
202+
- [@wei](https://github.com/wei)
203+
- [@laugharn](https://github.com/laugharn)
204+
- [@rajington](https://github.com/rajington)
205+
- [@etrepum](https://github.com/etrepum)
206+
- [@jonasbuntinx](https://github.com/jonasbuntinx)
207+
- [@joostmeijles](https://github.com/joostmeijles)
208+
209+
210+
## Showcase
211+
212+
The following sites are built with `next-on-netlify`:
213+
214+
![opinionatedreact.com](https://raw.githubusercontent.com/netlify/next-on-netlify/master/assets/showcase-opinionatedreact.png)
215+
[opinionatedreact.com](https://opinionatedreact.com/) ([via Twitter](https://twitter.com/NikkitaFTW/status/1302667952920162309))
216+
217+
![missionbit.org](https://raw.githubusercontent.com/netlify/next-on-netlify/master/assets/showcase-missionbit.png)
218+
[missionbit.org](https://www.missionbit.org/) ([#18](https://github.com/netlify/next-on-netlify/pull/18#issuecomment-643828966))
219+
220+
Are you building something awesome with `next-on-netlify`? 🔥 Let us know and we will feature it here :)
221+

0 commit comments

Comments
 (0)