Skip to content
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

Merged
merged 14 commits into from
Aug 18, 2022
Merged

feat: add edge middleware support to ntl dev #1546

merged 14 commits into from
Aug 18, 2022

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Aug 17, 2022

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 run ntl build before ntl dev, and changes would require a recompiled. This is not good DX: we want to be able to run ntl 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 spawns esbuild. 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

  1. Check out repo
  2. Run npm i in root
  3. Install latest version of CLI once 11.0.0 is released
  4. Edit demos/middleware/netlify.toml to switch plugin package names
  5. Run npm i and ntl dev in middleware demo
  6. Load site and check the "rewrite static" demo. You should see a cat and your city.

Relevant links (GitHub issues, Notion docs, etc.) or a picture of cute animal

capy lifeguard

Standard checks:

  • Check the Deploy Preview's Demo site for your PR's functionality
  • Add docs when necessary

🧪 Once merged, make sure to update the version if needed and that it was published correctly.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for netlify-plugin-nextjs-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/62fe1fe5b7221200096d61ea
😎 Deploy Preview https://deploy-preview-1546--netlify-plugin-nextjs-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for netlify-plugin-nextjs-export-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/62fe1fe513f3b400089cc092
😎 Deploy Preview https://deploy-preview-1546--netlify-plugin-nextjs-export-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for netlify-plugin-nextjs-nx-monorepo-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/62fe1fe503377700087612a7
😎 Deploy Preview https://deploy-preview-1546--netlify-plugin-nextjs-nx-monorepo-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for next-hp-edge-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/next-hp-edge-demo/deploys/62fe1fe63070b800091e3355
😎 Deploy Preview https://deploy-preview-1546--next-hp-edge-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@github-actions github-actions bot added the type: feature code contributing to the implementation of a feature and/or user facing functionality label Aug 17, 2022
@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for next-plugin-rsc-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-rsc-demo/deploys/62fe1fe552caa500082a39d0
😎 Deploy Preview https://deploy-preview-1546--next-plugin-rsc-demo.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for netlify-plugin-nextjs-next-auth-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/62fe1fe53070b800091e3350
😎 Deploy Preview https://deploy-preview-1546--netlify-plugin-nextjs-next-auth-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/62fe1fe50e5c5d000722ea49
😎 Deploy Preview https://deploy-preview-1546--next-i18next-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for next-plugin-edge-middleware ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/62fe1fe55bf26100095e73bb
😎 Deploy Preview https://deploy-preview-1546--next-plugin-edge-middleware.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for netlify-plugin-nextjs-static-root-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/62fe1fe541081200097f04de
😎 Deploy Preview https://deploy-preview-1546--netlify-plugin-nextjs-static-root-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/62fe1fe5669717000834db9b
😎 Deploy Preview https://deploy-preview-1546--next-plugin-canary.netlify.app/
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@netlify
Copy link

netlify bot commented Aug 17, 2022

Deploy Preview for nextjs-plugin-custom-routes-demo ready!

Name Link
🔨 Latest commit fc0c68b
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/62fe1fe5c6325c000929c1ad
😎 Deploy Preview https://deploy-preview-1546--nextjs-plugin-custom-routes-demo.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

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')) {
Copy link
Contributor Author

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')) {
Copy link
Contributor Author

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

@ascorbic ascorbic marked this pull request as ready for review August 18, 2022 11:23
@ascorbic ascorbic requested a review from a team August 18, 2022 11:23
Copy link
Contributor

@eduardoboucas eduardoboucas left a 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')) {
Copy link
Contributor

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, good point.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: feature code contributing to the implementation of a feature and/or user facing functionality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants