vercel / next.js

The React Framework
https://nextjs.org
MIT License
127.36k stars 27.02k forks source link

Middleware sometimes doesn't executes #58025

Open apuatcfbd opened 1 year ago

apuatcfbd commented 1 year ago

Link to the code that reproduces this issue

https://github.com/apuatcfbd/nextjs13-middleware-issue

To Reproduce

When using router.push middleware sometimes doesn't gets executed. The issue can be produced in the provided repo. There i'm intentionally using router.push instead of Link. The issue can be seen on my screencast too. https://drive.google.com/file/d/19-UsJ18vxEoAK5b8X-bm0KEZpcZqrRmU/view?usp=sharing

In there if you look at the terminal, first time the middleware was executed, then thats not executing, after I waited around 15-25 seconds, that started executing again everytime I visit the secure path which is under protection of the middleware. Is here i'm doing something wrong?

Current vs. Expected behavior

Current Behavior Middleware is not executing everytime route gets changed. Strangely it starts work normal after few seconds!

Expected Behavior Middleware should gets executed everytime route gets changed,

Verify canary release

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #37~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Mon Oct  9 15:34:04 UTC 2
Binaries:
  Node: 20.4.0
  npm: 9.7.2
  Yarn: N/A
  pnpm: 8.10.2
Relevant Packages:
  next: 13.5.6
  eslint-config-next: 13.5.6
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.2.2
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Middleware / Edge (API routes, runtime)

Additional context

No response

erfanGharib commented 1 year ago

Hi, thanks for your report! 🙏 I checked this out and realized that this bug is only related to the app directory, not the pages. I'm going to work on fixing this. 👍

sophiedophie commented 11 months ago

Hey @erfanGharib can you give us some hint why this is happening? Or what is the ETA for this bug? Seriously we need to make sure middleware is executed every time. This is nonsense that middleware execution is not consistent

NickG-NZ commented 10 months ago

Following up. This is a critical bug, when can we expect a fix? We use middleware to protect routes (Next 13.5.4). We have consistently seen a situation where after logout the user can return to their "protected" account page via a link because the middleware isn't executed (verified by logging all calls to middleware).

umar-brackets commented 7 months ago

I am also facing the issue that on dev the middleware is called on each route specified in the matcher:[], it is all ok in dev but after creating a build of my next js 13.5 project the middle is not called on the path "/", except "/" middleware is called on every route in the build. Can anyone please guide me why there is a difference in calling middleware in dev and after creating the build?

Benjythebee commented 4 months ago

According to the docs https://nextjs.org/docs/app/api-reference/functions/use-router; router.push is solely client side and therefore won't execute server-side request (Which includes the middleware);

Unless I'm mistaken?

RubensKj commented 4 months ago

Hello everyone! I was wondering if we have some updates on this bug??

sophiedophie commented 4 months ago

@Benjythebee But then here, if you see the video attached on the top, sometimes you see the 'Middleware ran' console message even the navigation happened from router.push. It sounds valid that middleware only triggered for server-side request, then router.push triggers server-side request...?

mickythompson commented 4 months ago

We encountered this middleware bug in Next.js v14.2.4. The issue is that the middleware doesn't always execute as expected. Specifically, we've noticed that it works on the first visit to a specific path, but not on subsequent visits during the same session. As the original author pointed out, temporary solutions like waiting for approximately 15 seconds or forcing a browser refresh can be used.

My specific use case involves checking if the user has verified their email address before allowing them to access a certain route (e.g., /app). Additionally, we need to check if the user is an administrator before allowing them to access another route (e.g., /admin). While we could manually implement these checks on each page, using middleware provides a simpler solution.

harshRishi commented 3 months ago

We’ve encountered a middleware bug in Next.js v14.2.4 that significantly affects user experience, requiring a hard refresh. This issue is critical for us since we rely on client-side middleware to protect routes. To address this, we consider using higher-order functions as a workaround, though it's not ideal. It's been a while since this bug was reported, and there is still no fix. We need a resolution soon to maintain a smooth user experience.

moussaab-moulim commented 3 months ago

after almost a year the bug is stilll there did anyone find a workaround ? im getting some weird bugs bcs of it

mozart25 commented 3 months ago

yeah same with me

RahulBirCodes commented 3 months ago

Link doesn't work in some cases either. Having to default to client components because of this issue.

jamj2000 commented 1 month ago

I have just tried with next@rc and it works !!

npx create-next-app@rc app

package.json

{
  "scripts": {
    "dev": "next dev --turbo",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "react": "19.0.0-rc-f994737d14-20240522",
    "react-dom": "19.0.0-rc-f994737d14-20240522",
    "next": "15.0.0-rc.0"
  }
}

screencast.webm