Skip to content

Commit 8dff29d

Browse files
authored
Merge pull request #408 from netlify/mk/force-serverless
fix: force serverless target
2 parents 7075f61 + 625a2c0 commit 8dff29d

8 files changed

+10154
-95
lines changed

demo/next.config.js

-5
This file was deleted.

helpers/doesNotNeedPlugin.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
const findUp = require('find-up')
22

33
// Checks all the cases for which the plugin should do nothing
4-
const isStaticExportProject = require('./isStaticExportProject')
54
const doesSiteUseNextOnNetlify = require('./doesSiteUseNextOnNetlify')
6-
const hasCorrectNextConfig = require('./hasCorrectNextConfig')
5+
const isStaticExportProject = require('./isStaticExportProject')
76

8-
const doesNotNeedPlugin = async ({ netlifyConfig, packageJson, failBuild }) => {
7+
const doesNotNeedPlugin = ({ netlifyConfig, packageJson }) => {
98
const { build } = netlifyConfig
10-
const { name, scripts = {} } = packageJson
11-
const nextConfigPath = await findUp('next.config.js')
9+
const { scripts = {} } = packageJson
1210

13-
return (
14-
isStaticExportProject({ build, scripts }) ||
15-
doesSiteUseNextOnNetlify({ packageJson }) ||
16-
!(await hasCorrectNextConfig({ nextConfigPath, failBuild }))
17-
)
11+
return isStaticExportProject({ build, scripts }) || doesSiteUseNextOnNetlify({ packageJson })
1812
}
1913

2014
module.exports = doesNotNeedPlugin

helpers/getNextConfig.js

-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const { cwd: getCwd } = require('process')
4-
const { resolve } = require('path')
54

65
const moize = require('moize')
76

helpers/hasCorrectNextConfig.js

-23
This file was deleted.

helpers/verifyBuildTarget.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const getNextConfig = require('./getNextConfig')
2+
const findUp = require('find-up')
3+
const { writeFile, unlink } = require('fs-extra')
4+
const path = require('path')
5+
6+
// Checks if site has the correct next.config.js
7+
const verifyBuildTarget = async ({ failBuild }) => {
8+
const { target } = await getNextConfig(failBuild)
9+
10+
// If the next config exists, log warning if target isnt in acceptableTargets
11+
const acceptableTargets = ['serverless', 'experimental-serverless-trace']
12+
const isValidTarget = acceptableTargets.includes(target)
13+
if (isValidTarget) {
14+
return
15+
}
16+
console.log(
17+
`The "target" config property must be one of "${acceptableTargets.join(
18+
'", "',
19+
)}". Building with "serverless" target.`,
20+
)
21+
22+
/* eslint-disable fp/no-delete, node/no-unpublished-require */
23+
24+
// We emulate Vercel so that we can set target to serverless if needed
25+
process.env.NOW_BUILDER = true
26+
// If no valid target is set, we use an internal Next env var to force it
27+
process.env.NEXT_PRIVATE_TARGET = 'serverless'
28+
29+
// 🐉 We need Next to recalculate "isZeitNow" var so we can set the target, but it's
30+
// set as an import side effect so we need to clear the require cache first. 🐲
31+
// https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/ci-info.ts
32+
33+
delete require.cache[require.resolve('next/dist/telemetry/ci-info')]
34+
delete require.cache[require.resolve('next/dist/next-server/server/config')]
35+
36+
// Clear memoized cache
37+
getNextConfig.clear()
38+
39+
// Creating a config file, because otherwise Next won't reload the config and pick up the new target
40+
41+
if (!(await findUp('next.config.js'))) {
42+
await writeFile(
43+
path.resolve('next.config.js'),
44+
`module.exports = {
45+
// Supported targets are "serverless" and "experimental-serverless-trace"
46+
target: "serverless"
47+
}`,
48+
)
49+
}
50+
// Force the new config to be generated
51+
await getNextConfig(failBuild)
52+
53+
// Reset the value in case something else is looking for it
54+
process.env.NOW_BUILDER = false
55+
/* eslint-enable fp/no-delete, node/no-unpublished-require */
56+
}
57+
58+
module.exports = verifyBuildTarget

index.js

+8-22
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
const fs = require('fs')
2-
const path = require('path')
3-
const util = require('util')
4-
5-
const findUp = require('find-up')
61
const makeDir = require('make-dir')
72

83
const { restoreCache, saveCache } = require('./helpers/cacheBuild')
94
const copyUnstableIncludedDirs = require('./helpers/copyUnstableIncludedDirs')
105
const doesNotNeedPlugin = require('./helpers/doesNotNeedPlugin')
116
const getNextConfig = require('./helpers/getNextConfig')
127
const validateNextUsage = require('./helpers/validateNextUsage')
8+
const verifyBuildTarget = require('./helpers/verifyBuildTarget')
139
const nextOnNetlify = require('./src')
1410

15-
const pWriteFile = util.promisify(fs.writeFile)
16-
1711
// * Helpful Plugin Context *
1812
// - Between the prebuild and build steps, the project's build command is run
1913
// - Between the build and postbuild steps, any functions are bundled
@@ -29,21 +23,13 @@ module.exports = {
2923
return failBuild('Could not find a package.json for this project')
3024
}
3125

32-
const pluginNotNeeded = await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })
33-
34-
if (!pluginNotNeeded) {
35-
const nextConfigPath = await findUp('next.config.js')
36-
if (nextConfigPath === undefined) {
37-
// Create the next config file with target set to serverless by default
38-
const nextConfig = `
39-
module.exports = {
40-
target: 'serverless'
41-
}
42-
`
43-
await pWriteFile('next.config.js', nextConfig)
44-
}
26+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
27+
return
4528
}
4629

30+
// Populates the correct config if needed
31+
await verifyBuildTarget({ netlifyConfig, packageJson, failBuild })
32+
4733
// Because we memoize nextConfig, we need to do this after the write file
4834
const nextConfig = await getNextConfig(utils.failBuild)
4935

@@ -64,7 +50,7 @@ module.exports = {
6450
}) {
6551
const { failBuild } = utils.build
6652

67-
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
53+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild })) {
6854
return
6955
}
7056

@@ -76,7 +62,7 @@ module.exports = {
7662
},
7763

7864
async onPostBuild({ netlifyConfig, packageJson, constants: { FUNCTIONS_DIST }, utils }) {
79-
if (await doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
65+
if (doesNotNeedPlugin({ netlifyConfig, packageJson, utils })) {
8066
return
8167
}
8268

0 commit comments

Comments
 (0)