Skip to content

Commit

Permalink
Trim buildId returned from generateBuildId (vercel#5634)
Browse files Browse the repository at this point in the history
* Trim buildId returned from generateBuildId

* Pass dir to function
  • Loading branch information
timneutkens authored Nov 12, 2018
1 parent d8f8794 commit d0dd1bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
9 changes: 9 additions & 0 deletions errors/generatebuildid-not-a-string.md
Original file line number Diff line number Diff line change
@@ -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.
30 changes: 21 additions & 9 deletions packages/next/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
async function generateBuildId (generate, fallback) {
let buildId = await generate()
// If there's no buildId defined we'll fall back
if (buildId === null) {
buildId = fallback()
}

let buildId = await config.generateBuildId() // defaults to a uuid
if (buildId == null) {
// nanoid is a small url-safe uuid generator
buildId = nanoid()
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([
Expand Down

0 comments on commit d0dd1bc

Please sign in to comment.