Skip to main content
GoodFirstPicks
DashboardIssuesReposLeaderboard

GoodFirstPicks by Leaveitblank © 2026

CreatorRequest a RepoPrivacy PolicyTerms of Service
router.push with "as" parameter causes infinite loop when using a middleware | GoodFirstPicks

router.push with "as" parameter causes infinite loop when using a middleware

vercel/next.js 2 comments 1mo ago
View on GitHub
mediumopenScope: somewhat clearSkill match: maybeTest focusedNext.jsTypeScriptReact

Why this is a good first issue

Middleware interaction with router.push causes an infinite loop.

AI Summary

The issue involves an infinite loop when using router.push with the 'as' parameter alongside middleware. The problem is reproducible and affects routing behavior. The fix requires understanding middleware and routing interactions in Next.js.

Issue Description

Link to the code that reproduces this issue

https://github.com/KmaCoder/reproduce-router-middleware-bug

To Reproduce

  1. Add an empty middleware.ts
import { NextMiddleware } from 'next/server';

export const middleware: NextMiddleware = (req) => {
    console.log(`> middleware req.url ${req.url}`)
};
  1. Use router.push with an "as" parameter
// On the index page make a redirect to some other page,
// with an 'asPath' = '/' to be shown in browser search bar  
void router.push('/home-redirect', '/')

Current vs. Expected behavior

Current

The page does not change, in network there is an infinite loop of fetching the page JSON /_next/data/.

With middleware.ts:

Screenshot 2023-11-17 at 17 15 22

Expected:

The page changes to the specified one, while the pathname in the browser equals to the as param.

Without middleware.ts, works as expected:

Screenshot 2023-11-17 at 17 17 38

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 22.6.0: Wed Jul  5 22:22:05 PDT 2023; root:xnu-8796.141.3~6/RELEASE_ARM64_T6000
Binaries:
  Node: 18.17.0
  npm: 9.6.7
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 14.0.4-canary.1
  eslint-config-next: N/A
  react: 18.2.0
  react-dom: 18.2.0
  typescript: 5.1.3
Next.js Config:
  output: N/A

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

Middleware / Edge (API routes, runtime), Routing (next/router, next/navigation, next/link)

Additional context

Reproducible since the middleware

GitHub Labels

bugLinking and NavigatingRuntime

Want to work on this?

Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!

Risk Flags

  • infinite loop
  • middleware interaction
Loading labels...

Details

Points20 pts
Difficultymedium
Scopesomewhat clear
Skill Matchmaybe
Test Focusedyes