2020-03-29 01:18:22 +01:00
|
|
|
import devalue from 'next/dist/compiled/devalue'
|
2020-03-29 01:21:07 +01:00
|
|
|
import escapeRegexp from 'next/dist/compiled/escape-string-regexp'
|
2019-05-29 13:57:26 +02:00
|
|
|
import { join } from 'path'
|
|
|
|
import { parse } from 'querystring'
|
2020-02-12 02:16:42 +01:00
|
|
|
import { loader } from 'webpack'
|
|
|
|
import { API_ROUTE } from '../../../lib/constants'
|
2019-09-04 16:00:54 +02:00
|
|
|
import {
|
|
|
|
BUILD_MANIFEST,
|
|
|
|
REACT_LOADABLE_MANIFEST,
|
2020-02-12 02:16:42 +01:00
|
|
|
ROUTES_MANIFEST,
|
2019-09-04 16:00:54 +02:00
|
|
|
} from '../../../next-server/lib/constants'
|
|
|
|
import { isDynamicRoute } from '../../../next-server/lib/router/utils'
|
2020-02-12 02:16:42 +01:00
|
|
|
import { __ApiPreviewProps } from '../../../next-server/server/api-utils'
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
|
|
|
|
export type ServerlessLoaderQuery = {
|
2019-05-29 13:57:26 +02:00
|
|
|
page: string
|
|
|
|
distDir: string
|
|
|
|
absolutePagePath: string
|
|
|
|
absoluteAppPath: string
|
|
|
|
absoluteDocumentPath: string
|
|
|
|
absoluteErrorPath: string
|
2019-08-20 18:38:14 +02:00
|
|
|
buildId: string
|
2019-05-29 13:57:26 +02:00
|
|
|
assetPrefix: string
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
generateEtags: string
|
2019-05-29 02:32:18 +02:00
|
|
|
canonicalBase: string
|
2019-12-29 20:03:12 +01:00
|
|
|
basePath: string
|
2020-02-01 14:24:22 +01:00
|
|
|
runtimeConfig: string
|
2020-02-12 02:16:42 +01:00
|
|
|
previewProps: string
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
const nextServerlessLoader: loader.Loader = function() {
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
const {
|
|
|
|
distDir,
|
|
|
|
absolutePagePath,
|
|
|
|
page,
|
2019-08-20 18:38:14 +02:00
|
|
|
buildId,
|
2019-05-29 02:32:18 +02:00
|
|
|
canonicalBase,
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
assetPrefix,
|
|
|
|
absoluteAppPath,
|
|
|
|
absoluteDocumentPath,
|
|
|
|
absoluteErrorPath,
|
2019-04-04 23:54:01 +02:00
|
|
|
generateEtags,
|
2019-12-29 20:03:12 +01:00
|
|
|
basePath,
|
2020-02-01 14:24:22 +01:00
|
|
|
runtimeConfig,
|
2020-02-12 02:16:42 +01:00
|
|
|
previewProps,
|
2019-05-29 13:57:26 +02:00
|
|
|
}: ServerlessLoaderQuery =
|
|
|
|
typeof this.query === 'string' ? parse(this.query.substr(1)) : this.query
|
2019-12-29 20:03:12 +01:00
|
|
|
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
const buildManifest = join(distDir, BUILD_MANIFEST).replace(/\\/g, '/')
|
2019-05-29 13:57:26 +02:00
|
|
|
const reactLoadableManifest = join(distDir, REACT_LOADABLE_MANIFEST).replace(
|
|
|
|
/\\/g,
|
|
|
|
'/'
|
|
|
|
)
|
2019-12-23 22:20:17 +01:00
|
|
|
const routesManifest = join(distDir, ROUTES_MANIFEST).replace(/\\/g, '/')
|
|
|
|
|
2020-01-25 00:27:39 +01:00
|
|
|
const escapedBuildId = escapeRegexp(buildId)
|
2019-12-23 22:20:17 +01:00
|
|
|
const pageIsDynamicRoute = isDynamicRoute(page)
|
2019-07-07 23:45:40 +02:00
|
|
|
|
2020-02-12 02:16:42 +01:00
|
|
|
const encodedPreviewProps = devalue(
|
|
|
|
JSON.parse(previewProps) as __ApiPreviewProps
|
|
|
|
)
|
|
|
|
|
2020-02-01 14:24:22 +01:00
|
|
|
const runtimeConfigImports = runtimeConfig
|
|
|
|
? `
|
2020-03-02 18:38:16 +01:00
|
|
|
const { setConfig } = require('next/config')
|
2020-02-01 14:24:22 +01:00
|
|
|
`
|
|
|
|
: ''
|
|
|
|
|
|
|
|
const runtimeConfigSetter = runtimeConfig
|
|
|
|
? `
|
|
|
|
const runtimeConfig = ${runtimeConfig}
|
|
|
|
setConfig(runtimeConfig)
|
|
|
|
`
|
|
|
|
: 'const runtimeConfig = {}'
|
|
|
|
|
2019-12-23 22:20:17 +01:00
|
|
|
const dynamicRouteImports = pageIsDynamicRoute
|
|
|
|
? `
|
2020-02-03 04:33:53 +01:00
|
|
|
const { getRouteMatcher } = require('next/dist/next-server/lib/router/utils/route-matcher');
|
|
|
|
const { getRouteRegex } = require('next/dist/next-server/lib/router/utils/route-regex');
|
2019-12-23 22:20:17 +01:00
|
|
|
`
|
|
|
|
: ''
|
|
|
|
|
|
|
|
const dynamicRouteMatcher = pageIsDynamicRoute
|
|
|
|
? `
|
|
|
|
const dynamicRouteMatcher = getRouteMatcher(getRouteRegex("${page}"))
|
|
|
|
`
|
|
|
|
: ''
|
|
|
|
|
|
|
|
const rewriteImports = `
|
2020-02-03 04:33:53 +01:00
|
|
|
const { rewrites } = require('${routesManifest}')
|
|
|
|
const { pathToRegexp, default: pathMatch } = require('next/dist/next-server/server/lib/path-match')
|
2019-12-23 22:20:17 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
const handleRewrites = `
|
|
|
|
const getCustomRouteMatcher = pathMatch(true)
|
2020-02-26 19:14:34 +01:00
|
|
|
const {prepareDestination} = require('next/dist/next-server/server/router')
|
2019-12-23 22:20:17 +01:00
|
|
|
|
|
|
|
function handleRewrites(parsedUrl) {
|
|
|
|
for (const rewrite of rewrites) {
|
|
|
|
const matcher = getCustomRouteMatcher(rewrite.source)
|
|
|
|
const params = matcher(parsedUrl.pathname)
|
|
|
|
|
|
|
|
if (params) {
|
2020-02-26 19:14:34 +01:00
|
|
|
const { parsedDestination } = prepareDestination(
|
|
|
|
rewrite.destination,
|
2020-04-11 12:57:52 +02:00
|
|
|
params,
|
|
|
|
parsedUrl.query
|
2019-12-23 22:20:17 +01:00
|
|
|
)
|
2020-02-26 19:14:34 +01:00
|
|
|
Object.assign(parsedUrl.query, parsedDestination.query, params)
|
|
|
|
delete parsedDestination.query
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2020-02-26 19:14:34 +01:00
|
|
|
Object.assign(parsedUrl, parsedDestination)
|
2019-12-23 22:20:17 +01:00
|
|
|
|
|
|
|
if (parsedUrl.pathname === '${page}'){
|
|
|
|
break
|
|
|
|
}
|
|
|
|
${
|
|
|
|
pageIsDynamicRoute
|
|
|
|
? `
|
|
|
|
const dynamicParams = dynamicRouteMatcher(parsedUrl.pathname);\
|
|
|
|
if (dynamicParams) {
|
|
|
|
parsedUrl.query = {
|
|
|
|
...parsedUrl.query,
|
|
|
|
...dynamicParams
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
`
|
|
|
|
: ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parsedUrl
|
2019-07-07 23:45:40 +02:00
|
|
|
}
|
2019-12-23 22:20:17 +01:00
|
|
|
`
|
|
|
|
|
|
|
|
if (page.match(API_ROUTE)) {
|
|
|
|
return `
|
2019-11-01 20:13:13 +01:00
|
|
|
import initServer from 'next-plugin-loader?middleware=on-init-server!'
|
|
|
|
import onError from 'next-plugin-loader?middleware=on-error-server!'
|
2020-02-01 14:24:22 +01:00
|
|
|
${runtimeConfigImports}
|
|
|
|
${
|
2020-02-03 04:33:53 +01:00
|
|
|
/*
|
|
|
|
this needs to be called first so its available for any other imports
|
|
|
|
*/
|
2020-02-01 14:24:22 +01:00
|
|
|
runtimeConfigSetter
|
|
|
|
}
|
2020-02-03 04:33:53 +01:00
|
|
|
${dynamicRouteImports}
|
|
|
|
const { parse } = require('url')
|
|
|
|
const { apiResolver } = require('next/dist/next-server/server/api-utils')
|
|
|
|
${rewriteImports}
|
|
|
|
|
|
|
|
${dynamicRouteMatcher}
|
2020-04-02 09:24:32 +02:00
|
|
|
|
2020-02-03 04:33:53 +01:00
|
|
|
${handleRewrites}
|
2019-07-10 21:23:28 +02:00
|
|
|
|
2019-11-01 20:13:13 +01:00
|
|
|
export default async (req, res) => {
|
|
|
|
try {
|
|
|
|
await initServer()
|
2019-12-29 20:03:12 +01:00
|
|
|
|
|
|
|
${
|
|
|
|
basePath
|
|
|
|
? `
|
|
|
|
if(req.url.startsWith('${basePath}')) {
|
|
|
|
req.url = req.url.replace('${basePath}', '')
|
|
|
|
}
|
|
|
|
`
|
|
|
|
: ''
|
|
|
|
}
|
2020-02-26 19:14:34 +01:00
|
|
|
const parsedUrl = handleRewrites(parse(req.url, true))
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2019-11-01 20:13:13 +01:00
|
|
|
const params = ${
|
2019-12-23 22:20:17 +01:00
|
|
|
pageIsDynamicRoute
|
|
|
|
? `dynamicRouteMatcher(parsedUrl.pathname)`
|
2019-11-01 20:13:13 +01:00
|
|
|
: `{}`
|
|
|
|
}
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2019-11-01 20:13:13 +01:00
|
|
|
const resolver = require('${absolutePagePath}')
|
2020-04-14 17:43:46 +02:00
|
|
|
await apiResolver(
|
2019-12-23 22:20:17 +01:00
|
|
|
req,
|
|
|
|
res,
|
|
|
|
Object.assign({}, parsedUrl.query, params ),
|
|
|
|
resolver,
|
2020-02-12 02:16:42 +01:00
|
|
|
${encodedPreviewProps},
|
2019-12-23 22:20:17 +01:00
|
|
|
onError
|
|
|
|
)
|
2019-11-16 03:00:24 +01:00
|
|
|
} catch (err) {
|
2019-11-01 20:13:13 +01:00
|
|
|
console.error(err)
|
2019-11-16 03:00:24 +01:00
|
|
|
await onError(err)
|
2020-04-02 09:24:32 +02:00
|
|
|
|
|
|
|
if (err.code === 'DECODE_FAILED') {
|
|
|
|
res.statusCode = 400
|
|
|
|
res.end('Bad Request')
|
|
|
|
} else {
|
|
|
|
res.statusCode = 500
|
|
|
|
res.end('Internal Server Error')
|
|
|
|
}
|
2019-07-07 23:45:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
} else {
|
|
|
|
return `
|
2019-11-01 20:13:13 +01:00
|
|
|
import initServer from 'next-plugin-loader?middleware=on-init-server!'
|
|
|
|
import onError from 'next-plugin-loader?middleware=on-error-server!'
|
2020-02-01 14:24:22 +01:00
|
|
|
${runtimeConfigImports}
|
|
|
|
${
|
2020-02-03 04:33:53 +01:00
|
|
|
// this needs to be called first so its available for any other imports
|
2020-02-01 14:24:22 +01:00
|
|
|
runtimeConfigSetter
|
|
|
|
}
|
2020-02-03 04:33:53 +01:00
|
|
|
const {parse} = require('url')
|
|
|
|
const {parse: parseQs} = require('querystring')
|
2020-02-22 10:26:39 +01:00
|
|
|
const {renderToHTML} = require('next/dist/next-server/server/render');
|
|
|
|
const { tryGetPreviewData } = require('next/dist/next-server/server/api-utils');
|
2020-02-03 04:33:53 +01:00
|
|
|
const {sendHTML} = require('next/dist/next-server/server/send-html');
|
2020-03-13 10:40:10 +01:00
|
|
|
const {sendPayload} = require('next/dist/next-server/server/send-payload');
|
2020-02-03 04:33:53 +01:00
|
|
|
const buildManifest = require('${buildManifest}');
|
|
|
|
const reactLoadableManifest = require('${reactLoadableManifest}');
|
|
|
|
const Document = require('${absoluteDocumentPath}').default;
|
|
|
|
const Error = require('${absoluteErrorPath}').default;
|
|
|
|
const App = require('${absoluteAppPath}').default;
|
|
|
|
${dynamicRouteImports}
|
|
|
|
${rewriteImports}
|
2020-02-01 14:24:22 +01:00
|
|
|
|
|
|
|
const ComponentInfo = require('${absolutePagePath}')
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2019-06-10 02:16:14 +02:00
|
|
|
const Component = ComponentInfo.default
|
2019-05-22 18:36:53 +02:00
|
|
|
export default Component
|
2019-12-10 20:30:22 +01:00
|
|
|
export const unstable_getStaticParams = ComponentInfo['unstable_getStaticParam' + 's']
|
2020-02-27 18:57:39 +01:00
|
|
|
export const getStaticProps = ComponentInfo['getStaticProp' + 's']
|
|
|
|
export const getStaticPaths = ComponentInfo['getStaticPath' + 's']
|
|
|
|
export const getServerSideProps = ComponentInfo['getServerSideProp' + 's']
|
|
|
|
|
|
|
|
// kept for detecting legacy exports
|
|
|
|
export const unstable_getStaticProps = ComponentInfo['unstable_getStaticProp' + 's']
|
2019-12-10 20:30:22 +01:00
|
|
|
export const unstable_getStaticPaths = ComponentInfo['unstable_getStaticPath' + 's']
|
2020-02-27 18:57:39 +01:00
|
|
|
export const unstable_getServerProps = ComponentInfo['unstable_getServerProp' + 's']
|
2019-11-28 05:23:32 +01:00
|
|
|
|
2019-12-23 22:20:17 +01:00
|
|
|
${dynamicRouteMatcher}
|
|
|
|
${handleRewrites}
|
|
|
|
|
2019-07-01 23:13:52 +02:00
|
|
|
export const config = ComponentInfo['confi' + 'g'] || {}
|
2019-05-22 18:36:53 +02:00
|
|
|
export const _app = App
|
2020-03-09 18:30:44 +01:00
|
|
|
export async function renderReqToHTML(req, res, renderMode, _renderOpts, _params) {
|
|
|
|
const fromExport = renderMode === 'export' || renderMode === true;
|
2019-12-29 20:03:12 +01:00
|
|
|
${
|
|
|
|
basePath
|
|
|
|
? `
|
|
|
|
if(req.url.startsWith('${basePath}')) {
|
|
|
|
req.url = req.url.replace('${basePath}', '')
|
|
|
|
}
|
|
|
|
`
|
|
|
|
: ''
|
|
|
|
}
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
const options = {
|
|
|
|
App,
|
|
|
|
Document,
|
|
|
|
buildManifest,
|
2020-02-27 18:57:39 +01:00
|
|
|
getStaticProps,
|
|
|
|
getServerSideProps,
|
|
|
|
getStaticPaths,
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
reactLoadableManifest,
|
2019-05-29 02:32:18 +02:00
|
|
|
canonicalBase: "${canonicalBase}",
|
2019-08-20 18:38:14 +02:00
|
|
|
buildId: "${buildId}",
|
2019-04-03 23:42:12 +02:00
|
|
|
assetPrefix: "${assetPrefix}",
|
2020-02-01 14:24:22 +01:00
|
|
|
runtimeConfig: runtimeConfig.publicRuntimeConfig || {},
|
2020-02-12 02:16:42 +01:00
|
|
|
previewProps: ${encodedPreviewProps},
|
2020-03-26 13:32:41 +01:00
|
|
|
env: process.env,
|
2020-04-14 09:50:39 +02:00
|
|
|
basePath: "${basePath}",
|
2019-12-14 07:31:48 +01:00
|
|
|
..._renderOpts
|
2019-09-24 10:50:04 +02:00
|
|
|
}
|
2020-01-15 02:22:15 +01:00
|
|
|
let _nextData = false
|
2020-04-02 09:24:32 +02:00
|
|
|
let parsedUrl
|
2019-09-24 10:50:04 +02:00
|
|
|
|
2020-04-02 09:24:32 +02:00
|
|
|
try {
|
|
|
|
parsedUrl = handleRewrites(parse(req.url, true))
|
2020-01-25 05:34:00 +01:00
|
|
|
|
2020-04-02 09:24:32 +02:00
|
|
|
if (parsedUrl.pathname.match(/_next\\/data/)) {
|
|
|
|
_nextData = true
|
|
|
|
parsedUrl.pathname = parsedUrl.pathname
|
|
|
|
.replace(new RegExp('/_next/data/${escapedBuildId}/'), '/')
|
|
|
|
.replace(/\\.json$/, '')
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderOpts = Object.assign(
|
|
|
|
{
|
|
|
|
Component,
|
|
|
|
pageConfig: config,
|
|
|
|
nextExport: fromExport
|
|
|
|
},
|
|
|
|
options,
|
|
|
|
)
|
|
|
|
|
|
|
|
${
|
|
|
|
page === '/_error'
|
|
|
|
? `
|
|
|
|
if (!res.statusCode) {
|
|
|
|
res.statusCode = 404
|
|
|
|
}
|
|
|
|
`
|
|
|
|
: ''
|
|
|
|
}
|
2019-12-23 22:20:17 +01:00
|
|
|
|
2019-05-27 20:20:33 +02:00
|
|
|
${
|
2019-12-23 22:20:17 +01:00
|
|
|
pageIsDynamicRoute
|
2020-02-27 18:57:39 +01:00
|
|
|
? `const params = fromExport && !getStaticProps && !getServerSideProps ? {} : dynamicRouteMatcher(parsedUrl.pathname) || {};`
|
2019-05-27 20:20:33 +02:00
|
|
|
: `const params = {};`
|
|
|
|
}
|
2019-10-15 20:44:50 +02:00
|
|
|
${
|
2019-10-16 20:54:07 +02:00
|
|
|
// Temporary work around: `x-now-route-matches` is a platform header
|
2019-10-15 20:44:50 +02:00
|
|
|
// _only_ set for `Prerender` requests. We should move this logic
|
|
|
|
// into our builder to ensure we're decoupled. However, this entails
|
|
|
|
// removing reliance on `req.url` and using `req.query` instead
|
|
|
|
// (which is needed for "custom routes" anyway).
|
2019-12-23 22:20:17 +01:00
|
|
|
pageIsDynamicRoute
|
2019-10-16 20:54:07 +02:00
|
|
|
? `const nowParams = req.headers && req.headers["x-now-route-matches"]
|
2019-10-15 22:30:10 +02:00
|
|
|
? getRouteMatcher(
|
|
|
|
(function() {
|
|
|
|
const { re, groups } = getRouteRegex("${page}");
|
|
|
|
return {
|
|
|
|
re: {
|
|
|
|
// Simulate a RegExp match from the \`req.url\` input
|
|
|
|
exec: str => {
|
|
|
|
const obj = parseQs(str);
|
|
|
|
return Object.keys(obj).reduce(
|
|
|
|
(prev, key) =>
|
|
|
|
Object.assign(prev, {
|
2020-02-24 19:06:11 +01:00
|
|
|
[key]: obj[key]
|
2019-10-15 22:30:10 +02:00
|
|
|
}),
|
|
|
|
{}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
groups
|
|
|
|
};
|
|
|
|
})()
|
2019-10-16 20:54:07 +02:00
|
|
|
)(req.headers["x-now-route-matches"])
|
2019-10-15 22:30:10 +02:00
|
|
|
: null;
|
|
|
|
`
|
2019-10-15 20:44:50 +02:00
|
|
|
: `const nowParams = null;`
|
|
|
|
}
|
2020-01-27 23:50:59 +01:00
|
|
|
// make sure to set renderOpts to the correct params e.g. _params
|
|
|
|
// if provided from worker or params if we're parsing them here
|
|
|
|
renderOpts.params = _params || params
|
|
|
|
|
2020-02-07 14:09:06 +01:00
|
|
|
const isFallback = parsedUrl.query.__nextFallback
|
|
|
|
|
2020-02-22 10:26:39 +01:00
|
|
|
const previewData = tryGetPreviewData(req, res, options.previewProps)
|
|
|
|
const isPreviewMode = previewData !== false
|
|
|
|
|
2020-03-20 09:46:52 +01:00
|
|
|
let result = await renderToHTML(req, res, "${page}", Object.assign({}, getStaticProps ? { ...(parsedUrl.query.amp ? { amp: '1' } : {}) } : parsedUrl.query, nowParams ? nowParams : params, _params, isFallback ? { __nextFallback: 'true' } : {}), renderOpts)
|
2019-10-16 23:24:28 +02:00
|
|
|
|
2020-03-13 10:40:10 +01:00
|
|
|
if (!renderMode) {
|
|
|
|
if (_nextData || getStaticProps || getServerSideProps) {
|
|
|
|
sendPayload(res, _nextData ? JSON.stringify(renderOpts.pageData) : result, _nextData ? 'json' : 'html', {
|
|
|
|
private: isPreviewMode,
|
|
|
|
stateful: !!getServerSideProps,
|
|
|
|
revalidate: renderOpts.revalidate,
|
|
|
|
})
|
|
|
|
return null
|
|
|
|
}
|
2020-02-22 10:26:39 +01:00
|
|
|
} else if (isPreviewMode) {
|
|
|
|
res.setHeader(
|
|
|
|
'Cache-Control',
|
|
|
|
'private, no-cache, no-store, max-age=0, must-revalidate'
|
|
|
|
)
|
2019-10-16 23:24:28 +02:00
|
|
|
}
|
2019-05-22 18:36:53 +02:00
|
|
|
|
2020-03-09 18:30:44 +01:00
|
|
|
if (renderMode) return { html: result, renderOpts }
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
return result
|
|
|
|
} catch (err) {
|
2020-04-02 09:24:32 +02:00
|
|
|
if (!parsedUrl) {
|
|
|
|
parsedUrl = parse(req.url, true)
|
|
|
|
}
|
|
|
|
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
res.statusCode = 404
|
2020-04-02 09:24:32 +02:00
|
|
|
} else if (err.code === 'DECODE_FAILED') {
|
|
|
|
res.statusCode = 400
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
} else {
|
|
|
|
console.error(err)
|
|
|
|
res.statusCode = 500
|
|
|
|
}
|
2020-04-02 09:24:32 +02:00
|
|
|
|
|
|
|
const result = await renderToHTML(req, res, "/_error", parsedUrl.query, Object.assign({}, options, {
|
|
|
|
getStaticProps: undefined,
|
|
|
|
getStaticPaths: undefined,
|
|
|
|
getServerSideProps: undefined,
|
|
|
|
Component: Error,
|
|
|
|
err: res.statusCode === 404 ? undefined : err
|
|
|
|
}))
|
|
|
|
return result
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
}
|
|
|
|
}
|
2019-01-02 14:59:28 +01:00
|
|
|
export async function render (req, res) {
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
try {
|
2019-11-01 20:13:13 +01:00
|
|
|
await initServer()
|
2019-01-02 14:59:28 +01:00
|
|
|
const html = await renderReqToHTML(req, res)
|
2019-10-16 23:24:28 +02:00
|
|
|
if (html) {
|
|
|
|
sendHTML(req, res, html, {generateEtags: ${generateEtags}})
|
|
|
|
}
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
} catch(err) {
|
2019-11-01 20:13:13 +01:00
|
|
|
await onError(err)
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
console.error(err)
|
|
|
|
res.statusCode = 500
|
|
|
|
res.end('Internal Server Error')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
2019-07-07 23:45:40 +02:00
|
|
|
}
|
Serverless Next.js (#5927)
**This does not change existing behavior.**
building to serverless is completely opt-in.
- Implements `target: 'serverless'` in `next.config.js`
- Removes `next build --lambdas` (was only available on next@canary so far)
This implements the concept of build targets. Currently there will be 2 build targets:
- server (This is the target that already existed / the default, no changes here)
- serverless (New target aimed at compiling pages to serverless handlers)
The serverless target will output a single file per `page` in the `pages` directory:
- `pages/index.js` => `.next/serverless/index.js`
- `pages/about.js` => `.next/serverless/about.js`
So what is inside `.next/serverless/about.js`? All the code needed to render that specific page. It has the Node.js `http.Server` request handler function signature:
```ts
(req: http.IncomingMessage, res: http.ServerResponse) => void
```
So how do you use it? Generally you **don't** want to use the below example, but for illustration purposes it's shown how the handler is called using a plain `http.Server`:
```js
const http = require('http')
// Note that `.default` is needed because the exported module is an esmodule
const handler = require('./.next/serverless/about.js').default
const server = new http.Server((req, res) => handler(req, res))
server.listen(3000, () => console.log('Listening on http://localhost:3000'))
```
Generally you'll upload this handler function to an external service like [Now v2](https://zeit.co/now-2), the `@now/next` builder will be updated to reflect these changes. This means that it'll be no longer neccesary for `@now/next` to do some of the guesswork in creating smaller handler functions. As Next.js will output the smallest possible serverless handler function automatically.
The function has 0 dependencies so no node_modules are required to run it, and is generally very small. 45Kb zipped is the baseline, but I'm sure we can make it even smaller in the future.
One important thing to note is that the function won't try to load `next.config.js`, so `publicRuntimeConfig` / `serverRuntimeConfig` are not supported. Reasons are outlined here: #5846
So to summarize:
- every page becomes a serverless function
- the serverless function has 0 dependencies (they're all inlined)
- "just" uses the `req` and `res` coming from Node.js
- opt-in using `target: 'serverless'` in `next.config.js`
- Does not load next.config.js when executing the function
TODO:
- [x] Compile next/dynamic / `import()` into the function file, so that no extra files have to be uploaded.
- [x] Setting `assetPrefix` at build time for serverless target
- [x] Support custom /_app
- [x] Support custom /_document
- [x] Support custom /_error
- [x] Add `next.config.js` property for `target`
Need discussion:
- [ ] Since the serverless target won't support `publicRuntimeConfig` / `serverRuntimeConfig` as they're runtime values. I think we should support build-time env var replacement with webpack.DefinePlugin or similar.
- [ ] Serving static files with the correct cache-control, as there is no static file serving in the serverless target
2018-12-28 11:39:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export default nextServerlessLoader
|