Skip to content

Commit 3c56eda

Browse files
authored
feat: middleware warning (#1425)
* feat: add warning about middleware without edge * chore: modify readme to include info about edge * chore: reword * chore: readme and formatting review fixes * chore: changes from Matts reviews * chore: changing to american spelling
1 parent b56de8f commit 3c56eda

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

README.md

+22-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ package = "@netlify/plugin-nextjs"
3434
## Deploying
3535

3636
If you build on Netlify, this plugin will work with no additional configuration. However if you are building and
37-
deploying locally using the Netlify CLI, you must deploy using `netlify deploy --build`. Running the
38-
build and deploy commands separately will not work, because the plugin will not generate the required configuration.
37+
deploying locally using the Netlify CLI, you must deploy using `netlify deploy --build`. Running the build and deploy
38+
commands separately will not work, because the plugin will not generate the required configuration.
3939

4040
## Migrating from an older version of the plugin
4141

@@ -67,9 +67,26 @@ If you currently use redirects or rewrites on your site, see
6767
for information on changes to how they are handled in this version. In particular, note that `_redirects` and `_headers`
6868
files must be placed in `public`, not in the root of the site.
6969

70-
If you want to use Next 12's beta Middleware feature, this will mostly work as expected but please
71-
[read the docs on some caveats and workarounds](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/middleware.md)
72-
that are currently needed.
70+
## Next.js Middleware on Netlify
71+
72+
Next.js Middleware works out of the box on Netlify, but check out the
73+
[docs on some caveats](https://github.com/netlify/netlify-plugin-nextjs/blob/main/docs/middleware.md). By default,
74+
middleware runs using SSR. For better results, you should enable [Netlify Edge Functions](#netlify-edge-functions),
75+
which ensures middleware runs at the edge.
76+
77+
### No nested middleware in Next 12.2.0
78+
79+
In Next 12.2.0, nested middleware [has been deprecated](https://nextjs.org/docs/messages/middleware-upgrade-guide) in
80+
favor of root level middleware. If you are not using edge functions then this means that you won't get the benefits of
81+
using a CDN, and ISR will not work.
82+
83+
To fix this issue, you can run your middleware on [Netlify Edge Functions](#netlify-edge-functions).
84+
85+
## Netlify Edge Functions
86+
87+
To use Netlify Edge Functions for middleware or to enable
88+
[edge server rendering](https://nextjs.org/blog/next-12-2#edge-server-rendering-experimental), set the environment
89+
variable `NEXT_USE_NETLIFY_EDGE` to `true`.
7390

7491
## Monorepos
7592

plugin/src/helpers/edge.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export interface FunctionManifest {
2121
>
2222
}
2323

24-
const loadMiddlewareManifest = (netlifyConfig: NetlifyConfig): Promise<MiddlewareManifest | null> => {
24+
export const loadMiddlewareManifest = (netlifyConfig: NetlifyConfig): Promise<MiddlewareManifest | null> => {
2525
const middlewarePath = resolve(netlifyConfig.build.publish, 'server', 'middleware-manifest.json')
2626
if (!existsSync(middlewarePath)) {
2727
return null

plugin/src/index.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { join, relative } from 'path'
33

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

@@ -15,7 +15,7 @@ import {
1515
configureHandlerFunctions,
1616
generateCustomHeaders,
1717
} from './helpers/config'
18-
import { updateConfig, writeEdgeFunctions } from './helpers/edge'
18+
import { updateConfig, writeEdgeFunctions, loadMiddlewareManifest } from './helpers/edge'
1919
import { moveStaticPages, movePublicFiles, patchNextFiles, unpatchNextFiles } from './helpers/files'
2020
import { generateFunctions, setupImageFunction, generatePagesResolver } from './helpers/functions'
2121
import { generateRedirects, generateStaticRedirects } from './helpers/redirects'
@@ -145,6 +145,18 @@ const plugin: NetlifyPlugin = {
145145
await writeEdgeFunctions(netlifyConfig)
146146
await updateConfig(publish)
147147
}
148+
149+
const middlewareManifest = await loadMiddlewareManifest(netlifyConfig)
150+
151+
if (!process.env.NEXT_USE_NETLIFY_EDGE && middlewareManifest.sortedMiddleware.length !== 0) {
152+
console.log(
153+
yellowBright(outdent`
154+
You are using Next.js Middleware without Netlify Edge Functions.
155+
This will soon be deprecated because it negatively affects performance and will disable ISR and static rendering.
156+
To get the best performance and use Netlify Edge Functions, set the env var ${bold`NEXT_USE_NETLIFY_EDGE=true`}.
157+
`),
158+
)
159+
}
148160
},
149161

150162
async onPostBuild({

0 commit comments

Comments
 (0)