Skip to content

Commit

Permalink
Allow BUILD_ID to be set using generateBuildId (minor) (#3873)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Craig McNamara authored and timneutkens committed Mar 31, 2018
1 parent 085b2f8 commit 7f335cb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

<p><details>
Expand Down
5 changes: 2 additions & 3 deletions server/build/index.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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)
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)
Expand Down
2 changes: 2 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import findUp from 'find-up'
import uuid from 'uuid'

const cache = new Map()

Expand All @@ -11,6 +12,7 @@ const defaultConfig = {
assetPrefix: '',
configOrigin: 'default',
useFileSystemPublicRoutes: true,
generateBuildId: () => uuid.v4(),
generateEtags: true,
pageExtensions: ['jsx', 'js']
}
Expand Down

0 comments on commit 7f335cb

Please sign in to comment.