From 607b324db37d47ddff27c89eb5776bfea1323f2b Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Wed, 17 Aug 2022 19:13:11 +0100 Subject: [PATCH 1/2] feat: default edge middleware to on --- demos/middleware/netlify.toml | 3 --- demos/server-components/netlify.toml | 3 --- packages/runtime/src/helpers/edge.ts | 11 ++++---- packages/runtime/src/helpers/files.ts | 2 +- packages/runtime/src/index.ts | 39 +++++++++++++++------------ 5 files changed, 28 insertions(+), 30 deletions(-) diff --git a/demos/middleware/netlify.toml b/demos/middleware/netlify.toml index 1bb1c1f777..51f8d6ad06 100644 --- a/demos/middleware/netlify.toml +++ b/demos/middleware/netlify.toml @@ -3,9 +3,6 @@ command = "npm run build" publish = ".next" ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;" -[build.environment] -NEXT_USE_NETLIFY_EDGE = "true" - [[plugins]] package = "../plugin-wrapper/" diff --git a/demos/server-components/netlify.toml b/demos/server-components/netlify.toml index a0ab27257f..22e01e9f0b 100644 --- a/demos/server-components/netlify.toml +++ b/demos/server-components/netlify.toml @@ -3,9 +3,6 @@ command = "npm run build" publish = ".next" ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;" -[build.environment] -NEXT_USE_NETLIFY_EDGE = "true" - [[plugins]] package = "../plugin-wrapper/" diff --git a/packages/runtime/src/helpers/edge.ts b/packages/runtime/src/helpers/edge.ts index 77b0da69bd..5899fa357a 100644 --- a/packages/runtime/src/helpers/edge.ts +++ b/packages/runtime/src/helpers/edge.ts @@ -135,11 +135,9 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => { await emptyDir(edgeFunctionRoot) if (!process.env.NEXT_DISABLE_EDGE_IMAGES) { - if (!process.env.NEXT_USE_NETLIFY_EDGE) { - console.log( - 'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.', - ) - } + console.log( + 'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.', + ) const edgeFunctionDir = join(edgeFunctionRoot, 'ipx') await ensureDir(edgeFunctionDir) await copyEdgeSourceFile({ edgeFunctionDir, file: 'ipx.ts', target: 'index.ts' }) @@ -152,7 +150,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => { path: '/_next/image*', }) } - if (process.env.NEXT_USE_NETLIFY_EDGE) { + if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) { const middlewareManifest = await loadMiddlewareManifest(netlifyConfig) if (!middlewareManifest) { console.error("Couldn't find the middleware manifest") @@ -187,6 +185,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => { export const updateConfig = async (publish: string) => { const configFile = join(publish, 'required-server-files.json') const config = await readJSON(configFile) + // This is for runtime in Next.js, rather than a build plugin setting config.config.env.NEXT_USE_NETLIFY_EDGE = 'true' await writeJSON(configFile, config) } diff --git a/packages/runtime/src/helpers/files.ts b/packages/runtime/src/helpers/files.ts index 2629eb6e0c..8c9a84d847 100644 --- a/packages/runtime/src/helpers/files.ts +++ b/packages/runtime/src/helpers/files.ts @@ -60,7 +60,7 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => { } export const getMiddleware = async (publish: string): Promise> => { - if (process.env.NEXT_USE_NETLIFY_EDGE) { + if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) { return [] } const manifestPath = join(publish, 'server', 'middleware-manifest.json') diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index 2915ba2763..ba167e2a15 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -142,27 +142,32 @@ const plugin: NetlifyPlugin = { buildId, }) - // We call this even if we don't have edge functions enabled because we still use it for images - await writeEdgeFunctions(netlifyConfig) - - if (process.env.NEXT_USE_NETLIFY_EDGE) { - console.log(outdent` - ✨ Deploying to ${greenBright`Netlify Edge Functions`} ✨ - This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge - `) + if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) { + await writeEdgeFunctions(netlifyConfig) + await updateConfig(publish) } const middlewareManifest = await loadMiddlewareManifest(netlifyConfig) - - if (!process.env.NEXT_USE_NETLIFY_EDGE && middlewareManifest?.sortedMiddleware?.length) { - console.log( - yellowBright(outdent` - You are using Next.js Middleware without Netlify Edge Functions. - This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering. - To get the best performance and use Netlify Edge Functions, set the env var ${bold`NEXT_USE_NETLIFY_EDGE=true`}. - `), - ) + if ( + Object.keys(middlewareManifest?.middleware).length !== 0 || + Object.keys(middlewareManifest?.functions).length !== 0 + ) { + if (process.env.NEXT_DISABLE_NETLIFY_EDGE) { + console.log( + yellowBright(outdent` + You are using Next.js Middleware without Netlify Edge Functions. + This is deprecated because it negatively affects performance and will disable ISR and static rendering. + It also disables advanced middleware features from @netlify/next + To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}. + `), + ) + } else { + console.log(outdent` + ✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`} ✨ + This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge + `) + } } }, From 087607c5bd5c12966b91bacd1e4732a39fbd5671 Mon Sep 17 00:00:00 2001 From: Matt Kane Date: Thu, 18 Aug 2022 10:49:29 +0100 Subject: [PATCH 2/2] chore: fal build if edge disabled with edge runtime --- packages/runtime/src/helpers/edge.ts | 2 +- packages/runtime/src/index.ts | 61 ++++++++++++++++------------ 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/packages/runtime/src/helpers/edge.ts b/packages/runtime/src/helpers/edge.ts index 5899fa357a..4c88b4614c 100644 --- a/packages/runtime/src/helpers/edge.ts +++ b/packages/runtime/src/helpers/edge.ts @@ -182,7 +182,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => { await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest) } -export const updateConfig = async (publish: string) => { +export const enableEdgeInNextConfig = async (publish: string) => { const configFile = join(publish, 'required-server-files.json') const config = await readJSON(configFile) // This is for runtime in Next.js, rather than a build plugin setting diff --git a/packages/runtime/src/index.ts b/packages/runtime/src/index.ts index 0f06856cd5..75c26d8848 100644 --- a/packages/runtime/src/index.ts +++ b/packages/runtime/src/index.ts @@ -2,7 +2,7 @@ import { join, relative } from 'path' import type { NetlifyPlugin } from '@netlify/build' -import { greenBright, yellowBright, bold } from 'chalk' +import { greenBright, bold, redBright } from 'chalk' import { existsSync, readFileSync } from 'fs-extra' import { outdent } from 'outdent' @@ -15,7 +15,7 @@ import { configureHandlerFunctions, generateCustomHeaders, } from './helpers/config' -import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge' +import { enableEdgeInNextConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge' import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files' import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions' import { generateRedirects, generateStaticRedirects } from './helpers/redirects' @@ -80,6 +80,34 @@ const plugin: NetlifyPlugin = { }, ) + const middlewareManifest = await loadMiddlewareManifest(netlifyConfig) + + let usingEdge = false + + if (Object.keys(middlewareManifest?.functions).length !== 0) { + usingEdge = true + if (process.env.NEXT_DISABLE_NETLIFY_EDGE) { + failBuild(outdent` + You are using Next.js experimental edge runtime, but have set NEXT_DISABLE_NETLIFY_EDGE to true. This is not supported. + To use edge runtime, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}. + `) + } + } + + if (Object.keys(middlewareManifest?.middleware).length !== 0) { + usingEdge = true + if (process.env.NEXT_DISABLE_NETLIFY_EDGE) { + console.log( + redBright(outdent` + You are using Next.js Middleware without Netlify Edge Functions. + This is deprecated because it negatively affects performance and will disable ISR and static rendering. + It also disables advanced middleware features from @netlify/next + To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}. + `), + ) + } + } + if (experimental.images) { experimentalRemotePatterns = experimental.images.remotePatterns || [] } @@ -138,32 +166,15 @@ const plugin: NetlifyPlugin = { buildId, }) - if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) { + if (usingEdge) { await writeEdgeFunctions(netlifyConfig) - await updateConfig(publish) - } + await enableEdgeInNextConfig(publish) - const middlewareManifest = await loadMiddlewareManifest(netlifyConfig) - if ( - Object.keys(middlewareManifest?.middleware).length !== 0 || - Object.keys(middlewareManifest?.functions).length !== 0 - ) { - if (process.env.NEXT_DISABLE_NETLIFY_EDGE) { - console.log( - yellowBright(outdent` - You are using Next.js Middleware without Netlify Edge Functions. - This is deprecated because it negatively affects performance and will disable ISR and static rendering. - It also disables advanced middleware features from @netlify/next - To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}. - `), - ) - } else { - console.log(outdent` - ✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`} ✨ - This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge - `) - } + console.log(outdent` + ✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`} ✨ + This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge + `) } },