Skip to content

fix: correctly enable edge images #1631

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 29 additions & 40 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@netlify/ipx": "^1.2.5",
"@vercel/node-bridge": "^2.1.0",
"chalk": "^4.1.2",
"destr": "^1.1.1",
"execa": "^5.1.1",
"fs-extra": "^10.0.0",
"globby": "^11.0.4",
Expand Down Expand Up @@ -63,4 +64,4 @@
"engines": {
"node": ">=12.0.0"
}
}
}
20 changes: 16 additions & 4 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ import { promises as fs, existsSync } from 'fs'
import { resolve, join } from 'path'

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

import { getRequiredServerFiles } from './config'

Expand Down Expand Up @@ -198,14 +201,13 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
const edgeFunctionRoot = resolve('.netlify', 'edge-functions')
await emptyDir(edgeFunctionRoot)

await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))

const { publish } = netlifyConfig.build
const nextConfigFile = await getRequiredServerFiles(publish)
const nextConfig = nextConfigFile.config
await copy(getEdgeTemplatePath('../edge-shared'), join(edgeFunctionRoot, 'edge-shared'))
await writeJSON(join(edgeFunctionRoot, 'edge-shared', 'nextConfig.json'), nextConfig)

if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
if (!destr(process.env.NEXT_DISABLE_EDGE_IMAGES) && !destr(process.env.NEXT_DISABLE_NETLIFY_EDGE)) {
console.log(
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
)
Expand All @@ -221,14 +223,17 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
path: '/_next/image*',
})
}
if (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1') {
if (!destr(process.env.NEXT_DISABLE_NETLIFY_EDGE)) {
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
if (!middlewareManifest) {
console.error("Couldn't find the middleware manifest")
return
}

let usesEdge = false

for (const middleware of middlewareManifest.sortedMiddleware) {
usesEdge = true
const edgeFunctionDefinition = middlewareManifest.middleware[middleware]
const functionDefinitions = await writeEdgeFunction({
edgeFunctionDefinition,
Expand All @@ -241,6 +246,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
// No, the version field was not incremented
if (typeof middlewareManifest.functions === 'object') {
for (const edgeFunctionDefinition of Object.values(middlewareManifest.functions)) {
usesEdge = true
const functionDefinitions = await writeEdgeFunction({
edgeFunctionDefinition,
edgeFunctionRoot,
Expand All @@ -249,6 +255,12 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
manifest.functions.push(...functionDefinitions)
}
}
if (usesEdge) {
console.log(outdent`
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`} ✨
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
`)
}
}
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
}
Expand Down
Loading