From 5c2c9587395455adaa0a0dea5e2ecf5d10afa394 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Tue, 26 Oct 2021 10:23:45 +0100 Subject: [PATCH] fix: disable serverless targets --- README.md | 4 +++- src/helpers/verification.js | 8 -------- src/index.js | 7 ++++--- test/index.js | 10 +++++++++- 4 files changed, 16 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index af74ab1e46..d75a8b8858 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,9 @@ publish = ".next" package = "@netlify/plugin-nextjs" ``` -If you previously set `target: "serverless"` or a custom `distDir` in your `next.config.js`, or set `node_bundler` or `external_node_modules` in your `netlify.toml` these are no longer needed and can be removed. +If you previously set `target: "serverless"` or a custom `distDir` in your `next.config.js`, or set `node_bundler` or `external_node_modules` in your `netlify.toml` these are no longer needed and can be removed. + +The `serverless` and `experimental-serverless-trace` targets are deprecated in Next 12, and all builds with this plugin will now use the default `server` target. If you are using a monorepo you will need to change `publish` to point to the full path to the built `.next` directory, which may be in a subdirectory. If you have changed your `distDir` then it will need to match that. diff --git a/src/helpers/verification.js b/src/helpers/verification.js index 79deec9bd4..da9966a8fe 100644 --- a/src/helpers/verification.js +++ b/src/helpers/verification.js @@ -8,14 +8,6 @@ const outdent = require('outdent') const prettyBytes = require('pretty-bytes') const { satisfies } = require('semver') -exports.verifyBuildTarget = (target) => { - if (target !== 'server') { - console.log( - yellowBright`Setting target to ${target} is no longer required. You should check if target=server works for you.`, - ) - } -} - // This is when nft support was added const REQUIRED_BUILD_VERSION = '>=18.16.0' diff --git a/src/index.js b/src/index.js index 98fd8e70fe..0da73e26f9 100644 --- a/src/index.js +++ b/src/index.js @@ -12,7 +12,6 @@ const { generateFunctions, setupImageFunction, generatePagesResolver } = require const { verifyNetlifyBuildVersion, checkNextSiteHasBuilt, - verifyBuildTarget, checkForRootPublish, logBetaMessage, checkZipSize, @@ -33,6 +32,10 @@ module.exports = { verifyNetlifyBuildVersion({ failBuild, ...constants }) await restoreCache({ cache, publish }) + + netlifyConfig.build.environment ||= {} + // eslint-disable-next-line unicorn/consistent-destructuring + netlifyConfig.build.environment.NEXT_PRIVATE_TARGET = 'server' }, async onBuild({ @@ -48,8 +51,6 @@ module.exports = { const { appDir, basePath, i18n, images, target, ignore } = await getNextConfig({ publish, failBuild }) - verifyBuildTarget(target) - configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) }) await generateFunctions(constants, appDir) diff --git a/test/index.js b/test/index.js index aca72c260b..eaba226bec 100644 --- a/test/index.js +++ b/test/index.js @@ -79,6 +79,8 @@ beforeEach(async () => { cleanup = tmpDir.cleanup netlifyConfig.build.publish = path.posix.resolve('.next') + netlifyConfig.build.environment = {} + netlifyConfig.redirects = [] netlifyConfig.functions[HANDLER_FUNCTION_NAME] && (netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files = []) netlifyConfig.functions[ODB_FUNCTION_NAME] && (netlifyConfig.functions[ODB_FUNCTION_NAME].included_files = []) @@ -88,7 +90,6 @@ beforeEach(async () => { afterEach(async () => { jest.clearAllMocks() jest.resetAllMocks() - delete process.env.NEXT_PRIVATE_TARGET // Cleans up the temporary directory from `getTmpDir()` and do not make it // the current directory anymore restoreCwd() @@ -133,6 +134,13 @@ describe('preBuild()', () => { expect(restore).toHaveBeenCalledWith(path.posix.resolve('.next/cache')) }) + + it('forces the target to "server"', async () => { + const netlifyConfig = { ...defaultArgs.netlifyConfig } + + await plugin.onPreBuild({ ...defaultArgs, netlifyConfig }) + expect(netlifyConfig.build.environment.NEXT_PRIVATE_TARGET).toBe('server') + }) }) describe('onBuild()', () => {