1
1
/* eslint-disable max-lines */
2
2
import { join , relative } from 'path'
3
3
4
- import type { NetlifyPlugin , OnPreBuild } from '@netlify/build'
5
- import { greenBright , yellowBright , bold } from 'chalk'
4
+ import type { NetlifyPlugin } from '@netlify/build'
5
+ import { greenBright , bold , redBright } from 'chalk'
6
6
import { existsSync , readFileSync } from 'fs-extra'
7
7
import { outdent } from 'outdent'
8
8
@@ -16,7 +16,7 @@ import {
16
16
generateCustomHeaders ,
17
17
} from './helpers/config'
18
18
import { onPreDev } from './helpers/dev'
19
- import { updateConfig , writeEdgeFunctions , loadMiddlewareManifest } from './helpers/edge'
19
+ import { enableEdgeInNextConfig , writeEdgeFunctions , loadMiddlewareManifest } from './helpers/edge'
20
20
import { moveStaticPages , movePublicFiles , patchNextFiles } from './helpers/files'
21
21
import { generateFunctions , setupImageFunction , generatePagesResolver } from './helpers/functions'
22
22
import { generateRedirects , generateStaticRedirects } from './helpers/redirects'
@@ -81,6 +81,34 @@ const plugin: NetlifyPlugin & { onPreDev?: OnPreBuild; onDev?: OnPreBuild } = {
81
81
} ,
82
82
)
83
83
84
+ const middlewareManifest = await loadMiddlewareManifest ( netlifyConfig )
85
+
86
+ let usingEdge = false
87
+
88
+ if ( Object . keys ( middlewareManifest ?. functions ) . length !== 0 ) {
89
+ usingEdge = true
90
+ if ( process . env . NEXT_DISABLE_NETLIFY_EDGE ) {
91
+ failBuild ( outdent `
92
+ You are using Next.js experimental edge runtime, but have set NEXT_DISABLE_NETLIFY_EDGE to true. This is not supported.
93
+ To use edge runtime, remove the env var ${ bold `NEXT_DISABLE_NETLIFY_EDGE` } .
94
+ ` )
95
+ }
96
+ }
97
+
98
+ if ( Object . keys ( middlewareManifest ?. middleware ) . length !== 0 ) {
99
+ usingEdge = true
100
+ if ( process . env . NEXT_DISABLE_NETLIFY_EDGE ) {
101
+ console . log (
102
+ redBright ( outdent `
103
+ You are using Next.js Middleware without Netlify Edge Functions.
104
+ This is deprecated because it negatively affects performance and will disable ISR and static rendering.
105
+ It also disables advanced middleware features from @netlify/next
106
+ To get the best performance and use Netlify Edge Functions, remove the env var ${ bold `NEXT_DISABLE_NETLIFY_EDGE` } .
107
+ ` ) ,
108
+ )
109
+ }
110
+ }
111
+
84
112
if ( experimental . images ) {
85
113
experimentalRemotePatterns = experimental . images . remotePatterns || [ ]
86
114
}
@@ -139,27 +167,15 @@ const plugin: NetlifyPlugin & { onPreDev?: OnPreBuild; onDev?: OnPreBuild } = {
139
167
buildId,
140
168
} )
141
169
142
- // We call this even if we don't have edge functions enabled because we still use it for images
143
- await writeEdgeFunctions ( netlifyConfig )
170
+ if ( usingEdge ) {
171
+ await writeEdgeFunctions ( netlifyConfig )
172
+
173
+ await enableEdgeInNextConfig ( publish )
144
174
145
- if ( process . env . NEXT_USE_NETLIFY_EDGE ) {
146
175
console . log ( outdent `
147
- ✨ Deploying to ${ greenBright `Netlify Edge Functions` } ✨
176
+ ✨ Deploying middleware and functions to ${ greenBright `Netlify Edge Functions` } ✨
148
177
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
149
178
` )
150
- await updateConfig ( publish )
151
- }
152
-
153
- const middlewareManifest = await loadMiddlewareManifest ( netlifyConfig )
154
-
155
- if ( ! process . env . NEXT_USE_NETLIFY_EDGE && middlewareManifest ?. sortedMiddleware ?. length ) {
156
- console . log (
157
- yellowBright ( outdent `
158
- You are using Next.js Middleware without Netlify Edge Functions.
159
- This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering.
160
- To get the best performance and use Netlify Edge Functions, set the env var ${ bold `NEXT_USE_NETLIFY_EDGE=true` } .
161
- ` ) ,
162
- )
163
179
}
164
180
} ,
165
181
0 commit comments