vercel / vercel/next.js

Unexpected Behavior using "after" in middleware

Open
#74,941 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Middleware
Dominant language
JavaScript
Stars
142k
Forks
32.4k
Avg merge
2d 14h
Merged PRs (30d)
351

Description

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.....
  );
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the linked reproduction and compare the logs from src/middleware.ts with those from src/app/page.tsx under next dev, next start, and next build. Trace how after is scheduled for middleware and page entry points, then verify that the observed execution order matches the documented response or prerender lifecycle.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, next.js, react, typescript
Domain
backend, web-dev
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.