Skip to content

Commit 04bf691

Browse files
authored
add next.js instructions
1 parent 0121b5f commit 04bf691

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

Diff for: README.md

+89-2
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Say you are running `webpack-serve` on port 8080 and `netlify-lambda serve` on p
137137

138138
- If you are using with `create-react-app`, see [netlify/create-react-app-lambda](https://github.com/netlify/create-react-app-lambda/blob/f0e94f1d5a42992a2b894bfeae5b8c039a177dd9/src/setupProxy.js) for an example of how to do this with `create-react-app`. [setupProxy is partially documented in the CRA docs](https://facebook.github.io/create-react-app/docs/proxying-api-requests-in-development#configuring-the-proxy-manually). You can also learn how to do this from scratch in a video: https://www.youtube.com/watch?v=3ldSM98nCHI
139139
- If you are using Gatsby, see [their Advanced Proxying docs](https://www.gatsbyjs.org/docs/api-proxy/#advanced-proxying). This is implemented in the [JAMstack Hackathon Starter](https://github.com/sw-yx/jamstack-hackathon-starter), and here is an accompanying blogpost: [Turning the Static Dynamic: Gatsby + Netlify Functions + Netlify Identity](https://www.gatsbyjs.org/blog/2018-12-17-turning-the-static-dynamic/).
140-
- If you are using Next.js, see [this issue for how to proxy](https://github.com/netlify/netlify-lambda/pull/28#issuecomment-439675503).
140+
- If you are using Nuxt.js, see [this issue for how to proxy](https://github.com/netlify/netlify-lambda/pull/28#issuecomment-439675503).
141141
- If you are using Vue CLI, you may just use https://github.com/netlify/vue-cli-plugin-netlify-lambda/.
142142
- If you are using with Angular CLI, see the instructions below.
143143

@@ -159,7 +159,7 @@ module.exports = {
159159

160160
<details>
161161
<summary>
162-
<b>Using with `Angular CLI`</b>
162+
<b>Using with <code>Angular CLI</code></b>
163163
</summary>
164164

165165
CORS issues when trying to use netlify-lambdas locally with angular? you need to set up a proxy.
@@ -205,6 +205,93 @@ To make your life easier you can add these to your `scripts` in `package.json`
205205

206206
Obviously you need to run up `netlify-lambda` & `angular` at the same time.
207207

208+
</details>
209+
<details>
210+
<summary>
211+
<b>Using with <code>Next.js</code></b>
212+
</summary>
213+
214+
Next.js [doesnt use Webpack Dev Server](https://github.com/zeit/next.js/issues/2281), so you can't modify any config in `next.config.js` to get a proxy to run. However, since the CORS proxy issue only happens in dev mode (Functions are on the same domain when deployed on Netlify) you can run Next.js through a Node server for local development:
215+
216+
```js
217+
touch server.js
218+
yarn add -D http-proxy-middleware express
219+
```
220+
221+
```js
222+
// server.js
223+
/* eslint-disable no-console */
224+
const express = require('express');
225+
const next = require('next');
226+
227+
const devProxy = {
228+
'/.netlify': {
229+
target: 'http://localhost:9000',
230+
pathRewrite: { '^/.netlify/functions': '' }
231+
}
232+
};
233+
234+
const port = parseInt(process.env.PORT, 10) || 3000;
235+
const env = process.env.NODE_ENV;
236+
const dev = env !== 'production';
237+
const app = next({
238+
dir: '.', // base directory where everything is, could move to src later
239+
dev
240+
});
241+
242+
const handle = app.getRequestHandler();
243+
244+
let server;
245+
app
246+
.prepare()
247+
.then(() => {
248+
server = express();
249+
250+
// Set up the proxy.
251+
if (dev && devProxy) {
252+
const proxyMiddleware = require('http-proxy-middleware');
253+
Object.keys(devProxy).forEach(function(context) {
254+
server.use(proxyMiddleware(context, devProxy[context]));
255+
});
256+
}
257+
258+
// Default catch-all handler to allow Next.js to handle all other routes
259+
server.all('*', (req, res) => handle(req, res));
260+
261+
server.listen(port, err => {
262+
if (err) {
263+
throw err;
264+
}
265+
console.log(`> Ready on port ${port} [${env}]`);
266+
});
267+
})
268+
.catch(err => {
269+
console.log('An error occurred, unable to start the server');
270+
console.log(err);
271+
});
272+
273+
```
274+
275+
run your server and netlify-lambda at the same time:
276+
277+
```js
278+
// package.json
279+
"scripts": {
280+
"start": "cross-env NODE_ENV=dev npm-run-all --parallel start:app start:server",
281+
"start:app": "PORT=3000 node server.js",
282+
"start:server": "netlify-lambda serve functions"
283+
},
284+
```
285+
286+
and now you can ping Netlify Functions with locally emulated by `netlify-lambda`!
287+
288+
For production deployment, you have two options:
289+
290+
- [using `next export` to do static HTML export](https://nextjs.org/docs/#static-html-export)
291+
- [using the Next.js 8 `serverless` target option](https://nextjs.org/blog/next-8/#serverless-nextjs) to run your site in a function as well.
292+
293+
Just remember to configure your `netlify.toml` to point to the `Next.js` build folder and your `netlify-lambda` functions folder accordingly.
294+
208295
</details>
209296

210297
## Webpack Configuration

0 commit comments

Comments
 (0)