Skip to content

Commit 2555815

Browse files
committed
fix: cwd memoization
1 parent 8cb478c commit 2555815

File tree

6 files changed

+16
-10
lines changed

6 files changed

+16
-10
lines changed

Diff for: helpers/getNextConfig.js

+10-4
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,35 @@
11
'use strict'
22

3+
const { cwd: getCwd } = require('process')
34
const { resolve } = require('path')
45

56
const moize = require('moize')
67

78
// We used to cache nextConfig for any cwd. Now we pass process.cwd() to cache
89
// (or memoize) nextConfig per cwd.
9-
const getNextConfig = async function (cwd, failBuild = defaultFailBuild) {
10+
const getNextConfig = async function (failBuild = defaultFailBuild, cwd = getCwd()) {
1011
// We cannot load `next` at the top-level because we validate whether the
1112
// site is using `next` inside `onPreBuild`.
1213
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
1314
const loadConfig = require('next/dist/next-server/server/config').default
1415

1516
try {
16-
return await loadConfig(PHASE_PRODUCTION_BUILD, resolve('.'))
17+
return await loadConfig(PHASE_PRODUCTION_BUILD, cwd)
1718
} catch (error) {
1819
return failBuild('Error loading your next.config.js.', { error })
1920
}
2021
}
2122

22-
const moizedGetNextConfig = moize(getNextConfig, { maxSize: 1e3, isPromise: true })
23+
const moizedGetNextConfig = moize(getNextConfig, {
24+
maxSize: 1e3,
25+
isPromise: true,
26+
// Memoization cache key. We need to use `transformArgs` so `process.cwd()`
27+
// default value is assigned
28+
transformArgs: ([, cwd = getCwd()]) => [cwd],
29+
})
2330

2431
const defaultFailBuild = function (message, { error }) {
2532
throw new Error(`${message}\n${error.stack}`)
2633
}
2734

28-
// module.exports = process.env.NODE_ENV === 'test' ? getNextConfig : moizedGetNextConfig
2935
module.exports = moizedGetNextConfig

Diff for: helpers/hasCorrectNextConfig.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => {
55
// In the plugin's case, no config is valid because we'll make it ourselves
66
if (nextConfigPath === undefined) return true
77

8-
const { target } = await getNextConfig(process.cwd(), failBuild)
8+
const { target } = await getNextConfig(failBuild)
99

1010
// If the next config exists, log warning if target isnt in acceptableTargets
1111
const acceptableTargets = ['serverless', 'experimental-serverless-trace']

Diff for: index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
return failBuild('Could not find a package.json for this project')
2929
}
3030

31-
const nextConfig = await getNextConfig(process.cwd(), utils.failBuild)
31+
const nextConfig = await getNextConfig(utils.failBuild)
3232
await restoreCache({ cache: utils.cache, distDir: nextConfig.distDir })
3333

3434
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
@@ -70,7 +70,7 @@ module.exports = {
7070
return
7171
}
7272

73-
const nextConfig = await getNextConfig(process.cwd(), utils.failBuild)
73+
const nextConfig = await getNextConfig(utils.failBuild)
7474
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir })
7575
copyUnstableIncludedDirs({ nextConfig, functionsDist: FUNCTIONS_DIST })
7676
},

Diff for: src/lib/helpers/getI18n.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
const getNextConfig = require('../../../helpers/getNextConfig')
33

44
const getI18n = async () => {
5-
const nextConfig = await getNextConfig(process.cwd())
5+
const nextConfig = await getNextConfig()
66

77
return nextConfig.i18n || { locales: [] }
88
}

Diff for: src/lib/helpers/getNextDistDir.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const { join } = require('path')
33
const getNextConfig = require('../../../helpers/getNextConfig')
44

55
const getNextDistDir = async () => {
6-
const nextConfig = await getNextConfig(process.cwd())
6+
const nextConfig = await getNextConfig()
77

88
return join('.', nextConfig.distDir)
99
}

Diff for: src/lib/helpers/getPrerenderManifest.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const transformManifestForI18n = async (manifest) => {
2727
}
2828

2929
const getPrerenderManifest = async () => {
30-
const nextConfig = await getNextConfig(process.cwd())
30+
const nextConfig = await getNextConfig()
3131
const nextDistDir = await getNextDistDir()
3232
const manifest = readJSONSync(join(nextDistDir, 'prerender-manifest.json'))
3333
if (nextConfig.i18n) return await transformManifestForI18n(manifest)

0 commit comments

Comments
 (0)