Skip to content

Commit 92e0990

Browse files
authored
feat: try to generate stable build ids (#606)
1 parent acc89fe commit 92e0990

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,9 @@ Temporary Items
149149
.apdisk
150150

151151
# End of https://www.toptal.com/developers/gitignore/api/osx,node
152+
152153
demo/package-lock.json
154+
# We generate this on build in the demo
155+
demo/next.config.js
153156

154157
.netlify

demo/next.config.js

Lines changed: 0 additions & 4 deletions
This file was deleted.

helpers/verifyBuildTarget.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@ const verifyBuildTarget = async ({ failBuild, netlifyConfig }) => {
1212

1313
const { target, configFile } = await getNextConfig(failBuild, nextRoot)
1414

15-
// If the next config exists, log warning if target isnt in acceptableTargets
15+
if (!configFile) {
16+
await writeFile(
17+
path.resolve(nextRoot, 'next.config.js'),
18+
`module.exports = {
19+
// Supported targets are "serverless" and "experimental-serverless-trace"
20+
target: "serverless",
21+
generateBuildId: () => "build"
22+
}`,
23+
)
24+
}
25+
26+
// If the next config exists, log warning if target isn't in acceptableTargets
1627
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
1728
const isValidTarget = acceptableTargets.includes(target)
1829
if (isValidTarget) {
@@ -49,18 +60,6 @@ const verifyBuildTarget = async ({ failBuild, netlifyConfig }) => {
4960
// Clear memoized cache
5061
getNextConfig.clear()
5162

52-
// Creating a config file, because otherwise Next won't reload the config and pick up the new target
53-
54-
if (!configFile) {
55-
await writeFile(
56-
path.resolve(nextRoot, 'next.config.js'),
57-
`
58-
module.exports = {
59-
// Supported targets are "serverless" and "experimental-serverless-trace"
60-
target: "serverless"
61-
}`,
62-
)
63-
}
6463
// Force the new config to be generated
6564
await getNextConfig(failBuild, nextRoot)
6665
// Reset the value in case something else is looking for it

index.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { readdirSync, existsSync } = require('fs')
22
const path = require('path')
33

4-
const { yellowBright, greenBright } = require('chalk')
4+
const { yellowBright, greenBright, green } = require('chalk')
55
const makeDir = require('make-dir')
66
const { satisfies } = require('semver')
77
const glob = require('tiny-glob')
@@ -140,12 +140,24 @@ See https://ntl.fyi/remove-plugin for instructions.
140140
const nextRoot = getNextRoot({ netlifyConfig })
141141

142142
const nextConfig = await getNextConfig(utils.failBuild, nextRoot)
143-
await saveCache({ cache: utils.cache, distDir: nextConfig.distDir, nextRoot })
143+
const { distDir, generateBuildId } = nextConfig
144+
await saveCache({ cache: utils.cache, distDir, nextRoot })
144145
copyUnstableIncludedDirs({ nextConfig, functionsDist: path.resolve(FUNCTIONS_DIST) })
145146
utils.status.show({
146147
title: 'Essential Next.js Build Plugin ran successfully',
147148
summary: 'Generated serverless functions and stored the Next.js cache',
148149
})
150+
151+
// The default generateBuildId function returns null, which causes it to use a random value from nanoid
152+
// eslint-disable-next-line no-self-compare
153+
if (generateBuildId() === null || generateBuildId() !== generateBuildId()) {
154+
console.warn(
155+
yellowBright`
156+
For faster deploy times, build IDs should be set to a static value.
157+
To do this, set ${green`generateBuildId: () => 'build'`} in your next.config.js`,
158+
// TODO: add shortlink when docs are updated
159+
)
160+
}
149161
},
150162
}
151163

0 commit comments

Comments
 (0)