-
Notifications
You must be signed in to change notification settings - Fork 86
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
feat: add edge middleware support to ntl dev
#1546
Conversation
✅ Deploy Preview for netlify-plugin-nextjs-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-export-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-hp-edge-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-rsc-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-i18next-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-edge-middleware ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for next-plugin-canary ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for nextjs-plugin-custom-routes-demo ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
@@ -58,6 +55,16 @@ export async function middleware(req: NextRequest) { | |||
response.headers.set('x-modified-in-rewrite', 'true') | |||
} | |||
|
|||
if (pathname.startsWith('/shows/redirectme')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redirects weren't working, so this is a test for the fix
@@ -58,6 +55,16 @@ export async function middleware(req: NextRequest) { | |||
response.headers.set('x-modified-in-rewrite', 'true') | |||
} | |||
|
|||
if (pathname.startsWith('/shows/redirectme')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redirects were broken, so these are the added tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Didn't go deep into the Next.js parts, but LGTM!
_inputs, | ||
meta: { events?: Set<string> } = {}, | ||
): NetlifyPlugin & { onPreDev?: NetlifyPlugin['onPreBuild'] } => { | ||
if (!meta?.events?.has('onPreDev')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great touch!
import { writeDevEdgeFunction } from './edge' | ||
import { patchNextFiles } from './files' | ||
|
||
// The types haven't been updated yet |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, good point.
Summary
Adds support for running middleware using edge functions with
ntl dev
.When
next dev
is run, it doesn't emit any of the compiled files that we need to run middleware via our normal edge function. This means that currently we need to runntl build
beforentl dev
, and changes would require a recompiled. This is not good DX: we want to be able to runntl dev
without building or linking the site, and have it update middleware when it is changed.In this PR this is implemented with a special dev-only edge function which runs the middleware directly, rather than via the Next server. While importing the middleware.ts file directly works in simple cases, it breaks if the middleware imports anything else, because the imports won't be Deno-compatible. We handle this by running
esbuild --watch
on the middleware, and importing the compiled middleware. It still works if the middleware hasn't been created yet because esbuild will compile it when it is.The wrapper edge function checks at request time whether the compiled middleware exists. If not, it just silently returns as there is no middleware. If it does exist then it dynamically imports it, then executes it. This means that it will always have the latest version without needing to reload the edge function.
The edge function is generated in a new
onDev
hook, which also spawnsesbuild
. A regular build will delete the dev edge function to ensure it's never deployed, and the dev hook deletes the production edge functions to ensure they don't conflict.This needs e2e tests, which will follow later: #1553
Test plan
demos/middleware/netlify.toml
to switch plugin package namesnpm i
andntl dev
in middleware demoRelevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal
Standard checks:
🧪 Once merged, make sure to update the version if needed and that it was published correctly.