Skip to content

feat: default edge middleware to on #1547

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 4 commits into from
Aug 18, 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
3 changes: 0 additions & 3 deletions demos/middleware/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ command = "npm run build"
publish = ".next"
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"

[build.environment]
NEXT_USE_NETLIFY_EDGE = "true"

[[plugins]]
package = "../plugin-wrapper/"

Expand Down
3 changes: 0 additions & 3 deletions demos/server-components/netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ command = "npm run build"
publish = ".next"
ignore = "if [ $CACHED_COMMIT_REF == $COMMIT_REF ]; then (exit 1); else git diff --quiet $CACHED_COMMIT_REF $COMMIT_REF ../..; fi;"

[build.environment]
NEXT_USE_NETLIFY_EDGE = "true"

[[plugins]]
package = "../plugin-wrapper/"

Expand Down
13 changes: 6 additions & 7 deletions packages/runtime/src/helpers/edge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,9 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
await emptyDir(edgeFunctionRoot)

if (!process.env.NEXT_DISABLE_EDGE_IMAGES) {
if (!process.env.NEXT_USE_NETLIFY_EDGE) {
console.log(
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
)
}
console.log(
'Using Netlify Edge Functions for image format detection. Set env var "NEXT_DISABLE_EDGE_IMAGES=true" to disable.',
)
const edgeFunctionDir = join(edgeFunctionRoot, 'ipx')
await ensureDir(edgeFunctionDir)
await copyEdgeSourceFile({ edgeFunctionDir, file: 'ipx.ts', target: 'index.ts' })
Expand All @@ -152,7 +150,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
path: '/_next/image*',
})
}
if (process.env.NEXT_USE_NETLIFY_EDGE) {
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
if (!middlewareManifest) {
console.error("Couldn't find the middleware manifest")
Expand Down Expand Up @@ -184,9 +182,10 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
await writeJson(join(edgeFunctionRoot, 'manifest.json'), manifest)
}

export const updateConfig = async (publish: string) => {
export const enableEdgeInNextConfig = async (publish: string) => {
const configFile = join(publish, 'required-server-files.json')
const config = await readJSON(configFile)
// This is for runtime in Next.js, rather than a build plugin setting
config.config.env.NEXT_USE_NETLIFY_EDGE = 'true'
await writeJSON(configFile, config)
}
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/helpers/files.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const matchesRewrite = (file: string, rewrites: Rewrites): boolean => {
}

export const getMiddleware = async (publish: string): Promise<Array<string>> => {
if (process.env.NEXT_USE_NETLIFY_EDGE) {
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
return []
}
const manifestPath = join(publish, 'server', 'middleware-manifest.json')
Expand Down
54 changes: 35 additions & 19 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { join, relative } from 'path'

import type { NetlifyPlugin } from '@netlify/build'
import { greenBright, yellowBright, bold } from 'chalk'
import { greenBright, bold, redBright } from 'chalk'
import { existsSync, readFileSync } from 'fs-extra'
import { outdent } from 'outdent'

Expand All @@ -15,7 +15,7 @@ import {
configureHandlerFunctions,
generateCustomHeaders,
} from './helpers/config'
import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
import { enableEdgeInNextConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
import { moveStaticPages, movePublicFiles, patchNextFiles } from './helpers/files'
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
Expand Down Expand Up @@ -80,6 +80,34 @@ const plugin: NetlifyPlugin = {
},
)

const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)

let usingEdge = false

if (Object.keys(middlewareManifest?.functions).length !== 0) {
usingEdge = true
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
failBuild(outdent`
You are using Next.js experimental edge runtime, but have set NEXT_DISABLE_NETLIFY_EDGE to true. This is not supported.
To use edge runtime, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
`)
}
}

if (Object.keys(middlewareManifest?.middleware).length !== 0) {
usingEdge = true
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
console.log(
redBright(outdent`
You are using Next.js Middleware without Netlify Edge Functions.
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
It also disables advanced middleware features from @netlify/next
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`}.
`),
)
}
}

if (experimental.images) {
experimentalRemotePatterns = experimental.images.remotePatterns || []
}
Expand Down Expand Up @@ -138,27 +166,15 @@ const plugin: NetlifyPlugin = {
buildId,
})

// We call this even if we don't have edge functions enabled because we still use it for images
await writeEdgeFunctions(netlifyConfig)
if (usingEdge) {
await writeEdgeFunctions(netlifyConfig)

await enableEdgeInNextConfig(publish)

if (process.env.NEXT_USE_NETLIFY_EDGE) {
console.log(outdent`
✨ Deploying to ${greenBright`Netlify Edge Functions`} ✨
✨ 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 updateConfig(publish)
}

const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)

if (!process.env.NEXT_USE_NETLIFY_EDGE && middlewareManifest?.sortedMiddleware?.length) {
console.log(
yellowBright(outdent`
You are using Next.js Middleware without Netlify Edge Functions.
This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering.
To get the best performance and use Netlify Edge Functions, set the env var ${bold`NEXT_USE_NETLIFY_EDGE=true`}.
`),
)
}
},

Expand Down