2
2
import { join , relative } from 'path'
3
3
4
4
import type { NetlifyPlugin } from '@netlify/build'
5
- import { greenBright , yellowBright , bold } from 'chalk'
5
+ import { greenBright , bold , redBright } from 'chalk'
6
6
import { existsSync , readFileSync } from 'fs-extra'
7
7
import { outdent } from 'outdent'
8
8
@@ -15,7 +15,7 @@ import {
15
15
configureHandlerFunctions ,
16
16
generateCustomHeaders ,
17
17
} from './helpers/config'
18
- import { updateConfig , writeEdgeFunctions , loadMiddlewareManifest } from './helpers/edge'
18
+ import { enableEdgeInNextConfig , writeEdgeFunctions , loadMiddlewareManifest } from './helpers/edge'
19
19
import { moveStaticPages , movePublicFiles , patchNextFiles } from './helpers/files'
20
20
import { generateFunctions , setupImageFunction , generatePagesResolver } from './helpers/functions'
21
21
import { generateRedirects , generateStaticRedirects } from './helpers/redirects'
@@ -80,6 +80,34 @@ const plugin: NetlifyPlugin = {
80
80
} ,
81
81
)
82
82
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
+
83
111
if ( experimental . images ) {
84
112
experimentalRemotePatterns = experimental . images . remotePatterns || [ ]
85
113
}
@@ -138,32 +166,15 @@ const plugin: NetlifyPlugin = {
138
166
buildId,
139
167
} )
140
168
141
- if ( ! process . env . NEXT_DISABLE_NETLIFY_EDGE ) {
169
+ if ( usingEdge ) {
142
170
await writeEdgeFunctions ( netlifyConfig )
143
171
144
- await updateConfig ( publish )
145
- }
172
+ await enableEdgeInNextConfig ( publish )
146
173
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
+ ` )
167
178
}
168
179
} ,
169
180
0 commit comments