2020-11-06 03:33:14 +01:00
import chalk from 'chalk'
2020-03-29 18:21:53 +02:00
import findUp from 'next/dist/compiled/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'
2020-12-04 11:14:55 +01:00
import { execOnce } from '../lib/utils'
2020-06-29 20:50:32 +02:00
import * as Log from '../../build/output/log'
2019-04-24 16:47:50 +02:00
import { CONFIG_FILE } from '../lib/constants'
2020-12-04 11:14:55 +01:00
import { Header , Rewrite , Redirect } from '../../lib/load-custom-routes'
2020-11-12 20:24:08 +01:00
import { ImageConfig , imageConfigDefault , VALID_LOADERS } from './image-config'
2018-06-04 11:38:46 +02:00
2019-08-06 00:26:20 +02:00
const targets = [ 'server' , 'serverless' , 'experimental-serverless-trace' ]
2019-10-30 14:39:58 +01:00
const reactModes = [ 'legacy' , 'blocking' , 'concurrent' ]
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
2020-12-30 07:44:07 +01:00
export type DomainLocales = Array < {
http? : true
domain : string
locales? : string [ ]
defaultLocale : string
} >
2020-12-04 11:14:55 +01:00
export type NextConfig = { [ key : string ] : any } & {
2020-12-22 18:12:53 +01:00
i18n ? : {
2020-12-04 11:14:55 +01:00
locales : string [ ]
defaultLocale : string
2020-12-30 07:44:07 +01:00
domains? : DomainLocales
2020-12-04 11:14:55 +01:00
localeDetection? : false
} | null
headers ? : ( ) = > Promise < Header [ ] >
rewrites ? : ( ) = > Promise < Rewrite [ ] >
redirects ? : ( ) = > Promise < Redirect [ ] >
2020-12-22 18:12:53 +01:00
trailingSlash? : boolean
2021-01-11 18:19:56 +01:00
future : {
strictPostcssConfiguration : boolean
excludeDefaultMomentLocales : boolean
2021-01-14 02:59:08 +01:00
webpack5 : boolean
2021-01-11 18:19:56 +01:00
}
2020-12-04 11:14:55 +01:00
}
const defaultConfig : NextConfig = {
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 ,
2020-10-27 04:36:31 +01:00
analyticsId : process.env.VERCEL_ANALYTICS_ID || '' ,
2020-11-12 20:24:08 +01:00
images : imageConfigDefault ,
2019-09-15 23:18:11 +02:00
devIndicators : {
buildActivity : 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 : '' ,
} ,
2020-06-18 12:10:20 +02:00
basePath : '' ,
2020-05-11 04:11:48 +02:00
sassOptions : { } ,
2020-07-20 18:16:59 +02:00
trailingSlash : false ,
2020-12-04 11:14:55 +01:00
i18n : null ,
2020-12-17 16:04:17 +01:00
productionBrowserSourceMaps : false ,
2019-02-14 16:22:57 +01:00
experimental : {
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-11-01 20:13:13 +01:00
plugins : false ,
2019-09-11 20:10:51 +02:00
profiling : false ,
2019-09-24 10:50:04 +02:00
sprFlushToDisk : true ,
2019-10-30 14:39:58 +01:00
reactMode : 'legacy' ,
2019-10-29 01:01:25 +01:00
workerThreads : false ,
2020-05-09 17:27:35 +02:00
pageEnv : false ,
2020-12-21 20:26:00 +01:00
optimizeFonts : false ,
2020-08-05 19:49:44 +02:00
optimizeImages : false ,
2020-12-01 19:02:07 +01:00
optimizeCss : false ,
2020-07-06 16:27:45 +02:00
scrollRestoration : false ,
2020-12-01 19:10:16 +01:00
scriptLoader : false ,
2019-08-20 21:19:45 +02:00
} ,
future : {
2021-01-11 18:19:56 +01:00
strictPostcssConfiguration : false ,
2019-08-20 21:19:45 +02:00
excludeDefaultMomentLocales : false ,
2021-01-14 02:59:08 +01:00
webpack5 : 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 ( ( ) = > {
2020-04-29 10:49:28 +02:00
Log . warn ( chalk . bold ( 'You have enabled experimental feature(s).' ) )
Log . warn (
2019-08-20 07:56:55 +02:00
` 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 } ) {
2020-08-03 16:03:11 +02:00
if ( typeof userConfig . exportTrailingSlash !== 'undefined' ) {
console . warn (
chalk . yellow . bold ( 'Warning: ' ) +
'The "exportTrailingSlash" option has been renamed to "trailingSlash". Please update your next.config.js.'
)
if ( typeof userConfig . trailingSlash === 'undefined' ) {
userConfig . trailingSlash = userConfig . exportTrailingSlash
}
delete userConfig . exportTrailingSlash
}
2020-05-22 18:46:36 +02:00
const config = Object . keys ( userConfig ) . reduce < { [ key : string ] : any } > (
2020-06-01 23:00:22 +02:00
( currentConfig , key ) = > {
2020-05-22 18:46:36 +02:00
const value = userConfig [ key ]
2019-07-16 13:07:08 +02:00
2020-05-22 18:46:36 +02:00
if ( value === undefined || value === null ) {
2020-06-01 23:00:22 +02:00
return currentConfig
2020-01-20 21:35:29 +01:00
}
2020-05-22 18:46:36 +02:00
if ( key === 'experimental' && value && value !== defaultConfig [ key ] ) {
experimentalWarning ( )
2020-01-20 21:35:29 +01:00
}
2019-10-06 13:44:03 +02:00
2020-05-22 18:46:36 +02:00
if ( key === 'distDir' ) {
if ( typeof value !== 'string' ) {
throw new Error (
` Specified distDir is not a string, found type " ${ typeof value } " `
)
}
const userDistDir = value . trim ( )
2020-01-20 23:19:33 +01:00
2020-05-22 18:46:36 +02:00
// don't allow public as the distDir as this is a reserved folder for
// public files
if ( userDistDir === 'public' ) {
throw new Error (
2020-05-27 23:51:11 +02:00
` The 'public' directory is reserved in Next.js and can not be set as the 'distDir'. https://err.sh/vercel/next.js/can-not-output-to-public `
2020-05-22 18:46:36 +02:00
)
}
// make sure distDir isn't an empty string as it can result in the provided
// directory being deleted in development mode
if ( userDistDir . length === 0 ) {
throw new Error (
` Invalid distDir provided, distDir can not be an empty string. Please remove this config or set it to undefined `
)
}
2020-01-20 23:19:33 +01:00
}
2020-05-22 18:46:36 +02:00
if ( key === 'pageExtensions' ) {
if ( ! Array . isArray ( value ) ) {
throw new Error (
` Specified pageExtensions is not an array of strings, found " ${ value } ". Please update this config or remove it. `
)
}
2020-01-20 23:19:33 +01:00
2020-05-22 18:46:36 +02:00
if ( ! value . length ) {
2020-01-20 23:19:33 +01:00
throw new Error (
2020-05-22 18:46:36 +02:00
` Specified pageExtensions is an empty array. Please update it with the relevant extensions or remove it. `
2020-01-20 23:19:33 +01:00
)
}
2020-05-22 18:46:36 +02:00
value . forEach ( ( ext ) = > {
if ( typeof ext !== 'string' ) {
throw new Error (
` Specified pageExtensions is not an array of strings, found " ${ ext } " of type " ${ typeof ext } ". Please update this config or remove it. `
)
}
} )
2019-02-20 14:00:03 +01:00
}
2020-05-22 18:46:36 +02:00
if ( ! ! value && value . constructor === Object ) {
2020-06-01 23:00:22 +02:00
currentConfig [ key ] = {
2020-05-22 18:46:36 +02:00
. . . defaultConfig [ key ] ,
. . . Object . keys ( value ) . reduce < any > ( ( c , k ) = > {
const v = value [ k ]
if ( v !== undefined && v !== null ) {
c [ k ] = v
}
return c
} , { } ) ,
}
} else {
2020-06-01 23:00:22 +02:00
currentConfig [ key ] = value
2020-05-22 18:46:36 +02:00
}
2020-06-01 23:00:22 +02:00
return currentConfig
2020-05-22 18:46:36 +02:00
} ,
{ }
)
const result = { . . . defaultConfig , . . . config }
2019-12-16 16:07:03 +01:00
if ( typeof result . assetPrefix !== 'string' ) {
throw new Error (
2020-05-27 23:51:11 +02:00
` Specified assetPrefix is not a string, found type " ${ typeof result . assetPrefix } " https://err.sh/vercel/next.js/invalid-assetprefix `
2019-12-16 16:07:03 +01:00
)
}
2020-09-25 04:29:13 +02:00
if ( typeof result . basePath !== 'string' ) {
throw new Error (
` Specified basePath is not a string, found type " ${ typeof result . basePath } " `
)
}
if ( result . basePath !== '' ) {
if ( result . basePath === '/' ) {
2019-12-29 20:03:12 +01:00
throw new Error (
2020-09-25 04:29:13 +02:00
` Specified basePath /. basePath has to be either an empty string or a path prefix" `
2019-12-29 20:03:12 +01:00
)
}
2020-09-25 04:29:13 +02:00
if ( ! result . basePath . startsWith ( '/' ) ) {
throw new Error (
` Specified basePath has to start with a /, found " ${ result . basePath } " `
)
}
2019-12-29 20:03:12 +01:00
2020-09-25 04:29:13 +02:00
if ( result . basePath !== '/' ) {
if ( result . basePath . endsWith ( '/' ) ) {
2019-12-29 20:03:12 +01:00
throw new Error (
2020-09-25 04:29:13 +02:00
` Specified basePath should not end with /, found " ${ result . basePath } " `
2019-12-29 20:03:12 +01:00
)
}
2020-09-25 04:29:13 +02:00
if ( result . assetPrefix === '' ) {
result . assetPrefix = result . basePath
}
2020-08-08 15:15:00 +02:00
2020-09-25 04:29:13 +02:00
if ( result . amp . canonicalBase === '' ) {
result . amp . canonicalBase = result . basePath
2019-12-29 20:03:12 +01:00
}
}
2019-12-10 21:21:52 +01:00
}
2020-09-25 04:29:13 +02:00
2020-10-16 13:10:01 +02:00
if ( result ? . images ) {
2020-11-12 20:24:08 +01:00
const images : Partial < ImageConfig > = result . images
2020-10-20 18:43:24 +02:00
2020-10-16 13:10:01 +02:00
if ( typeof images !== 'object' ) {
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified images should be an object received ${ typeof images } . \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-16 13:10:01 +02:00
)
}
2020-10-27 15:29:52 +01:00
2020-10-16 13:10:01 +02:00
if ( images . domains ) {
if ( ! Array . isArray ( images . domains ) ) {
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified images.domains should be an Array received ${ typeof images . domains } . \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-16 13:10:01 +02:00
)
}
2020-10-26 16:42:44 +01:00
if ( images . domains . length > 50 ) {
throw new Error (
2020-11-20 14:04:18 +01:00
` Specified images.domains exceeds length of 50, received length ( ${ images . domains . length } ), please reduce the length of the array to continue. \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-26 16:42:44 +01:00
)
}
2020-10-16 13:10:01 +02:00
const invalid = images . domains . filter (
( d : unknown ) = > typeof d !== 'string'
)
if ( invalid . length > 0 ) {
throw new Error (
` Specified images.domains should be an Array of strings received invalid values ( ${ invalid . join (
', '
2020-11-02 03:02:28 +01:00
) } ) . \ nSee more info here : https : //err.sh/next.js/invalid-images-config`
2020-10-16 13:10:01 +02:00
)
}
}
2020-10-26 21:07:52 +01:00
if ( images . deviceSizes ) {
const { deviceSizes } = images
if ( ! Array . isArray ( deviceSizes ) ) {
2020-10-16 13:10:01 +02:00
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified images.deviceSizes should be an Array received ${ typeof deviceSizes } . \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-16 13:10:01 +02:00
)
}
2020-10-26 16:42:44 +01:00
2020-10-26 21:07:52 +01:00
if ( deviceSizes . length > 25 ) {
2020-10-26 16:42:44 +01:00
throw new Error (
2020-11-20 14:04:18 +01:00
` Specified images.deviceSizes exceeds length of 25, received length ( ${ deviceSizes . length } ), please reduce the length of the array to continue. \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-26 16:42:44 +01:00
)
}
2020-10-26 21:07:52 +01:00
const invalid = deviceSizes . filter ( ( d : unknown ) = > {
2020-10-26 16:42:44 +01:00
return typeof d !== 'number' || d < 1 || d > 10000
} )
2020-10-16 13:10:01 +02:00
if ( invalid . length > 0 ) {
throw new Error (
2020-10-26 21:07:52 +01:00
` Specified images.deviceSizes should be an Array of numbers that are between 1 and 10000, received invalid values ( ${ invalid . join (
', '
2020-11-02 03:02:28 +01:00
) } ) . \ nSee more info here : https : //err.sh/next.js/invalid-images-config`
2020-10-26 21:07:52 +01:00
)
}
}
2020-10-27 14:19:23 +01:00
if ( images . imageSizes ) {
const { imageSizes } = images
if ( ! Array . isArray ( imageSizes ) ) {
2020-10-26 21:07:52 +01:00
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified images.imageSizes should be an Array received ${ typeof imageSizes } . \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-26 21:07:52 +01:00
)
}
2020-10-27 14:19:23 +01:00
if ( imageSizes . length > 25 ) {
2020-10-26 21:07:52 +01:00
throw new Error (
2020-11-20 14:04:18 +01:00
` Specified images.imageSizes exceeds length of 25, received length ( ${ imageSizes . length } ), please reduce the length of the array to continue. \ nSee more info here: https://err.sh/next.js/invalid-images-config `
2020-10-26 21:07:52 +01:00
)
}
2020-10-27 14:19:23 +01:00
const invalid = imageSizes . filter ( ( d : unknown ) = > {
2020-10-26 21:07:52 +01:00
return typeof d !== 'number' || d < 1 || d > 10000
} )
if ( invalid . length > 0 ) {
throw new Error (
2020-10-27 14:19:23 +01:00
` Specified images.imageSizes should be an Array of numbers that are between 1 and 10000, received invalid values ( ${ invalid . join (
2020-10-16 13:10:01 +02:00
', '
2020-11-02 03:02:28 +01:00
) } ) . \ nSee more info here : https : //err.sh/next.js/invalid-images-config`
2020-10-16 13:10:01 +02:00
)
}
}
2020-10-27 15:29:52 +01:00
2020-11-12 20:24:08 +01:00
if ( ! images . loader ) {
images . loader = 'default'
}
if ( ! VALID_LOADERS . includes ( images . loader ) ) {
throw new Error (
` Specified images.loader should be one of ( ${ VALID_LOADERS . join (
', '
) } ) , received invalid value ( $ {
images . loader
} ) . \ nSee more info here : https : //err.sh/next.js/invalid-images-config`
)
}
2020-10-27 15:29:52 +01:00
// Append trailing slash for non-default loaders
if ( images . path ) {
2020-11-12 20:24:08 +01:00
if (
images . loader !== 'default' &&
images . path [ images . path . length - 1 ] !== '/'
) {
2020-10-27 15:29:52 +01:00
images . path += '/'
}
}
2020-12-30 02:57:08 +01:00
if ( images . path === imageConfigDefault . path && result . basePath ) {
images . path = ` ${ result . basePath } ${ images . path } `
}
2020-10-16 13:10:01 +02:00
}
2020-10-27 16:30:34 +01:00
if ( result . i18n ) {
const { i18n } = result
2020-10-07 23:11:01 +02:00
const i18nType = typeof i18n
if ( i18nType !== 'object' ) {
2020-10-29 04:48:24 +01:00
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified i18n should be an object received ${ i18nType } . \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-29 04:48:24 +01:00
)
2020-10-07 23:11:01 +02:00
}
if ( ! Array . isArray ( i18n . locales ) ) {
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified i18n.locales should be an Array received ${ typeof i18n . locales } . \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-07 23:11:01 +02:00
)
}
const defaultLocaleType = typeof i18n . defaultLocale
if ( ! i18n . defaultLocale || defaultLocaleType !== 'string' ) {
2020-10-29 04:48:24 +01:00
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified i18n.defaultLocale should be a string. \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-29 04:48:24 +01:00
)
2020-10-07 23:11:01 +02:00
}
2020-10-10 12:22:45 +02:00
if ( typeof i18n . domains !== 'undefined' && ! Array . isArray ( i18n . domains ) ) {
throw new Error (
2020-11-20 14:04:18 +01:00
` Specified i18n.domains must be an array of domain objects e.g. [ { domain: 'example.fr', defaultLocale: 'fr', locales: ['fr'] } ] received ${ typeof i18n . domains } . \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-10 12:22:45 +02:00
)
}
if ( i18n . domains ) {
2020-12-04 11:14:55 +01:00
const invalidDomainItems = i18n . domains . filter ( ( item ) = > {
2020-10-10 12:22:45 +02:00
if ( ! item || typeof item !== 'object' ) return true
if ( ! item . defaultLocale ) return true
if ( ! item . domain || typeof item . domain !== 'string' ) return true
2020-10-27 09:45:39 +01:00
let hasInvalidLocale = false
if ( Array . isArray ( item . locales ) ) {
for ( const locale of item . locales ) {
if ( typeof locale !== 'string' ) hasInvalidLocale = true
2020-12-04 11:14:55 +01:00
for ( const domainItem of i18n . domains || [ ] ) {
2020-10-27 09:45:39 +01:00
if ( domainItem === item ) continue
if ( domainItem . locales && domainItem . locales . includes ( locale ) ) {
console . warn (
` Both ${ item . domain } and ${ domainItem . domain } configured the locale ( ${ locale } ) but only one can. Remove it from one i18n.domains config to continue `
)
hasInvalidLocale = true
break
}
}
}
}
return hasInvalidLocale
2020-10-10 12:22:45 +02:00
} )
if ( invalidDomainItems . length > 0 ) {
throw new Error (
` Invalid i18n.domains values: \ n ${ invalidDomainItems
. map ( ( item : any ) = > JSON . stringify ( item ) )
. join (
'\n'
2020-11-02 03:02:28 +01:00
) } \ n \ ndomains value must follow format { domain : 'example.fr' , defaultLocale : 'fr' , locales : [ 'fr' ] } . \ nSee more info here : https : //err.sh/next.js/invalid-i18n-config`
2020-10-10 12:22:45 +02:00
)
}
}
2020-10-08 13:12:17 +02:00
if ( ! Array . isArray ( i18n . locales ) ) {
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified i18n.locales must be an array of locale strings e.g. ["en-US", "nl-NL"] received ${ typeof i18n . locales } . \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-08 13:12:17 +02:00
)
}
const invalidLocales = i18n . locales . filter (
( locale : any ) = > typeof locale !== 'string'
)
if ( invalidLocales . length > 0 ) {
throw new Error (
2020-10-29 04:48:24 +01:00
` Specified i18n.locales contains invalid values ( ${ invalidLocales
. map ( String )
. join (
', '
) } ) , locales must be valid locale tags provided as strings e . g . "en-US" . \ n ` +
2020-10-08 13:12:17 +02:00
` See here for list of valid language sub-tags: http://www.iana.org/assignments/language-subtag-registry/language-subtag-registry `
)
}
2020-10-07 23:11:01 +02:00
if ( ! i18n . locales . includes ( i18n . defaultLocale ) ) {
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified i18n.defaultLocale should be included in i18n.locales. \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-07 23:11:01 +02:00
)
}
2020-10-08 13:12:17 +02:00
// make sure default Locale is at the front
i18n . locales = [
i18n . defaultLocale ,
2020-12-04 11:14:55 +01:00
. . . i18n . locales . filter ( ( locale ) = > locale !== i18n . defaultLocale ) ,
2020-10-08 13:12:17 +02:00
]
2020-11-02 22:02:15 +01:00
const localeDetectionType = typeof i18n . localeDetection
2020-10-07 23:11:01 +02:00
if (
localeDetectionType !== 'boolean' &&
localeDetectionType !== 'undefined'
) {
throw new Error (
2020-11-02 03:02:28 +01:00
` Specified i18n.localeDetection should be undefined or a boolean received ${ localeDetectionType } . \ nSee more info here: https://err.sh/next.js/invalid-i18n-config `
2020-10-07 23:11:01 +02:00
)
}
}
2019-12-10 21:21:52 +01:00
return result
2019-02-20 14:00:03 +01:00
}
2020-02-14 21:42:44 +01:00
export 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 (
2020-05-27 23:51:11 +02:00
'> Promise returned in next config. https://err.sh/vercel/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
2020-01-08 17:30:53 +01:00
if ( 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
)
2020-01-29 09:12:30 +01:00
if ( Object . keys ( userConfig ) . length === 0 ) {
2020-08-04 09:58:23 +02:00
Log . warn (
'Detected next.config.js, no exported configuration found. https://err.sh/vercel/next.js/empty-configuration'
2020-01-29 09:12:30 +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
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
2020-01-08 17:30:53 +01:00
if ( 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-10-30 14:39:58 +01:00
if (
2020-01-08 17:30:53 +01:00
userConfig . experimental ? . reactMode &&
2019-10-30 14:39:58 +01:00
! reactModes . includes ( userConfig . experimental . reactMode )
) {
throw new Error (
` Specified React Mode is invalid. Provided: ${
userConfig . experimental . reactMode
} should be one of $ { reactModes . join ( ', ' ) } `
)
}
2020-08-24 03:37:48 +02:00
return assignDefaults ( {
configOrigin : CONFIG_FILE ,
configFile : path ,
. . . 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 }
)
2020-01-08 17:30:53 +01:00
if ( nonJsPath ? . length ) {
2019-10-30 13:18:33 +01:00
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
}