Skip to content

fix: handle empty publish directory #457

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

Merged
merged 2 commits into from
Jun 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions helpers/checkNxConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const { existsSync } = require('fs')
const { EOL } = require('os')
const path = require('path')

const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR } }) => {
const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBLISH_DIR = 'out' } }) => {
const errors = []
if (nextConfig.distDir === '.next') {
errors.push(
Expand All @@ -15,8 +15,9 @@ const checkNxConfig = ({ netlifyConfig, nextConfig, failBuild, constants: { PUBL
"Please set the 'publish' value in your Netlify build config to a folder inside your app directory. e.g. 'apps/myapp/out'",
)
}

// Look for the config file as a sibling of the publish dir
const expectedConfigFile = path.resolve(netlifyConfig.build.publish, '..', 'next.config.js')
const expectedConfigFile = path.resolve(netlifyConfig.build.publish || PUBLISH_DIR, '..', 'next.config.js')

if (expectedConfigFile !== nextConfig.configFile) {
const confName = path.relative(process.cwd(), nextConfig.configFile)
Expand Down
6 changes: 5 additions & 1 deletion helpers/getNextRoot.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ const path = require('path')
*/
const getNextRoot = ({ netlifyConfig }) => {
let nextRoot = process.cwd()
if (!existsSync(path.join(nextRoot, 'next.config.js')) && netlifyConfig.build.publish) {
if (
!existsSync(path.join(nextRoot, 'next.config.js')) &&
netlifyConfig.build.publish &&
netlifyConfig.build.publish !== nextRoot
) {
nextRoot = path.dirname(netlifyConfig.build.publish)
}
return nextRoot
Expand Down
7 changes: 4 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const { readdirSync } = require('fs')
const path = require('path')

const makeDir = require('make-dir')
Expand Down Expand Up @@ -60,7 +61,7 @@ module.exports = {
async onBuild({
netlifyConfig,
packageJson,
constants: { PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
constants: { PUBLISH_DIR = DEFAULT_PUBLISH_DIR, FUNCTIONS_SRC = DEFAULT_FUNCTIONS_SRC },
utils,
}) {
const { failBuild } = utils.build
Expand All @@ -74,10 +75,9 @@ module.exports = {
console.log(`** Running Next on Netlify package **`)

await makeDir(PUBLISH_DIR)

await nextOnNetlify({
functionsDir: path.resolve(FUNCTIONS_SRC),
publishDir: netlifyConfig.build.publish,
publishDir: netlifyConfig.build.publish || PUBLISH_DIR,
nextRoot,
})
},
Expand All @@ -96,3 +96,4 @@ module.exports = {

const DEFAULT_FUNCTIONS_SRC = 'netlify/functions'
const DEFAULT_FUNCTIONS_DIST = '.netlify/functions/'
const DEFAULT_PUBLISH_DIR = 'out'
6 changes: 4 additions & 2 deletions src/lib/steps/copyNextAssets.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { join } = require('path')
const { join, resolve } = require('path')

const { copySync, existsSync } = require('fs-extra')

Expand All @@ -16,7 +16,9 @@ const copyNextAssets = async (publishPath) => {
const staticAssetsPath = join(nextDistDir, 'static')
if (!existsSync(staticAssetsPath)) {
throw new Error(
`No static assets found in your distDir (missing /static in ${nextDistDir}). Please check your project configuration. Your next.config.js must be one of \`serverless\` or \`experimental-serverless-trace\`. Your build command should include \`next build\`.`,
`No static assets found in your distDir (missing /static in ${resolve(
nextDistDir,
)}). Please check your project configuration. Your next.config.js must be one of \`serverless\` or \`experimental-serverless-trace\`. Your build command should include \`next build\`.`,
)
}
logTitle('💼 Copying static NextJS assets to', publishPath)
Expand Down