From 7f335cb032a713183d494db7583840b082e3fc9e Mon Sep 17 00:00:00 2001 From: Craig McNamara Date: Sat, 31 Mar 2018 07:34:52 -0700 Subject: [PATCH] Allow BUILD_ID to be set using `generateBuildId` (minor) (#3873) * Allow BUILD_ID to be set in the environment This makes multi server deploys with Capistrano possible. * next.config.js generateBuildId support enable customising the build id via config * Provide default for generateBuildId * This is not used --- readme.md | 14 ++++++++++++++ server/build/index.js | 5 ++--- server/config.js | 2 ++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/readme.md b/readme.md index 8d5a278b6585d..addf1c44c2c5f 100644 --- a/readme.md +++ b/readme.md @@ -1122,6 +1122,20 @@ module.exports = { } ``` +#### Configuring the build ID + +Next.js uses a constant generated at build time to identify which version of your application is being served. This can cause problems in multi-server deployments when `next build` is ran on every server. In order to keep a static build id between builds you can provide the `generateBuildId` function: + +```js +// next.config.js +module.exports = { + generateBuildId: async () => { + // For example get the latest git commit hash here + return 'my-build-id' + } +} +``` + ### Customizing webpack config

diff --git a/server/build/index.js b/server/build/index.js index a76413f5f1c00..79331024bde8d 100644 --- a/server/build/index.js +++ b/server/build/index.js @@ -1,10 +1,9 @@ import { join } from 'path' import promisify from '../lib/promisify' import fs from 'fs' -import uuid from 'uuid' import webpack from 'webpack' import getConfig from '../config' -import {PHASE_PRODUCTION_BUILD} from '../../lib/constants' +import { PHASE_PRODUCTION_BUILD } from '../../lib/constants' import getBaseWebpackConfig from './webpack' const access = promisify(fs.access) @@ -12,7 +11,7 @@ const writeFile = promisify(fs.writeFile) export default async function build (dir, conf = null) { const config = getConfig(PHASE_PRODUCTION_BUILD, dir, conf) - const buildId = uuid.v4() + const buildId = await config.generateBuildId() // defaults to a uuid try { await access(dir, (fs.constants || fs).W_OK) diff --git a/server/config.js b/server/config.js index 045a4ac8c3f38..0d138d34c7ac8 100644 --- a/server/config.js +++ b/server/config.js @@ -1,5 +1,6 @@ // @flow import findUp from 'find-up' +import uuid from 'uuid' const cache = new Map() @@ -11,6 +12,7 @@ const defaultConfig = { assetPrefix: '', configOrigin: 'default', useFileSystemPublicRoutes: true, + generateBuildId: () => uuid.v4(), generateEtags: true, pageExtensions: ['jsx', 'js'] }