Skip to content

Commit 087607c

Browse files
committed
chore: fal build if edge disabled with edge runtime
1 parent 14d4bb6 commit 087607c

File tree

2 files changed

+37
-26
lines changed

2 files changed

+37
-26
lines changed

packages/runtime/src/helpers/edge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
182182
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
183183
}
184184

185-
export const updateConfig = async (publish: string) => {
185+
export const enableEdgeInNextConfig = async (publish: string) => {
186186
const configFile = join(publish, 'required-server-files.json')
187187
const config = await readJSON(configFile)
188188
// This is for runtime in Next.js, rather than a build plugin setting

packages/runtime/src/index.ts

Lines changed: 36 additions & 25 deletions
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,32 +166,15 @@ const plugin: NetlifyPlugin = {
138166
buildId,
139167
})
140168

141-
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
169+
if (usingEdge) {
142170
await writeEdgeFunctions(netlifyConfig)
143171

144-
await updateConfig(publish)
145-
}
172+
await enableEdgeInNextConfig(publish)
146173

147-
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
148-
if (
149-
Object.keys(middlewareManifest?.middleware).length !== 0 ||
150-
Object.keys(middlewareManifest?.functions).length !== 0
151-
) {
152-
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
153-
console.log(
154-
yellowBright(outdent`
155-
You are using Next.js Middleware without Netlify Edge Functions.
156-
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
157-
It also disables advanced middleware features from @netlify/next
158-
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
159-
`),
160-
)
161-
} else {
162-
console.log(outdent`
163-
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`}
164-
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
165-
`)
166-
}
174+
console.log(outdent`
175+
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`}
176+
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
177+
`)
167178
}
168179
},
169180

0 commit comments

Comments
 (0)