-
Notifications
You must be signed in to change notification settings - Fork 86
fix: force serverless target #343
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
const getNextConfig = require('./getNextConfig') | ||
|
||
// Checks if site has the correct next.config.js | ||
const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => { | ||
// In the plugin's case, no config is valid because we'll make it ourselves | ||
|
@@ -12,9 +11,29 @@ const hasCorrectNextConfig = async ({ nextConfigPath, failBuild }) => { | |
const isValidTarget = acceptableTargets.includes(target) | ||
if (!isValidTarget) { | ||
console.log( | ||
`Your next.config.js must set the "target" property to one of: ${acceptableTargets.join(', ')}. Update the | ||
target property to allow this plugin to run.`, | ||
`The "target" config property must be one of "${acceptableTargets.join( | ||
'", "', | ||
)}". Building with "serverless" target.`, | ||
) | ||
|
||
/* eslint-disable fp/no-delete, node/no-unpublished-require */ | ||
|
||
// We emulate Vercel so that we can set target to serverless if needed | ||
process.env.NOW_BUILDER = true | ||
// If no valid target is set, we use an internal Next env var to force it | ||
process.env.NEXT_PRIVATE_TARGET = 'serverless' | ||
|
||
// 🐉 We need Next to recalculate "isZeitNow" var so we can set the target, but it's | ||
// set as an import side effect so we need to clear the require cache first. 🐲 | ||
// https://github.com/vercel/next.js/blob/canary/packages/next/telemetry/ci-info.ts | ||
|
||
delete require.cache[require.resolve('next/dist/telemetry/ci-info')] | ||
delete require.cache[require.resolve('next/dist/next-server/server/config')] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering The only alternative I can think of would be to call Two potential problems we might have in the future though are:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unfortunately we're already relying on the internal layout as we're using unpublished exports to get the config in the first place |
||
|
||
// Clear memoized cache | ||
getNextConfig.clear() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why do we have to clear here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function is memoized, so it will return the old value if we don't clear it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This works. |
||
|
||
/* eslint-enable fp/no-delete, node/no-unpublished-require */ | ||
} | ||
|
||
return isValidTarget | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -28,21 +28,8 @@ module.exports = { | |||
if (hasNoPackageJson) { | ||||
return failBuild('Could not find a package.json for this project') | ||||
} | ||||
|
||||
const pluginNotNeeded = await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild }) | ||||
|
||||
if (!pluginNotNeeded) { | ||||
const nextConfigPath = await findUp('next.config.js') | ||||
if (nextConfigPath === undefined) { | ||||
// Create the next config file with target set to serverless by default | ||||
const nextConfig = ` | ||||
module.exports = { | ||||
target: 'serverless' | ||||
} | ||||
` | ||||
await pWriteFile('next.config.js', nextConfig) | ||||
} | ||||
} | ||||
// Populates the correct config if needed | ||||
await doesNotNeedPlugin({ netlifyConfig, packageJson, failBuild }) | ||||
|
||||
// Because we memoize nextConfig, we need to do this after the write file | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can prob remove this comment now i think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double ping on this one?
Suggested change
|
||||
const nextConfig = await getNextConfig(utils.failBuild) | ||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe some automated tests might be using the same process, i.e. running one test would change
process.env
for other tests. We could probably fix this by either:next
right away so it uses those environment variables, then deletingprocess.env.NOW_BUILDER
andprocess.env.NEXT_PRIVATE_TARGET
(or restoring them to their original value).