@@ -3,9 +3,12 @@ import { promises as fs, existsSync } from 'fs'
3
3
import { resolve , join } from 'path'
4
4
5
5
import type { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
6
+ import { greenBright } from 'chalk'
7
+ import destr from 'destr'
6
8
import { copy , copyFile , emptyDir , ensureDir , readJSON , readJson , writeJSON , writeJson } from 'fs-extra'
7
9
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
8
10
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
11
+ import { outdent } from 'outdent'
9
12
10
13
import { getRequiredServerFiles } from './config'
11
14
@@ -198,14 +201,13 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
198
201
const edgeFunctionRoot = resolve ( '.netlify' , 'edge-functions' )
199
202
await emptyDir ( edgeFunctionRoot )
200
203
201
- await copy ( getEdgeTemplatePath ( '../edge-shared' ) , join ( edgeFunctionRoot , 'edge-shared' ) )
202
-
203
204
const { publish } = netlifyConfig . build
204
205
const nextConfigFile = await getRequiredServerFiles ( publish )
205
206
const nextConfig = nextConfigFile . config
207
+ await copy ( getEdgeTemplatePath ( '../edge-shared' ) , join ( edgeFunctionRoot , 'edge-shared' ) )
206
208
await writeJSON ( join ( edgeFunctionRoot , 'edge-shared' , 'nextConfig.json' ) , nextConfig )
207
209
208
- if ( ! process . env . NEXT_DISABLE_EDGE_IMAGES ) {
210
+ if ( ! destr ( process . env . NEXT_DISABLE_EDGE_IMAGES ) && ! destr ( process . env . NEXT_DISABLE_NETLIFY_EDGE ) ) {
209
211
console . log (
210
212
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.' ,
211
213
)
@@ -221,14 +223,17 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
221
223
path : '/_next/image*' ,
222
224
} )
223
225
}
224
- if ( process . env . NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process . env . NEXT_DISABLE_NETLIFY_EDGE !== '1' ) {
226
+ if ( ! destr ( process . env . NEXT_DISABLE_NETLIFY_EDGE ) ) {
225
227
const middlewareManifest = await loadMiddlewareManifest ( netlifyConfig )
226
228
if ( ! middlewareManifest ) {
227
229
console . error ( "Couldn't find the middleware manifest" )
228
230
return
229
231
}
230
232
233
+ let usesEdge = false
234
+
231
235
for ( const middleware of middlewareManifest . sortedMiddleware ) {
236
+ usesEdge = true
232
237
const edgeFunctionDefinition = middlewareManifest . middleware [ middleware ]
233
238
const functionDefinitions = await writeEdgeFunction ( {
234
239
edgeFunctionDefinition,
@@ -241,6 +246,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
241
246
// No, the version field was not incremented
242
247
if ( typeof middlewareManifest . functions === 'object' ) {
243
248
for ( const edgeFunctionDefinition of Object . values ( middlewareManifest . functions ) ) {
249
+ usesEdge = true
244
250
const functionDefinitions = await writeEdgeFunction ( {
245
251
edgeFunctionDefinition,
246
252
edgeFunctionRoot,
@@ -249,6 +255,12 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
249
255
manifest . functions . push ( ...functionDefinitions )
250
256
}
251
257
}
258
+ if ( usesEdge ) {
259
+ console . log ( outdent `
260
+ ✨ Deploying middleware and functions to ${ greenBright `Netlify Edge Functions` } ✨
261
+ This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
262
+ ` )
263
+ }
252
264
}
253
265
await writeJson ( join ( edgeFunctionRoot , 'manifest.json' ) , manifest )
254
266
}
0 commit comments