Skip to content

Commit

Permalink
next.config.js generateBuildId support
Browse files Browse the repository at this point in the history
enable customising the build id via config
  • Loading branch information
Matt Bessey committed Mar 2, 2018
1 parent 15e21b4 commit 29b7fcf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
19 changes: 19 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -1066,6 +1066,25 @@ module.exports = {
}
```

#### Configuring the build ID

Next uses a constant generated at build time to identify which version of your application it is being served by. This can cause problems in multi-server deployments because the default ID is not stable across server builds. In order to make it stable you can customize as you see fit, syncronously:

```js
module.exports = {
generateBuildId: () => process.env.BUILD_ID
}
```

or asyncronously:


```js
module.exports = {
generateBuildId: async () => await generateIdFromGitSHA()
}
```

### Customizing webpack config

<p><details>
Expand Down
16 changes: 10 additions & 6 deletions server/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import { PHASE_PRODUCTION_BUILD } from '../../lib/constants'
import getBaseWebpackConfig from './webpack'
import md5File from 'md5-file/promise'

export default async function build(dir, conf = null) {
export default async function build (dir, conf = null) {
const config = getConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const buildId = process.env.BUILD_ID || uuid.v4()

let buildId
if (typeof config.generateBuildId === 'function') {
buildId = await config.generateBuildId()
} else {
buildId = uuid.v4()
}
try {
await fs.access(dir, fs.constants.W_OK)
} catch (err) {
Expand All @@ -34,7 +38,7 @@ export default async function build(dir, conf = null) {
}
}

function runCompiler(compiler) {
function runCompiler (compiler) {
return new Promise(async (resolve, reject) => {
const webpackCompiler = await webpack(await compiler)
webpackCompiler.run((err, stats) => {
Expand All @@ -54,7 +58,7 @@ function runCompiler(compiler) {
})
}

async function writeBuildStats(dir, config) {
async function writeBuildStats (dir, config) {
// Here we can't use hashes in webpack chunks.
// That's because the "app.js" is not tied to a chunk.
// It's created by merging a few assets. (commons.js and main.js)
Expand All @@ -68,7 +72,7 @@ async function writeBuildStats(dir, config) {
await fs.writeFile(buildStatsPath, JSON.stringify(assetHashMap), 'utf8')
}

async function writeBuildId(dir, buildId, config) {
async function writeBuildId (dir, buildId, config) {
const buildIdPath = join(dir, config.distDir, 'BUILD_ID')
await fs.writeFile(buildIdPath, buildId, 'utf8')
}
3 changes: 2 additions & 1 deletion test/integration/config/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ module.exports = withCSS({
},
publicRuntimeConfig: {
staticFolder: '/static'
}
},
generateBuildId: async () => await 'TEST_BUILD_ID'
})

0 comments on commit 29b7fcf

Please sign in to comment.