2019-08-20 07:56:55 +02:00
|
|
|
import chalk from 'chalk'
|
2017-12-14 03:49:26 +01:00
|
|
|
import findUp from 'find-up'
|
2019-08-06 00:26:20 +02:00
|
|
|
import os from 'os'
|
2019-10-30 13:18:33 +01:00
|
|
|
import { basename, extname } from 'path'
|
2019-08-06 00:26:20 +02:00
|
|
|
|
2019-04-24 16:47:50 +02:00
|
|
|
import { CONFIG_FILE } from '../lib/constants'
|
2019-07-16 13:07:08 +02:00
|
|
|
import { execOnce } from '../lib/utils'
|
2018-06-04 11:38:46 +02:00
|
|
|
|
2019-08-06 00:26:20 +02:00
|
|
|
const targets = ['server', 'serverless', 'experimental-serverless-trace']
|
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 defaultConfig: { [key: string]: any } = {
|
2019-02-15 17:49:40 +01:00
|
|
|
env: [],
|
2016-12-19 16:27:47 +01:00
|
|
|
webpack: null,
|
2017-05-14 01:44:21 +02:00
|
|
|
webpackDevMiddleware: null,
|
2017-04-18 06:18:43 +02:00
|
|
|
distDir: '.next',
|
2017-05-27 17:40:15 +02:00
|
|
|
assetPrefix: '',
|
2017-05-31 10:06:07 +02:00
|
|
|
configOrigin: 'default',
|
2018-02-14 16:20:41 +01:00
|
|
|
useFileSystemPublicRoutes: true,
|
2018-11-02 23:19:48 +01:00
|
|
|
generateBuildId: () => null,
|
2018-03-13 14:18:59 +01:00
|
|
|
generateEtags: true,
|
2019-04-23 11:54:08 +02:00
|
|
|
pageExtensions: ['tsx', 'ts', 'jsx', 'js'],
|
2019-08-30 13:26:30 +02:00
|
|
|
target: 'server',
|
2019-04-15 09:48:14 +02:00
|
|
|
poweredByHeader: true,
|
2019-07-29 20:35:09 +02:00
|
|
|
compress: true,
|
2019-09-15 23:18:11 +02:00
|
|
|
devIndicators: {
|
|
|
|
buildActivity: true,
|
|
|
|
autoPrerender: true,
|
|
|
|
},
|
2019-01-01 01:07:10 +01:00
|
|
|
onDemandEntries: {
|
|
|
|
maxInactiveAge: 60 * 1000,
|
2019-04-24 16:47:50 +02:00
|
|
|
pagesBufferLength: 2,
|
2019-02-14 16:22:57 +01:00
|
|
|
},
|
2019-05-29 02:32:18 +02:00
|
|
|
amp: {
|
|
|
|
canonicalBase: '',
|
|
|
|
},
|
2019-07-03 19:25:44 +02:00
|
|
|
exportTrailingSlash: false,
|
2019-02-14 16:22:57 +01:00
|
|
|
experimental: {
|
2019-09-11 20:10:51 +02:00
|
|
|
ampBindInitData: false,
|
2019-03-11 17:42:54 +01:00
|
|
|
cpus: Math.max(
|
|
|
|
1,
|
|
|
|
(Number(process.env.CIRCLE_NODE_TOTAL) ||
|
2019-05-29 13:57:26 +02:00
|
|
|
(os.cpus() || { length: 1 }).length) - 1
|
2019-03-11 18:53:24 +01:00
|
|
|
),
|
2019-09-17 22:05:20 +02:00
|
|
|
css: false,
|
2019-06-24 17:31:27 +02:00
|
|
|
documentMiddleware: false,
|
2019-08-08 19:14:33 +02:00
|
|
|
granularChunks: false,
|
2019-07-25 04:16:32 +02:00
|
|
|
modern: false,
|
2019-09-11 20:10:51 +02:00
|
|
|
profiling: false,
|
|
|
|
publicDirectory: false,
|
2019-09-24 10:50:04 +02:00
|
|
|
sprFlushToDisk: true,
|
2019-10-26 19:27:22 +02:00
|
|
|
deferScripts: false,
|
2019-10-29 01:01:25 +01:00
|
|
|
workerThreads: false,
|
2019-08-20 21:19:45 +02:00
|
|
|
},
|
|
|
|
future: {
|
|
|
|
excludeDefaultMomentLocales: false,
|
2019-04-24 16:47:50 +02:00
|
|
|
},
|
2019-07-10 16:43:04 +02:00
|
|
|
serverRuntimeConfig: {},
|
|
|
|
publicRuntimeConfig: {},
|
2019-10-26 00:20:38 +02:00
|
|
|
reactStrictMode: false,
|
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-07-16 13:07:08 +02:00
|
|
|
const experimentalWarning = execOnce(() => {
|
|
|
|
console.warn(
|
2019-08-20 07:56:55 +02:00
|
|
|
chalk.yellow.bold('Warning: ') +
|
|
|
|
chalk.bold('You have enabled experimental feature(s).')
|
2019-07-16 13:07:08 +02:00
|
|
|
)
|
2019-08-20 07:56:55 +02:00
|
|
|
console.warn(
|
|
|
|
`Experimental features are not covered by semver, and may cause unexpected or broken application behavior. ` +
|
|
|
|
`Use them at your own risk.`
|
|
|
|
)
|
|
|
|
console.warn()
|
2019-07-16 13:07:08 +02:00
|
|
|
})
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
function assignDefaults(userConfig: { [key: string]: any }) {
|
2019-04-24 16:47:50 +02:00
|
|
|
Object.keys(userConfig).forEach((key: string) => {
|
2019-08-12 04:26:37 +02:00
|
|
|
if (
|
|
|
|
key === 'experimental' &&
|
|
|
|
userConfig[key] &&
|
|
|
|
userConfig[key] !== defaultConfig[key]
|
|
|
|
) {
|
2019-07-16 13:07:08 +02:00
|
|
|
experimentalWarning()
|
|
|
|
}
|
|
|
|
|
2019-10-06 13:44:03 +02:00
|
|
|
if (key === 'distDir' && userConfig[key] === 'public') {
|
|
|
|
throw new Error(
|
|
|
|
`The 'public' directory is reserved in Next.js and can not be set as the 'distDir'. https://err.sh/zeit/next.js/can-not-output-to-public`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:00:03 +01:00
|
|
|
const maybeObject = userConfig[key]
|
2019-05-29 13:57:26 +02:00
|
|
|
if (!!maybeObject && maybeObject.constructor === Object) {
|
2019-02-20 14:00:03 +01:00
|
|
|
userConfig[key] = {
|
|
|
|
...(defaultConfig[key] || {}),
|
2019-04-24 16:47:50 +02:00
|
|
|
...userConfig[key],
|
2019-02-20 14:00:03 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return { ...defaultConfig, ...userConfig }
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:47:50 +02:00
|
|
|
function normalizeConfig(phase: string, config: any) {
|
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 (typeof config === 'function') {
|
2019-02-28 14:39:51 +01:00
|
|
|
config = config(phase, { defaultConfig })
|
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-02-28 14:39:51 +01:00
|
|
|
if (typeof config.then === 'function') {
|
|
|
|
throw new Error(
|
2019-08-24 00:00:57 +02:00
|
|
|
'> Promise returned in next config. https://err.sh/zeit/next.js/promise-in-next-config'
|
2019-02-28 14:39:51 +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
|
|
|
return config
|
2016-12-17 19:38:11 +01:00
|
|
|
}
|
2016-10-28 16:38:24 +02:00
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
export default function loadConfig(
|
|
|
|
phase: string,
|
|
|
|
dir: string,
|
2019-10-04 17:26:44 +02:00
|
|
|
customConfig?: object | null
|
2019-05-29 13:57:26 +02:00
|
|
|
) {
|
2018-06-04 11:38:46 +02:00
|
|
|
if (customConfig) {
|
2019-02-20 14:00:03 +01:00
|
|
|
return assignDefaults({ configOrigin: 'server', ...customConfig })
|
2017-05-31 10:06:07 +02:00
|
|
|
}
|
2018-11-21 16:04:37 +01:00
|
|
|
const path = findUp.sync(CONFIG_FILE, {
|
2019-04-24 16:47:50 +02:00
|
|
|
cwd: dir,
|
2017-12-14 03:49:26 +01:00
|
|
|
})
|
2016-10-28 16:38:24 +02:00
|
|
|
|
2018-06-04 11:38:46 +02:00
|
|
|
// If config file was found
|
2017-12-14 03:49:26 +01:00
|
|
|
if (path && path.length) {
|
2016-12-17 19:38:11 +01:00
|
|
|
const userConfigModule = require(path)
|
2019-05-29 13:57:26 +02:00
|
|
|
const userConfig = normalizeConfig(
|
|
|
|
phase,
|
|
|
|
userConfigModule.default || userConfigModule
|
|
|
|
)
|
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 (userConfig.target && !targets.includes(userConfig.target)) {
|
2019-05-29 13:57:26 +02:00
|
|
|
throw new Error(
|
|
|
|
`Specified target is invalid. Provided: "${
|
|
|
|
userConfig.target
|
|
|
|
}" should be one of ${targets.join(', ')}`
|
|
|
|
)
|
2018-02-23 14:42:06 +01:00
|
|
|
}
|
2019-05-29 02:32:18 +02:00
|
|
|
|
|
|
|
if (userConfig.amp && userConfig.amp.canonicalBase) {
|
2019-05-29 13:57:26 +02:00
|
|
|
const { canonicalBase } = userConfig.amp || ({} as any)
|
2019-05-29 02:32:18 +02:00
|
|
|
userConfig.amp = userConfig.amp || {}
|
2019-05-29 13:57:26 +02:00
|
|
|
userConfig.amp.canonicalBase =
|
|
|
|
(canonicalBase.endsWith('/')
|
|
|
|
? canonicalBase.slice(0, -1)
|
|
|
|
: canonicalBase) || ''
|
2019-05-29 02:32:18 +02:00
|
|
|
}
|
|
|
|
|
2019-07-13 00:16:56 +02:00
|
|
|
if (
|
2019-08-06 00:26:20 +02:00
|
|
|
userConfig.target &&
|
|
|
|
userConfig.target !== 'server' &&
|
2019-10-10 23:56:38 +02:00
|
|
|
((userConfig.publicRuntimeConfig &&
|
|
|
|
Object.keys(userConfig.publicRuntimeConfig).length !== 0) ||
|
|
|
|
(userConfig.serverRuntimeConfig &&
|
|
|
|
Object.keys(userConfig.serverRuntimeConfig).length !== 0))
|
2019-07-13 00:16:56 +02:00
|
|
|
) {
|
2019-08-06 00:26:20 +02:00
|
|
|
// TODO: change error message tone to "Only compatible with [fat] server mode"
|
2019-07-10 16:43:04 +02:00
|
|
|
throw new Error(
|
2019-10-10 23:56:38 +02:00
|
|
|
'Cannot use publicRuntimeConfig or serverRuntimeConfig with target=serverless https://err.sh/zeit/next.js/serverless-publicRuntimeConfig'
|
2019-07-10 16:43:04 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-02-20 14:00:03 +01:00
|
|
|
return assignDefaults({ configOrigin: CONFIG_FILE, ...userConfig })
|
2019-10-30 13:18:33 +01:00
|
|
|
} else {
|
|
|
|
const configBaseName = basename(CONFIG_FILE, extname(CONFIG_FILE))
|
|
|
|
const nonJsPath = findUp.sync(
|
|
|
|
[
|
|
|
|
`${configBaseName}.jsx`,
|
|
|
|
`${configBaseName}.ts`,
|
|
|
|
`${configBaseName}.tsx`,
|
|
|
|
`${configBaseName}.json`,
|
|
|
|
],
|
|
|
|
{ cwd: dir }
|
|
|
|
)
|
|
|
|
if (nonJsPath && nonJsPath.length) {
|
|
|
|
throw new Error(
|
|
|
|
`Configuring Next.js via '${basename(
|
|
|
|
nonJsPath
|
|
|
|
)}' is not supported. Please replace the file with 'next.config.js'.`
|
|
|
|
)
|
|
|
|
}
|
2018-06-04 11:38:46 +02:00
|
|
|
}
|
2017-05-31 10:06:07 +02:00
|
|
|
|
2018-06-04 11:38:46 +02:00
|
|
|
return defaultConfig
|
2016-10-28 16:38:24 +02:00
|
|
|
}
|
2019-08-06 00:26:20 +02:00
|
|
|
|
|
|
|
export function isTargetLikeServerless(target: string) {
|
|
|
|
const isServerless = target === 'serverless'
|
|
|
|
const isServerlessTrace = target === 'experimental-serverless-trace'
|
|
|
|
return isServerless || isServerlessTrace
|
|
|
|
}
|