Skip to content

Commit 0bb9bf8

Browse files
committed
fix: correctly enable edge images
1 parent 43dc2b2 commit 0bb9bf8

File tree

2 files changed

+39
-41
lines changed

2 files changed

+39
-41
lines changed

packages/runtime/src/helpers/edge.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ import { promises as fs, existsSync } from 'fs'
33
import { resolve, join } from 'path'
44

55
import type { NetlifyConfig, NetlifyPluginConstants } from '@netlify/build'
6+
import { greenBright } from 'chalk'
67
import { copy, copyFile, emptyDir, ensureDir, readJSON, readJson, writeJSON, writeJson } from 'fs-extra'
78
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
89
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
10+
import { outdent } from 'outdent'
911

1012
// This is the format as of next@12.2
1113
interface EdgeFunctionDefinitionV1 {
@@ -196,8 +198,6 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
196198
const edgeFunctionRoot = resolve('.netlify', 'edge-functions')
197199
await emptyDir(edgeFunctionRoot)
198200

199-
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))
200-
201201
if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
202202
console.log(
203203
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
@@ -215,13 +215,18 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
215215
})
216216
}
217217
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
218+
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))
219+
218220
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
219221
if (!middlewareManifest) {
220222
console.error("Couldn't find the middleware manifest")
221223
return
222224
}
223225

226+
let usesEdge = false
227+
224228
for (const middleware of middlewareManifest.sortedMiddleware) {
229+
usesEdge = true
225230
const edgeFunctionDefinition = middlewareManifest.middleware[middleware]
226231
const functionDefinitions = await writeEdgeFunction({
227232
edgeFunctionDefinition,
@@ -234,6 +239,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
234239
// No, the version field was not incremented
235240
if (typeof middlewareManifest.functions === 'object') {
236241
for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
242+
usesEdge = true
237243
const functionDefinitions = await writeEdgeFunction({
238244
edgeFunctionDefinition,
239245
edgeFunctionRoot,
@@ -242,6 +248,12 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
242248
manifest.functions.push(...functionDefinitions)
243249
}
244250
}
251+
if (usesEdge) {
252+
console.log(outdent`
253+
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`}
254+
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
255+
`)
256+
}
245257
}
246258
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
247259
}

packages/runtime/src/index.ts

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { join, relative } from 'path'
33

44
import type { NetlifyPlugin } from '@netlify/build'
5-
import { greenBright, bold, redBright } from 'chalk'
5+
import { bold, redBright } from 'chalk'
66
import { existsSync, readFileSync } from 'fs-extra'
77
import { outdent } from 'outdent'
88

@@ -16,12 +16,7 @@ import {
1616
generateCustomHeaders,
1717
} from './helpers/config'
1818
import { onPreDev } from './helpers/dev'
19-
import {
20-
enableEdgeInNextConfig,
21-
writeEdgeFunctions,
22-
loadMiddlewareManifest,
23-
cleanupEdgeFunctions,
24-
} from './helpers/edge'
19+
import { writeEdgeFunctions, loadMiddlewareManifest, cleanupEdgeFunctions } from './helpers/edge'
2520
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
2621
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
2722
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
@@ -88,30 +83,30 @@ const plugin: NetlifyPlugin = {
8883

8984
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
9085

91-
let usingEdge = false
92-
93-
if (middlewareManifest?.functions && Object.keys(middlewareManifest.functions).length !== 0) {
94-
usingEdge = true
95-
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
96-
failBuild(outdent`
97-
You are using Next.js experimental edge runtime, but have set NEXT_DISABLE_NETLIFY_EDGE to true. This is not supported.
98-
To use edge runtime, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
99-
`)
100-
}
86+
if (
87+
middlewareManifest?.functions &&
88+
Object.keys(middlewareManifest.functions).length !== 0 &&
89+
process.env.NEXT_DISABLE_NETLIFY_EDGE
90+
) {
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+
`)
10195
}
10296

103-
if (middlewareManifest?.middleware && Object.keys(middlewareManifest.middleware).length !== 0) {
104-
usingEdge = true
105-
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
106-
console.log(
107-
redBright(outdent`
108-
You are using Next.js Middleware without Netlify Edge Functions.
109-
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
110-
It also disables advanced middleware features from @netlify/next
111-
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
112-
`),
113-
)
114-
}
97+
if (
98+
middlewareManifest?.middleware &&
99+
Object.keys(middlewareManifest.middleware).length !== 0 &&
100+
process.env.NEXT_DISABLE_NETLIFY_EDGE
101+
) {
102+
console.log(
103+
redBright(outdent`
104+
You are using Next.js Middleware without Netlify Edge Functions.
105+
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
106+
It also disables advanced middleware features from @netlify/next
107+
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
108+
`),
109+
)
115110
}
116111

117112
if (isNextAuthInstalled()) {
@@ -178,16 +173,7 @@ const plugin: NetlifyPlugin = {
178173
buildId,
179174
})
180175

181-
if (usingEdge) {
182-
await writeEdgeFunctions(netlifyConfig)
183-
184-
await enableEdgeInNextConfig(publish)
185-
186-
console.log(outdent`
187-
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`}
188-
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
189-
`)
190-
}
176+
await writeEdgeFunctions(netlifyConfig)
191177
},
192178

193179
async onPostBuild({

0 commit comments

Comments
 (0)