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

Unexpected Behavior using "after" in middleware #74941

Open
willwill96 opened this issue Jan 15, 2025 · 0 comments
Open

Unexpected Behavior using "after" in middleware #74941

willwill96 opened this issue Jan 15, 2025 · 0 comments
Labels
Middleware Related to Next.js Middleware.

Comments

@willwill96
Copy link

Link to the code that reproduces this issue

https://github.com/willwill96/nextjs-after-function-repro

To Reproduce

  1. Start the application
  2. Fetch http://localhost:3000 through preferred method
  3. Observe Logs for the following
  • Middleware Timing - Start {time}
  • Middleware Timing - End {time}
  • Page Timing - Start {time}
  • Page Timing - End {time}

Current vs. Expected behavior

Current Behavior:
after blocks within middleware functions are executed before page render begins

Expected Behavior:
From the docs

after allows you to schedule work to be executed after a response (or prerender) is finished.

Based on this, I expect after blocks within middleware functions to be executed after page render is finished.

Provide environment information

Operating System:
  Platform: linux
  Arch: x64
  Version: #1 SMP Tue Nov 5 00:21:55 UTC 2024
  Available memory (MB): 47749
  Available CPU cores: 16
Binaries:
  Node: 22.12.0
  npm: 10.9.0
  Yarn: 1.22.22
  pnpm: N/A
Relevant Packages:
  next: 15.1.4 // Latest available version is detected (15.1.4).
  eslint-config-next: 15.1.4
  react: 19.0.0
  react-dom: 19.0.0
  typescript: 5.7.3
Next.js Config:
  output: N/A

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

Middleware

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

next dev (local), next start (local), next build (local)

Additional context

Example Application Output:

Middleware Timing - Start 14041.842787
Middleware Timing - End 14042.483035
Page Timing - Start 14060.135459
 GET / 200 in 27ms
Page Timing - End 14069.832975

Relevant code:

import { after, NextRequest, NextResponse } from "next/server";

export async function middleware(req: NextRequest) {
    if (req.url === 'http://localhost:3000/') {

        console.log("Middleware Timing - Start", performance.now())
        after(()=>{
          console.log("Middleware Timing - End", performance.now())
        })
    }
    return NextResponse.next()
}
import { after } from "next/server";

export default function Home() {
  console.log("Page Timing - Start", performance.now())
  after(()=>{
    console.log("Page Timing - End", performance.now())
  })
  return (
    <div className.....
  );
}
@willwill96 willwill96 added the bug Issue was opened via the bug report template. label Jan 15, 2025
@github-actions github-actions bot added the Middleware Related to Next.js Middleware. label Jan 15, 2025
@samcx samcx removed the bug Issue was opened via the bug report template. label Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Middleware Related to Next.js Middleware.
Projects
None yet
Development

No branches or pull requests

2 participants