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,27 +166,15 @@ const plugin: NetlifyPlugin = {
138
166
buildId,
139
167
} )
140
168
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 )
143
173
144
- if ( process . env . NEXT_USE_NETLIFY_EDGE ) {
145
174
console . log ( outdent `
146
- ✨ Deploying to ${ greenBright `Netlify Edge Functions` } ✨
175
+ ✨ Deploying middleware and functions to ${ greenBright `Netlify Edge Functions` } ✨
147
176
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
148
177
` )
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
- )
162
178
}
163
179
} ,
164
180
0 commit comments