question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Middleware causes router to choose root server-rendered [...catchall] over more specific dynamic route only in production

See original GitHub issue

Verify canary release

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

Provide environment information

I am using codesandbox with 12.2.4-canary.9, I don’t know why next info is reporting 12.1.7-canary.46 - if that were correct, this example wouldn’t run at all as I’m using top level middleware.

sandbox@sse-sandbox-kfj5rr:/sandbox$ sandbox@sse-sandbox-kfj5rr:/sandbox$ npx --no-install next infonpx --no-install next info

Operating System:
  Platform: linux
  Arch: x64
  Version: #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022
Binaries:
  Node: 14.19.3
  npm: 6.14.17
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 12.1.7-canary.46
  react: 18.2.0
  react-dom: 18.2.0


Operating System:
  Platform: linux
  Arch: x64
  Version: #45~20.04.1-Ubuntu SMP Mon Apr 4 09:38:31 UTC 2022
Binaries:
  Node: 14.19.3
  npm: 6.14.17
  Yarn: 1.22.19
  pnpm: N/A
Relevant packages:
  next: 12.1.7-canary.46
  react: 18.2.0
  react-dom: 18.2.0

warn - Latest canary version not detected, detected: “12.1.7-canary.46”, newest: “12.2.4-canary.9”. Please try the latest canary version (npm install next@canary) to confirm the issue still exists before creating a new issue. Read more - https://nextjs.org/docs/messages/opening-an-issue warn - Latest canary version not detected, detected: “12.1.7-canary.46”, newest: “12.2.4-canary.9”. Please try the latest canary version (npm install next@canary) to confirm the issue still exists before creating a new issue. Read more - https://nextjs.org/docs/messages/opening-an-issue

What browser are you using? (if relevant)

How are you deploying your application? (if relevant)

Deployed to Vercel

Describe the Bug

Top level middleware appears to be breaking routing to routes with dynamic segments, in favour of a less specific root [...catchall].tsx that uses getServerSideProps.

  1. Given the following directory structure: image

  2. where all pages except [...catchall].tsx are simple components (no use of getServersideProps or getStaticProps) similar to this: image

  3. and a top level middleware that applies only to /broken/:path* image

  4. when trying to access a route broken/[userid]/edit with middleware in front of it, NextJS is routing instead to the less specific [...catchall].tsx, which uses getServerSideProps image

image

  1. notice how the URL is still pointing at the broken route /broken/somevalue/edit. This happens even if the [...catchall] redirects.

This does not happen if:

  • You are running in development.
  • You use [catchall] instead of [...catchall] (i.e. you use a dynamic route segment, but not one that is a catch all)
  • The catchall does not use getServerSideProps
  • There is no catchall route
  • The target path does not have middleware applied to it
  • You use previous versions of NextJS where you have a colocated _middleware.ts

I have not tried:

  • Moving the catchall to another location

Expected Behavior

Adding middleware in front of a dynamic route should not affect routing.

Link to reproduction

https://codesandbox.io/s/nextjs-12-2-middleware-catchall-issue-kfj5rr?file=/pages/index.tsx

To Reproduce

This bug ONLY appears after building. It does not happen when running next dev.

// ✔️ […catchall] // ✔️ middleware // ✔️ bug // https://csb-kfj5rr-pjwuo58u8-winwardo.vercel.app/

// ✔️ […catchall], but without getServerSideProps // ✔️ middleware // ❌ bug // https://csb-kfj5rr-dl8707jrw-winwardo.vercel.app/

// ❌ […catchall] // ✔️ middleware // ❌ bug // https://csb-kfj5rr-l5obh31us-winwardo.vercel.app/

// ❌ […catchall] // ✔️ [query] // ✔️ middleware // bug // https://csb-kfj5rr-pnre13we2-winwardo.vercel.app/

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ijjkcommented, Aug 9, 2022

Closing as this should be resolved in the latest version of Next.js

0reactions
github-actions[bot]commented, Sep 9, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure Route Selection for Routers - Cisco
This document describes how routers work, are configured, and how to select a route for them.
Read more >
Routing in ASP.NET Core - Microsoft Learn
Routing is responsible for matching incoming HTTP requests and dispatching those requests to the app's executable endpoints.
Read more >
Routing: Introduction - Next.js
When a file is added to the pages directory, it's automatically available as a route. The files inside the pages directory can be...
Read more >
Implementing SSR in Next.js: Dynamic routing and prefetching
This happens because named, or static, routes have a higher priority than a dynamic route. For example, if you have declared a dynamic...
Read more >
Catch All Routes On Main Level Of Application Causes ...
Recently though I've been using Next.js for more and more of them. It has a bunch of great features like routing and serverside...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found