Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for async fn / promise in next.config.js/.mjs #33662

Merged
merged 7 commits into from Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions docs/api-reference/next.config.js/introduction.md
Expand Up @@ -48,6 +48,20 @@ module.exports = (phase, { defaultConfig }) => {
}
```

Since Next.js 12.0.10, you can use an async function:

```js
module.exports = async (phase, { defaultConfig }) => {
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
/* config options here */
}
return nextConfig
}
```

`phase` is the current context in which the configuration is loaded. You can see the [available phases](https://github.com/vercel/next.js/blob/canary/packages/next/shared/lib/constants.ts#L1-L5). Phases can be imported from `next/constants`:

```js
Expand Down
4 changes: 3 additions & 1 deletion errors/promise-in-next-config.md
Expand Up @@ -14,6 +14,8 @@ module.exports = {

#### Possible Ways to Fix It

Check your `next.config.js` for `async` or `return Promise`
In Next.js versions above 12.0.10 `module.exports = async () =>` is supported.
timneutkens marked this conversation as resolved.
Show resolved Hide resolved

For older versions, you can check your `next.config.js` for `async` or `return Promise`.

Potentially a plugin is returning a `Promise` from the webpack function.
11 changes: 3 additions & 8 deletions packages/next/server/config-shared.ts
Expand Up @@ -259,15 +259,10 @@ export const defaultConfig: NextConfig = {
},
}

export function normalizeConfig(phase: string, config: any) {
export async function normalizeConfig(phase: string, config: any) {
if (typeof config === 'function') {
config = config(phase, { defaultConfig })

if (typeof config.then === 'function') {
throw new Error(
'> Promise returned in next config. https://nextjs.org/docs/messages/promise-in-next-config'
)
}
}
return config
// Support `new Promise` and `async () =>` as return values of the config export
return await config
}
2 changes: 1 addition & 1 deletion packages/next/server/config.ts
Expand Up @@ -585,7 +585,7 @@ export default async function loadConfig(
)
throw err
}
const userConfig = normalizeConfig(
const userConfig = await normalizeConfig(
phase,
userConfigModule.default || userConfigModule
)
Expand Down
33 changes: 33 additions & 0 deletions test/e2e/config-promise-export/async-function.test.ts
@@ -0,0 +1,33 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'

describe('async export', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'next.config.js': `
module.exports = async () => {
return {
basePath: '/docs'
}
}
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('should work', async () => {
const html = await renderViaHTTP(next.url, '/docs')
expect(html).toContain('hello world')
})
})
33 changes: 33 additions & 0 deletions test/e2e/config-promise-export/promise.test.ts
@@ -0,0 +1,33 @@
import { createNext } from 'e2e-utils'
import { NextInstance } from 'test/lib/next-modes/base'
import { renderViaHTTP } from 'next-test-utils'

describe('promise export', () => {
let next: NextInstance

beforeAll(async () => {
next = await createNext({
files: {
'pages/index.js': `
export default function Page() {
return <p>hello world</p>
}
`,
'next.config.js': `
module.exports = new Promise((resolve) => {
resolve({
basePath: '/docs'
})
})
`,
},
dependencies: {},
})
})
afterAll(() => next.destroy())

it('should work', async () => {
const html = await renderViaHTTP(next.url, '/docs')
expect(html).toContain('hello world')
})
})