Skip to content

Commit 93f87e7

Browse files
committed
chore: load next config depends on root publish build directory
1 parent 2f564b1 commit 93f87e7

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

helpers/defaultFailBuild.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
exports.defaultFailBuild = function (message, { error }) {
2+
throw new Error(`${message}\n${error.stack}`)
3+
}

helpers/getNextConfig.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,23 @@
22

33
const { resolve } = require('path')
44

5+
const { defaultFailBuild } = require('./defaultFailBuild')
56
const moize = require('moize')
67

78
// Load next.config.js
8-
const getNextConfig = async function (failBuild = defaultFailBuild) {
9+
const getNextConfig = async function (failBuild = defaultFailBuild, publishPath = '.') {
910
// We cannot load `next` at the top-level because we validate whether the
1011
// site is using `next` inside `onPreBuild`.
1112
const { PHASE_PRODUCTION_BUILD } = require('next/constants')
1213
const loadConfig = require('next/dist/next-server/server/config').default
1314

1415
try {
15-
return await loadConfig(PHASE_PRODUCTION_BUILD, resolve('.'))
16+
return await loadConfig(PHASE_PRODUCTION_BUILD, resolve(publishPath))
1617
} catch (error) {
1718
return failBuild('Error loading your next.config.js.', { error })
1819
}
1920
}
2021

2122
const moizedGetNextConfig = moize(getNextConfig, { maxSize: 1e3, isPromise: true })
2223

23-
const defaultFailBuild = function (message, { error }) {
24-
throw new Error(`${message}\n${error.stack}`)
25-
}
26-
2724
module.exports = moizedGetNextConfig

src/lib/helpers/getNextDistDir.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
// Get the NextJS distDir specified in next.config.js
22
const { join } = require('path')
3+
34
const getNextConfig = require('../../../helpers/getNextConfig')
5+
const { defaultFailBuild } = require('../../../helpers/defaultFailBuild')
46

5-
const getNextDistDir = async () => {
6-
const nextConfig = await getNextConfig()
7+
const getNextDistDir = async (publishPath) => {
8+
const nextConfig = await getNextConfig(defaultFailBuild, publishPath)
79

810
return join('.', nextConfig.distDir)
911
}

src/lib/steps/copyNextAssets.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getNextDistDir = require('../helpers/getNextDistDir')
66
// Copy the NextJS' static assets from NextJS distDir to Netlify publish folder.
77
// These need to be available for NextJS to work.
88
const copyNextAssets = async (publishPath) => {
9-
const nextDistDir = await getNextDistDir()
9+
const nextDistDir = await getNextDistDir(publishPath)
1010
const staticAssetsPath = join(nextDistDir, 'static')
1111
if (!existsSync(staticAssetsPath)) {
1212
throw new Error(

0 commit comments

Comments
 (0)