Skip to content

Commit 68710db

Browse files
committed
Merge branch 'main' into mk/edge-dev
2 parents 6748bd4 + 9578fa9 commit 68710db

File tree

5 files changed

+49
-33
lines changed

5 files changed

+49
-33
lines changed

demos/middleware/netlify.toml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@ command = "npm run build"
33
publish = ".next"
44
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"
55

6-
# [[plugins]]
7-
# package = "../plugin-wrapper/"
6+
[build.environment]
7+
NEXT_USE_NETLIFY_EDGE = "true"
8+
9+
[[plugins]]
10+
package = "../plugin-wrapper/"
11+
812
# This is a fake plugin, that makes it run npm install
913
[[plugins]]
1014
package = "@netlify/plugin-local-install-core"

demos/server-components/netlify.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,6 @@ command = "npm run build"
33
publish = ".next"
44
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"
55

6-
[build.environment]
7-
NEXT_USE_NETLIFY_EDGE = "true"
8-
96
[[plugins]]
107
package = "../plugin-wrapper/"
118

packages/runtime/src/helpers/edge.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,9 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
161161
await emptyDir(edgeFunctionRoot)
162162

163163
if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
164-
if (!process.env.NEXT_USE_NETLIFY_EDGE) {
165-
console.log(
166-
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
167-
)
168-
}
164+
console.log(
165+
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
166+
)
169167
const edgeFunctionDir = join(edgeFunctionRoot, 'ipx')
170168
await ensureDir(edgeFunctionDir)
171169
await copyEdgeSourceFile({ edgeFunctionDir, file: 'ipx.ts', target: 'index.ts' })
@@ -178,7 +176,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
178176
path: '/_next/image*',
179177
})
180178
}
181-
if (process.env.NEXT_USE_NETLIFY_EDGE) {
179+
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
182180
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
183181
if (!middlewareManifest) {
184182
console.error("Couldn't find the middleware manifest")
@@ -210,9 +208,10 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
210208
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
211209
}
212210

213-
export const updateConfig = async (publish: string) => {
211+
export const enableEdgeInNextConfig = async (publish: string) => {
214212
const configFile = join(publish, 'required-server-files.json')
215213
const config = await readJSON(configFile)
214+
// This is for runtime in Next.js, rather than a build plugin setting
216215
config.config.env.NEXT_USE_NETLIFY_EDGE = 'true'
217216
await writeJSON(configFile, config)
218217
}

packages/runtime/src/helpers/files.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => {
6060
}
6161

6262
export const getMiddleware = async (publish: string): Promise<Array<string>> => {
63-
if (process.env.NEXT_USE_NETLIFY_EDGE) {
63+
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
6464
return []
6565
}
6666
const manifestPath = join(publish, 'server', 'middleware-manifest.json')

packages/runtime/src/index.ts

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/* eslint-disable max-lines */
22
import { join, relative } from 'path'
33

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

@@ -16,7 +16,7 @@ import {
1616
generateCustomHeaders,
1717
} from './helpers/config'
1818
import { onPreDev } from './helpers/dev'
19-
import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
19+
import { enableEdgeInNextConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
2020
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
2121
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
2222
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
@@ -81,6 +81,34 @@ const plugin: NetlifyPlugin & { onPreDev?: OnPreBuild; onDev?: OnPreBuild } = {
8181
},
8282
)
8383

84+
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
85+
86+
let usingEdge = false
87+
88+
if (Object.keys(middlewareManifest?.functions).length !== 0) {
89+
usingEdge = true
90+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
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+
`)
95+
}
96+
}
97+
98+
if (Object.keys(middlewareManifest?.middleware).length !== 0) {
99+
usingEdge = true
100+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
101+
console.log(
102+
redBright(outdent`
103+
You are using Next.js Middleware without Netlify Edge Functions.
104+
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
105+
It also disables advanced middleware features from @netlify/next
106+
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
107+
`),
108+
)
109+
}
110+
}
111+
84112
if (experimental.images) {
85113
experimentalRemotePatterns = experimental.images.remotePatterns || []
86114
}
@@ -139,27 +167,15 @@ const plugin: NetlifyPlugin & { onPreDev?: OnPreBuild; onDev?: OnPreBuild } = {
139167
buildId,
140168
})
141169

142-
// We call this even if we don't have edge functions enabled because we still use it for images
143-
await writeEdgeFunctions(netlifyConfig)
170+
if (usingEdge) {
171+
await writeEdgeFunctions(netlifyConfig)
172+
173+
await enableEdgeInNextConfig(publish)
144174

145-
if (process.env.NEXT_USE_NETLIFY_EDGE) {
146175
console.log(outdent`
147-
✨ Deploying to ${greenBright`Netlify Edge Functions`}
176+
✨ Deploying middleware and functions to ${greenBright`Netlify Edge Functions`}
148177
This feature is in beta. Please share your feedback here: https://ntl.fyi/next-netlify-edge
149178
`)
150-
await updateConfig(publish)
151-
}
152-
153-
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
154-
155-
if (!process.env.NEXT_USE_NETLIFY_EDGE && middlewareManifest?.sortedMiddleware?.length) {
156-
console.log(
157-
yellowBright(outdent`
158-
You are using Next.js Middleware without Netlify Edge Functions.
159-
This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering.
160-
To get the best performance and use Netlify Edge Functions, set the env var ${bold`NEXT_USE_NETLIFY_EDGE=true`}.
161-
`),
162-
)
163179
}
164180
},
165181

0 commit comments

Comments
 (0)