Skip to content

Commit 9578fa9

Browse files
authored
feat: default edge middleware to on (#1547)
* feat: default edge middleware to on * chore: fal build if edge disabled with edge runtime
1 parent ba27564 commit 9578fa9

File tree

5 files changed

+42
-33
lines changed

5 files changed

+42
-33
lines changed

demos/middleware/netlify.toml

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ command = "npm run build"
33
publish = ".next"
44
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"
55

6-
[build.environment]
7-
NEXT_USE_NETLIFY_EDGE = "true"
8-
96
[[plugins]]
107
package = "../plugin-wrapper/"
118

demos/server-components/netlify.toml

-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ command = "npm run build"
33
publish = ".next"
44
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"
55

6-
[build.environment]
7-
NEXT_USE_NETLIFY_EDGE = "true"
8-
96
[[plugins]]
107
package = "../plugin-wrapper/"
118

packages/runtime/src/helpers/edge.ts

+6-7
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,9 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
135135
await emptyDir(edgeFunctionRoot)
136136

137137
if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
138-
if (!process.env.NEXT_USE_NETLIFY_EDGE) {
139-
console.log(
140-
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
141-
)
142-
}
138+
console.log(
139+
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
140+
)
143141
const edgeFunctionDir = join(edgeFunctionRoot, 'ipx')
144142
await ensureDir(edgeFunctionDir)
145143
await copyEdgeSourceFile({ edgeFunctionDir, file: 'ipx.ts', target: 'index.ts' })
@@ -152,7 +150,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
152150
path: '/_next/image*',
153151
})
154152
}
155-
if (process.env.NEXT_USE_NETLIFY_EDGE) {
153+
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
156154
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
157155
if (!middlewareManifest) {
158156
console.error("Couldn't find the middleware manifest")
@@ -184,9 +182,10 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
184182
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
185183
}
186184

187-
export const updateConfig = async (publish: string) => {
185+
export const enableEdgeInNextConfig = async (publish: string) => {
188186
const configFile = join(publish, 'required-server-files.json')
189187
const config = await readJSON(configFile)
188+
// This is for runtime in Next.js, rather than a build plugin setting
190189
config.config.env.NEXT_USE_NETLIFY_EDGE = 'true'
191190
await writeJSON(configFile, config)
192191
}

packages/runtime/src/helpers/files.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => {
6060
}
6161

6262
export const getMiddleware = async (publish: string): Promise<Array<string>> => {
63-
if (process.env.NEXT_USE_NETLIFY_EDGE) {
63+
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
6464
return []
6565
}
6666
const manifestPath = join(publish, 'server', 'middleware-manifest.json')

packages/runtime/src/index.ts

+35-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { join, relative } from 'path'
33

44
import type { NetlifyPlugin } from '@netlify/build'
5-
import { greenBright, yellowBright, bold } from 'chalk'
5+
import { greenBright, bold, redBright } from 'chalk'
66
import { existsSync, readFileSync } from 'fs-extra'
77
import { outdent } from 'outdent'
88

@@ -15,7 +15,7 @@ import {
1515
configureHandlerFunctions,
1616
generateCustomHeaders,
1717
} from './helpers/config'
18-
import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
18+
import { enableEdgeInNextConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
1919
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
2020
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
2121
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
@@ -80,6 +80,34 @@ const plugin: NetlifyPlugin = {
8080
},
8181
)
8282

83+
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
84+
85+
let usingEdge = false
86+
87+
if (Object.keys(middlewareManifest?.functions).length !== 0) {
88+
usingEdge = true
89+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
90+
failBuild(outdent`
91+
You are using Next.js experimental edge runtime, but have set NEXT_DISABLE_NETLIFY_EDGE to true. This is not supported.
92+
To use edge runtime, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
93+
`)
94+
}
95+
}
96+
97+
if (Object.keys(middlewareManifest?.middleware).length !== 0) {
98+
usingEdge = true
99+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
100+
console.log(
101+
redBright(outdent`
102+
You are using Next.js Middleware without Netlify Edge Functions.
103+
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
104+
It also disables advanced middleware features from @netlify/next
105+
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
106+
`),
107+
)
108+
}
109+
}
110+
83111
if (experimental.images) {
84112
experimentalRemotePatterns = experimental.images.remotePatterns || []
85113
}
@@ -138,27 +166,15 @@ const plugin: NetlifyPlugin = {
138166
buildId,
139167
})
140168

141-
// We call this even if we don't have edge functions enabled because we still use it for images
142-
await writeEdgeFunctions(netlifyConfig)
169+
if (usingEdge) {
170+
await writeEdgeFunctions(netlifyConfig)
171+
172+
await enableEdgeInNextConfig(publish)
143173

144-
if (process.env.NEXT_USE_NETLIFY_EDGE) {
145174
console.log(outdent`
146-
✨ Deploying to ${greenBright`Netlify Edge Functions`}
175+
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`}
147176
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
148177
`)
149-
await updateConfig(publish)
150-
}
151-
152-
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
153-
154-
if (!process.env.NEXT_USE_NETLIFY_EDGE && middlewareManifest?.sortedMiddleware?.length) {
155-
console.log(
156-
yellowBright(outdent`
157-
You are using Next.js Middleware without Netlify Edge Functions.
158-
This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering.
159-
To get the best performance and use Netlify Edge Functions, set the env var ${bold`NEXT_USE_NETLIFY_EDGE=true`}.
160-
`),
161-
)
162178
}
163179
},
164180

0 commit comments

Comments
 (0)