@@ -3,9 +3,12 @@ import { promises as fs, existsSync } from 'fs'
33import { resolve , join } from 'path'
44
55import type { NetlifyConfig , NetlifyPluginConstants } from '@netlify/build'
6+ import { greenBright } from 'chalk'
7+ import destr from 'destr'
68import { copy , copyFile , emptyDir , ensureDir , readJSON , readJson , writeJSON , writeJson } from 'fs-extra'
79import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
810import type { RouteHas } from 'next/dist/lib/load-custom-routes'
11+ import { outdent } from 'outdent'
912
1013import { getRequiredServerFiles } from './config'
1114
@@ -198,14 +201,13 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
198201 const edgeFunctionRoot = resolve ( '.netlify' , 'edge-functions' )
199202 await emptyDir ( edgeFunctionRoot )
200203
201- await copy ( getEdgeTemplatePath ( '../edge-shared' ) , join ( edgeFunctionRoot , 'edge-shared' ) )
202-
203204 const { publish } = netlifyConfig . build
204205 const nextConfigFile = await getRequiredServerFiles ( publish )
205206 const nextConfig = nextConfigFile . config
207+ await copy ( getEdgeTemplatePath ( '../edge-shared' ) , join ( edgeFunctionRoot , 'edge-shared' ) )
206208 await writeJSON ( join ( edgeFunctionRoot , 'edge-shared' , 'nextConfig.json' ) , nextConfig )
207209
208- if ( ! process . env . NEXT_DISABLE_EDGE_IMAGES ) {
210+ if ( ! destr ( process . env . NEXT_DISABLE_EDGE_IMAGES ) && ! destr ( process . env . NEXT_DISABLE_NETLIFY_EDGE ) ) {
209211 console . log (
210212 'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.' ,
211213 )
@@ -221,14 +223,17 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
221223 path : '/_next/image*' ,
222224 } )
223225 }
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 ) ) {
225227 const middlewareManifest = await loadMiddlewareManifest ( netlifyConfig )
226228 if ( ! middlewareManifest ) {
227229 console . error ( "Couldn't find the middleware manifest" )
228230 return
229231 }
230232
233+ let usesEdge = false
234+
231235 for ( const middleware of middlewareManifest . sortedMiddleware ) {
236+ usesEdge = true
232237 const edgeFunctionDefinition = middlewareManifest . middleware [ middleware ]
233238 const functionDefinitions = await writeEdgeFunction ( {
234239 edgeFunctionDefinition,
@@ -241,6 +246,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
241246 // No, the version field was not incremented
242247 if ( typeof middlewareManifest . functions === 'object' ) {
243248 for ( const edgeFunctionDefinition of Object . values ( middlewareManifest . functions ) ) {
249+ usesEdge = true
244250 const functionDefinitions = await writeEdgeFunction ( {
245251 edgeFunctionDefinition,
246252 edgeFunctionRoot,
@@ -249,6 +255,12 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
249255 manifest . functions . push ( ...functionDefinitions )
250256 }
251257 }
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+ }
252264 }
253265 await writeJson ( join ( edgeFunctionRoot , 'manifest.json' ) , manifest )
254266}
0 commit comments