Skip to content

Commit 8bcbad0

Browse files
authored
fix: correctly enable edge images (#1631)
* fix: correctly enable edge images * chore: ensure ef share dir exists * chore: copy shared ef files before writing next config * chore: add tests for image ef * chore: use destr for env var flags
1 parent bec881b commit 8bcbad0

File tree

5 files changed

+97
-85
lines changed

5 files changed

+97
-85
lines changed

package-lock.json

+29-40
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/runtime/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"@netlify/ipx": "^1.2.5",
1616
"@vercel/node-bridge": "^2.1.0",
1717
"chalk": "^4.1.2",
18+
"destr": "^1.1.1",
1819
"execa": "^5.1.1",
1920
"fs-extra": "^10.0.0",
2021
"globby": "^11.0.4",
@@ -63,4 +64,4 @@
6364
"engines": {
6465
"node": ">=12.0.0"
6566
}
66-
}
67+
}

packages/runtime/src/helpers/edge.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ 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'
7+
import destr from 'destr'
68
import { copy, copyFile, emptyDir, ensureDir, readJSON, readJson, writeJSON, writeJson } from 'fs-extra'
79
import type { MiddlewareManifest } from 'next/dist/build/webpack/plugins/middleware-plugin'
810
import type { RouteHas } from 'next/dist/lib/load-custom-routes'
11+
import { outdent } from 'outdent'
912

1013
import { 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

Comments
 (0)