Skip to content

Commit c46cd3c

Browse files
authored
Add docs for Serverless target (#6070)
I need feedback on if this is clear enough for users.
1 parent b4e6ded commit c46cd3c

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

packages/next/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,66 @@ Note: `NODE_ENV` is properly configured by the `next` subcommands, if absent, to
15871587
15881588
Note: we recommend putting `.next`, or your [custom dist folder](https://github.com/zeit/next.js#custom-configuration), in `.gitignore` or `.npmignore`. Otherwise, use `files` or `now.files` to opt-into a whitelist of files you want to deploy, excluding `.next` or your custom dist folder.
15891589
1590+
### Serverless deployment
1591+
1592+
<details>
1593+
<summary><b>Examples</b></summary>
1594+
<ul>
1595+
<li><a href="https://github.com/zeit/now-examples/tree/master/nextjs">Now.sh</a></li>
1596+
<li>We encourage contributing more examples to this section</li>
1597+
</ul>
1598+
</details>
1599+
1600+
Serverless deployment dramatically improves reliability and scalability by splitting your application into many entrypoints. In case of Next.js an entrypoint is a page in the `pages` directory.
1601+
1602+
To enable building serverless functions you have to enable the `serverless` build `target` in `next.config.js`:
1603+
1604+
```js
1605+
// next.config.js
1606+
module.exports = {
1607+
target: 'serverless'
1608+
}
1609+
```
1610+
1611+
The serverless target will output a single file per page, this file is completely standalone and doesn't require any dependencies to run:
1612+
1613+
- `pages/index.js` => `.next/serverless/pages/index.js`
1614+
- `pages/about.js` => `.next/serverless/pages/about.js`
1615+
1616+
The signature of the Next.js Serverless function is similar to the Node.js HTTP server callback:
1617+
1618+
```ts
1619+
export function render(req: http.IncomingMessage, res: http.ServerResponse) => void
1620+
```
1621+
1622+
- [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage)
1623+
- [http.ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse)
1624+
- `void` refers to the function not having a return value. Calling the function will finish the request.
1625+
1626+
Next.js provides low-level APIs for Serverless as hosting platforms have different function signatures. In general you will want to wrap the output of a Next.js Serverless build with a compatability layer.
1627+
1628+
For example if the platform supports the Node.js [`http.Server`](https://nodejs.org/api/http.html#http_class_http_server) class:
1629+
1630+
```js
1631+
const http = require('http')
1632+
const page = require('./.next/serverless/about.js')
1633+
const server = new http.Server((req, res) => page.render(req, res))
1634+
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
1635+
```
1636+
1637+
For specific platform examples see [the examples section above](#serverless-deployment).
1638+
1639+
To summarize:
1640+
1641+
- Low-level API for implementing Serverless deployment
1642+
- Every page in the `pages` directory becomes a serverless function
1643+
- Creates the smallest possible Serverless function (50Kb base zip size)
1644+
- Optimized for fast cold start of the function
1645+
- The serverless function has 0 dependencies (they are included in the function bundle)
1646+
- Uses the [http.IncomingMessage](https://nodejs.org/api/http.html#http_class_http_incomingmessage) and [http.ServerResponse](https://nodejs.org/api/http.html#http_class_http_serverresponse) from Node.js
1647+
- opt-in using `target: 'serverless'` in `next.config.js`
1648+
- Does not load `next.config.js` when executing the function, note that this means `publicRuntimeConfig` / `serverRuntimeConfig` are not supported
1649+
15901650
## Browser support
15911651

15921652
Next.js supports IE11 and all modern browsers out of the box using [`@babel/preset-env`](https://new.babeljs.io/docs/en/next/babel-preset-env.html). In order to support IE11 Next.js adds a global `Promise` polyfill. In cases where your own code or any external NPM dependencies you are using requires features not supported by your target browsers you will need to implement polyfills.

0 commit comments

Comments
 (0)