Skip to content

fix: ensure newly-created middleware works #1558

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 4 commits into from
Aug 18, 2022
Merged

Conversation

ascorbic
Copy link
Contributor

@ascorbic ascorbic commented Aug 18, 2022

Summary

Currently when running ntl dev, if there isn't any middleware when you start the server and then create one, even though the file is correctly compiled, the middleware isn't imported until a restart because Deno has cached the "fact" that it's missing. It correctly updates any changes if the middleware already exists at the time the server starts.

This PR adds a cache-busting hash to the import URL, which ensures it try to load dynamically on every request.

Published as @netlify/[email protected]

Test plan

  1. Install @netlify/[email protected] in a new Next.js project
  2. start ntl dev
  3. Add a middleware.ts, such as the one below
  4. Reload the page and check it works
import { NextResponse } from 'next/server'

export async function middleware(req) {
    const response = NextResponse.next()
    response.headers.set('x-hello-deno', 'Deno' in globalThis ? 'true' : 'false')
    return response
}

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

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 August 18, 2022 14:56
@netlify
Copy link

netlify bot commented Aug 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/next-hp-edge-demo/deploys/62fe86344a4b0f0008af9266
😎 Deploy Preview https://deploy-preview-1558--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.

@netlify
Copy link

netlify bot commented Aug 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-demo/deploys/62fe86336388640008cba7cd
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-nx-monorepo-demo/deploys/62fe86330a92f6000805e32a
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-export-demo/deploys/62fe86332abd2f00080accfb
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

Deploy Preview for next-i18next-demo ready!

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/next-i18next-demo/deploys/62fe86345818f5000848ee27
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/nextjs-plugin-custom-routes-demo/deploys/62fe8634de2de80008e08a9a
😎 Deploy Preview https://deploy-preview-1558--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 Aug 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-next-auth-demo/deploys/62fe8634979348000b4cc47f
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-rsc-demo/deploys/62fe8633979348000b4cc47a
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

Deploy Preview for next-plugin-canary ready!

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-canary/deploys/62fe86345818f5000848ee2c
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/netlify-plugin-nextjs-static-root-demo/deploys/62fe86335a0eba000814e38d
😎 Deploy Preview https://deploy-preview-1558--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 18, 2022

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

Name Link
🔨 Latest commit df7ba10
🔍 Latest deploy log https://app.netlify.com/sites/next-plugin-edge-middleware/deploys/62fe863481e0ca000881f1c8
😎 Deploy Preview https://deploy-preview-1558--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.

@ascorbic ascorbic added the type: bug code to address defects in shipped code label Aug 18, 2022
@sarahetter
Copy link

On refresh, I get a page that says "middleware is not a function"

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.

Yayy!

@kodiakhq kodiakhq bot merged commit 39f3b4b into main Aug 18, 2022
@kodiakhq kodiakhq bot deleted the mk/cache-bust-import branch August 18, 2022 18:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge type: bug code to address defects in shipped code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants