Skip to content

fix: use separate watcher script for middleware in dev #1831

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 32 commits into from
Mar 21, 2023
Merged

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Dec 5, 2022

Summary

Currently we build middleware in dev by spawning two different esbuild instances, each watching for either middleware.ts or middleware.js. This was always a bit of a hack, and was done because esbuild --watch doesn't compile unless all passed files are available. Now that middleware can also be in src, this would require four instances, which is getting silly. The plan was always to move to a dedicated watcher script and use the esbuild API instead. This PR does that.

It uses chokidar to watch for the four valid middleware filenames, and uses esbuild to build them if they change or are created. It deletes the output file if it's removed, and refuses to build if there are more than one.

The big missing part is the same as the current version: there is no way of seeing the build output. There is no way around this that I can see, because of the way netlify/build spawns the plugin commands.

Test plan

  1. Locally, run ntl dev in the middleware demo. Try renaming the middleware.ts file and see .netlify/middleware.js disappear. Try creating middleware.js instead. Try moving it into src.

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

Fixes #1824

image

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.

@ascorbic ascorbic requested a review from a team December 5, 2022 12:18
@netlify
Copy link

netlify bot commented Dec 5, 2022

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/6419ffe83e8783000712a917
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/6419ffe84727410008d32709
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/6419ffe849fab30008be7486
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/6419ffe8c51df6000894111e
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/6419ffe817da200008af26c9
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/6419ffe895f710000816389c
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/6419ffe89fc06a000857247c
😎 Deploy Preview https://deploy-preview-1831--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.

@netlify
Copy link

netlify bot commented Dec 5, 2022

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/6419ffe8dc8c9e000858c79b
😎 Deploy Preview https://deploy-preview-1831--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 Dec 5, 2022

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

Name Link
🔨 Latest commit 7bbf3ed
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/6392423e929d4b0009c49062
😎 Deploy Preview https://deploy-preview-1831--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.

Copy link
Contributor

@orinokai orinokai left a comment

Choose a reason for hiding this comment

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

Tested. Works great. 🚀

Copy link

@ericapisani ericapisani left a comment

Choose a reason for hiding this comment

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

In general this looks good, but would like to see some test coverage for this before giving the 👍🏻

}

console.log('...done')
}

Choose a reason for hiding this comment

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

Reading the contents of the update method, I think this could be broken down into several helper functions and then have them be invoked within update:

removeOldMiddlewareFile:

try {
      // Start by deleting the old file. If we error out, we don't want to leave the old file around
      await promises.unlink(join(base, '.netlify', 'middleware.js'))
    } catch {
      // Ignore, because it's fine if it didn't exist
    }

The logging in the branches of watchedFiles.length === 0 and watchedFiles.length > 1 could be it's own distinct method, with the return statement happening after that logging is done if necessary.

buildMiddlewareFile:

    try {
      await build({
        entryPoints: watchedFiles,
        outfile: join(base, '.netlify', 'middleware.js'),
        bundle: true,
        format: 'esm',
        target: 'esnext',
      })
    } catch (error) {
      console.error(error)
      return
    }

@ascorbic
Copy link
Contributor Author

ascorbic commented Dec 6, 2022

@ericapisani I've added a stack of tests

@netlify
Copy link

netlify bot commented Mar 17, 2023

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

Name Link
🔨 Latest commit c64bb95
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/6419ffe8ed98e50008dd227a
😎 Deploy Preview https://deploy-preview-1831--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.

@nickytonline
Copy link

I've tested all the scenarios in regards to renaming and moving the middleware file around and it all works as expected. cc: @MarcL. Just looking into the code/tests.

@nickytonline nickytonline dismissed ericapisani’s stale review March 20, 2023 15:24

Erica is no longer on the team.

Copy link

@sarahetter sarahetter left a comment

Choose a reason for hiding this comment

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

LGTM!

@nickytonline nickytonline merged commit 92e209f into main Mar 21, 2023
@nickytonline nickytonline deleted the mk/dev-watch branch March 21, 2023 20:03
expect(middlewareExists()).toBeTruthy()
})

it.skip('should compile a file if it is written after the watcher starts', async () => {

Choose a reason for hiding this comment

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

I've created an issue to revisit reneabling the skipped tests.

expect(middlewareExists()).toBeTruthy()
})

it.skip('should compile a file if it is written after the watcher starts', async () => {

Choose a reason for hiding this comment

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

I created an issue to reneable the skipped tests, #2005.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Middleware does not run with netlify-cli
5 participants