From d0dd1bc28d354262746eb33020614b3544e83835 Mon Sep 17 00:00:00 2001 From: Tim Neutkens Date: Mon, 12 Nov 2018 15:57:48 +0100 Subject: [PATCH] Trim buildId returned from generateBuildId (#5634) * Trim buildId returned from generateBuildId * Pass dir to function --- errors/generatebuildid-not-a-string.md | 9 ++++++++ packages/next/build/index.js | 32 ++++++++++++++++++-------- 2 files changed, 31 insertions(+), 10 deletions(-) create mode 100644 errors/generatebuildid-not-a-string.md diff --git a/errors/generatebuildid-not-a-string.md b/errors/generatebuildid-not-a-string.md new file mode 100644 index 00000000..dc0d89ff --- /dev/null +++ b/errors/generatebuildid-not-a-string.md @@ -0,0 +1,9 @@ +# generateBuildId did not return a string + +#### Why This Error Occurred + +The most common cause for this issue is a custom `next.config.js` with the `generateBuildId` method defined, but it does not return a string. + +#### Possible Ways to Fix It + +Always return a string from generateBuildId. diff --git a/packages/next/build/index.js b/packages/next/build/index.js index 879845d0..7ed2c044 100644 --- a/packages/next/build/index.js +++ b/packages/next/build/index.js @@ -10,22 +10,34 @@ import getBaseWebpackConfig from './webpack' const access = promisify(fs.access) const writeFile = promisify(fs.writeFile) -export default async function build (dir, conf = null) { - const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf) - const distDir = join(dir, config.distDir) - - let buildId = await config.generateBuildId() // defaults to a uuid - if (buildId == null) { - // nanoid is a small url-safe uuid generator - buildId = nanoid() +async function generateBuildId (generate, fallback) { + let buildId = await generate() + // If there's no buildId defined we'll fall back + if (buildId === null) { + buildId = fallback() } + if (typeof buildId !== 'string') { + throw new Error('generateBuildId did not return a string. https://err.sh/zeit/next.js/generatebuildid-not-a-string') + } + + return buildId.trim() +} + +async function ensureProjectDirectoryIsWriteAble (dir) { try { await access(dir, (fs.constants || fs).W_OK) } catch (err) { - console.error(`> Failed, build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable`) - throw err + throw new Error('Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable') } +} + +export default async function build (dir, conf = null) { + const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf) + const distDir = join(dir, config.distDir) + const buildId = await generateBuildId(config.generateBuildId, nanoid) + + await ensureProjectDirectoryIsWriteAble(dir) try { const configs = await Promise.all([