Skip to content

Commit 5cf8c52

Browse files
charkourascorbickodiakhq[bot]
authored
fix: check NEXT_DISABLE_NETLIFY_EDGE value rather than if truthy (#1603)
* fix: check NEXT_DISABLE_NETLIFY_EDGE value rather than if truthy * fix: convert to string compare * fix: allow unset var behavior to remain * chore(docs): update middleware docs * fix: update logic Co-authored-by: Matt Kane <[email protected]> Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 18c594f commit 5cf8c52

File tree

6 files changed

+21
-23
lines changed

6 files changed

+21
-23
lines changed

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ commands separately will not work, because the Next.js Runtime will not generate
1616
If you use [`next/image`](https://nextjs.org/docs/basic-features/image-optimization), your images will be automatically
1717
optimized at runtime, ensuring that they are served at the best size and format. The image will be processed on the
1818
first request which means it may take longer to load, but the generated image is then cached at the edge and served as a
19-
static file to future visitors. By default, Next will deliver WebP images if the browser supports it. WebP is a new
19+
static file to future visitors. By default, Next.js will deliver WebP images if the browser supports it. WebP is a new
2020
image format with wide browser support that will usually generate smaller files than png or jpg. You can additionally
2121
enable the AVIF format, which is often even smaller in filesize than WebP. The drawback is that with particularly large
2222
images AVIF may take too long to generate, meaning the function times-out. You can configure
@@ -48,6 +48,8 @@ Next.js Middleware works out of the box on Netlify. By default, middleware runs
4848
support for running Middleware at the origin, set the environment variable `NEXT_DISABLE_NETLIFY_EDGE` to `true`. Be
4949
aware that this will result in slower performance, as all pages that match middleware must use SSR.
5050

51+
For more details on Next.js Middleware with Netlify, see the [middleware docs](https://github.com/netlify/next-runtime/blob/main/docs/middleware.md).
52+
5153
## Monorepos
5254

5355
If you are using a monorepo you will need to change `publish` to point to the full path to the built `.next` directory,
@@ -111,7 +113,7 @@ If you previously set these values, they're no longer needed and should be remov
111113
- `external_node_modules` in `netlify.toml`
112114
- The environment variable `NEXT_USE_NETLIFY_EDGE` can be removed as this is now the default
113115

114-
The `serverless` and `experimental-serverless-trace` targets are deprecated in Next 12, and all builds with this Next
116+
The `serverless` and `experimental-serverless-trace` targets are deprecated in Next.js 12, and all builds with this Next.js
115117
Runtime will now use the default `server` target. If you previously set the target in your `next.config.js`, you should
116118
remove it.
117119

docs/middleware.md

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
# Using Next 12 middleware on Netlify
1+
# Using Next.js 12 middleware on Netlify
22

3-
Next 12 introduces a new feature called [Middleware](https://nextjs.org/docs/middleware), in which functions run before
3+
Next.js 12 introduces a new feature called [Middleware](https://nextjs.org/docs/middleware), in which functions run before
44
a request has finished processing. Middleware can be used to modify the request or replace the response. For example, it
55
can change headers, rewrite the request path, or return a different response entirely.
66

77
Next.js Middleware can run either in an edge function (highly recommended for version 12.2+ as ISR will not work
88
otherwise) or at the origin. On Netlify, Middleware runs at the origin as part of the normal Next.js server.
99

10-
If you'd like to run Middleware at the edge, set the environment variable `NEXT_USE_NETLIFY_EDGE` to `true`.
10+
## How to deploy Next.js 12 middleware
1111

12-
## How to deploy Next 12 middleware
13-
14-
Next 12 Middleware works out of the box with Netlify, and most functions will work unchanged. See
15-
[the middleware docs](https://nextjs.org/docs/middleware) for details of how to create them. There are however a few
16-
workarounds that are currently required for some features during the beta period:
12+
Next.js 12 Middleware works out of the box with Netlify, and most functions will work unchanged. By default, middleware runs using Netlify Edge Functions. For legacy support for running Middleware at the origin, set the environment variable `NEXT_DISABLE_NETLIFY_EDGE` to `true`. Be aware that this will result in slower performance, as all pages that match middleware must use SSR. See [the middleware docs](https://nextjs.org/docs/middleware) for details of how to create Next.js middleware. There are however a few workarounds that are currently required for some features during the beta period:
1713

1814
### `geo`
1915

docs/release-notes/v4.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ stability.
99
[incremental static regeneration (ISR)](https://github.com/netlify/next-runtime/blob/main/docs/isr.md).
1010
- Full support for
1111
[Next.js rewrites, redirects and headers](https://github.com/netlify/next-runtime/blob/main/docs/redirects-rewrites.md).
12-
- Beta support for [Next 12 Middleware](https://github.com/netlify/next-runtime/blob/main/docs/middleware.md)
12+
- Beta support for [Next.js 12 Middleware](https://github.com/netlify/next-runtime/blob/main/docs/middleware.md)
1313
- Faster builds and deploys. Instead of generating one function per route, there are just three functions per site and a
1414
much smaller list of rewrites.
1515
- Full support for Netlify's new [persistent On-Demand Builders](https://ntl.fyi/odb). Return `fallback: "blocking"`
@@ -20,7 +20,7 @@ stability.
2020
- Simplified configuration. You no longer need to set any Netlify-specific configuration options. For example, in a
2121
monorepo all you need to do is set `publish` to point to your `.next` directory and you can build the site in any way
2222
you like.
23-
- Removes requirement for the `target` to be set to `serverless`, which is deprecated in Next 12.
23+
- Removes requirement for the `target` to be set to `serverless`, which is deprecated in Next.js 12.
2424
- Bundling now uses Next.js's own node-file-trace, giving more predictable results and smaller uploads.
2525

2626
## Migration guide
@@ -45,6 +45,6 @@ If you currently use redirects or rewrites on your site, see
4545
information on changes to how they are handled in this version. If you currently have `_redirects` or `_headers` files
4646
in the root of your site, these will need to be moved into "public".
4747

48-
If you want to use Next 12's beta Middleware feature, this will mostly work as expected but please
48+
If you want to use Next.js 12's beta Middleware feature, this will mostly work as expected but please
4949
[read the docs on some caveats and workarounds](https://github.com/netlify/next-runtime/blob/main/docs/middleware.md)
5050
that are currently needed.

packages/runtime/src/helpers/edge.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ export const writeEdgeFunctions = async (netlifyConfig: NetlifyConfig) => {
214214
path: '/_next/image*',
215215
})
216216
}
217-
if (!process.env.NEXT_DISABLE_NETLIFY_EDGE) {
217+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1') {
218218
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
219219
if (!middlewareManifest) {
220220
console.error("Couldn't find the middleware manifest")

packages/runtime/src/helpers/files.ts

Lines changed: 5 additions & 5 deletions
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_DISABLE_NETLIFY_EDGE) {
63+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1') {
6464
return []
6565
}
6666
const manifestPath = join(publish, 'server', 'middleware-manifest.json')
@@ -346,19 +346,19 @@ const baseServerReplacements: Array<[string, string]> = [
346346
const nextServerReplacements: Array<[string, string]> = [
347347
[
348348
`getMiddlewareManifest() {\n if (this.minimalMode) return null;`,
349-
`getMiddlewareManifest() {\n if (this.minimalMode || !process.env.NEXT_DISABLE_NETLIFY_EDGE) return null;`,
349+
`getMiddlewareManifest() {\n if (this.minimalMode || (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1')) return null;`,
350350
],
351351
[
352352
`generateCatchAllMiddlewareRoute(devReady) {\n if (this.minimalMode) return []`,
353-
`generateCatchAllMiddlewareRoute(devReady) {\n if (this.minimalMode || !process.env.NEXT_DISABLE_NETLIFY_EDGE) return [];`,
353+
`generateCatchAllMiddlewareRoute(devReady) {\n if (this.minimalMode || (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1')) return [];`,
354354
],
355355
[
356356
`generateCatchAllMiddlewareRoute() {\n if (this.minimalMode) return undefined;`,
357-
`generateCatchAllMiddlewareRoute() {\n if (this.minimalMode || !process.env.NEXT_DISABLE_NETLIFY_EDGE) return undefined;`,
357+
`generateCatchAllMiddlewareRoute() {\n if (this.minimalMode || (process.env.NEXT_DISABLE_NETLIFY_EDGE !== 'true' && process.env.NEXT_DISABLE_NETLIFY_EDGE !== '1')) return undefined;`,
358358
],
359359
[
360360
`getMiddlewareManifest() {\n if (this.minimalMode) {`,
361-
`getMiddlewareManifest() {\n if (!this.minimalMode && process.env.NEXT_DISABLE_NETLIFY_EDGE) {`,
361+
`getMiddlewareManifest() {\n if (!this.minimalMode && (process.env.NEXT_DISABLE_NETLIFY_EDGE === 'true' || process.env.NEXT_DISABLE_NETLIFY_EDGE === '1')) {`,
362362
],
363363
]
364364

packages/runtime/src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,23 @@ const plugin: NetlifyPlugin = {
9292

9393
if (middlewareManifest?.functions && Object.keys(middlewareManifest.functions).length !== 0) {
9494
usingEdge = true
95-
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
95+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE === 'true' || process.env.NEXT_DISABLE_NETLIFY_EDGE === '1') {
9696
failBuild(outdent`
9797
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`}.
98+
To use edge runtime, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`} or set it to false.
9999
`)
100100
}
101101
}
102102

103103
if (middlewareManifest?.middleware && Object.keys(middlewareManifest.middleware).length !== 0) {
104104
usingEdge = true
105-
if (process.env.NEXT_DISABLE_NETLIFY_EDGE) {
105+
if (process.env.NEXT_DISABLE_NETLIFY_EDGE === 'true' || process.env.NEXT_DISABLE_NETLIFY_EDGE === '1') {
106106
console.log(
107107
redBright(outdent`
108108
You are using Next.js Middleware without Netlify Edge Functions.
109109
This is deprecated because it negatively affects performance and will disable ISR and static rendering.
110110
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`}.
111+
To get the best performance and use Netlify Edge Functions, remove the env var ${bold`NEXT_DISABLE_NETLIFY_EDGE`} or set it to false.
112112
`),
113113
)
114114
}

0 commit comments

Comments
 (0)