Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Middleware rewrite to edge router handler - Invariant: no match found for request #48295

Closed
1 task done
DaleLJefferson opened this issue Apr 12, 2023 · 13 comments · Fixed by #58797
Closed
1 task done
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@DaleLJefferson
Copy link

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.4.0: Mon Mar  6 21:00:41 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T8103
    Binaries:
      Node: 18.12.1
      npm: 8.19.2
      Yarn: 1.22.18
      pnpm: 7.29.3
    Relevant packages:
      next: 13.3.1-canary.5
      eslint-config-next: 13.3.0
      react: 18.2.0
      react-dom: 18.2.0

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true), Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue

https://codesandbox.io/p/sandbox/friendly-paper-t3xyol

To Reproduce

Using the codesandbox or code below

Visit: /one/example

middleware.ts

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

export function middleware(request: NextRequest) {
  const url = request.nextUrl;

  url.pathname = "/two/example";

  return NextResponse.rewrite(url);
}

export const config = {
  matcher: "/one/:path*",
};

/app/two/example/route.ts

export const runtime = "edge";
export const dynamic = "force-dynamic";

export function GET() {
  return new Response("Example");
}

Describe the Bug

A middleware rewrite to an edge route handler causes an error in the dev server.

Error: Invariant: no match found for request. Pathname '/one/example' should have matched '/two/example'

image

Expected Behavior

Expect: Middleware to rewrite to /two/example successfully.

Same code works when runtime is set to nodejs

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

@DaleLJefferson DaleLJefferson added the bug Issue was opened via the bug report template. label Apr 12, 2023
@github-actions github-actions bot added area: app Runtime Related to Node.js or Edge Runtime with Next.js. labels Apr 12, 2023
@DaleLJefferson
Copy link
Author

Same error message when deployed to Vercel

@eblanchette
Copy link

I think I'm experiencing something similar but with turbopack. When I include prisma it breaks. I don't think it's considering the runtime flag.

/src/app/api/order/route.ts

export const runtime = "nodejs";
export const revalidate = 0;

import { PrismaClient } from "@prisma/client/mrp";
import { NextRequest, NextResponse } from "next/server";

export async function GET(request: NextRequest) {
  const prisma = new PrismaClient();
  return NextResponse.json({
    foo: "bar",
  });
}

Error:

error rendering: Error: PrismaClient is unable to be run in the browser.

Debug info:
- error rendering: Error: PrismaClient is unable to be run in the browser.
  In case this error is unexpected for you, please report it in https://github.com/prisma/prisma/issuesstatus: signal: 9 (SIGKILL)

Error (if I move prisma up):

error rendering: TypeError: Cannot read properties of undefined (reading 'middleware_edge')

Debug info:
- error rendering: TypeError: Cannot read properties of undefined (reading 'middleware_edge')

@redgoldlace
Copy link

I believe I'm experiencing the same issue using Turbopack with next-auth; all requests to paths under /api/auth fail because of the same TypeError: Cannot read properties of undefined (reading 'middleware_edge'). To my knowledge next-auth uses middleware rewrites from a dynamic route, which is probably why.

That being said, I'm not sure why I'm encountering this issue in the first place; I'm not trying to use the edge runtime at all, and this should be running on the Node runtime. I have a feeling that @eblanchette's guess is correct, and the runtime flag is being ignored somehow.

I'm not sure if Turbopack lives in a separate repository - or where the next.js integration lives - but I'd be eager to contribute if possible.

@avmentzer
Copy link

same problem here

@godfrednanaowusu
Copy link

I believe I'm experiencing the same issue using Turbopack with next-auth; all requests to paths under /api/auth fail because of the same TypeError: Cannot read properties of undefined (reading 'middleware_edge'). To my knowledge next-auth uses middleware rewrites from a dynamic route, which is probably why.

That being said, I'm not sure why I'm encountering this issue in the first place; I'm not trying to use the edge runtime at all, and this should be running on the Node runtime. I have a feeling that @eblanchette's guess is correct, and the runtime flag is being ignored somehow.

I'm not sure if Turbopack lives in a separate repository - or where the next.js integration lives - but I'd be eager to contribute if possible.

i believe this specifically happens in the next 13.3.0 version

the next 13.3.1 version has a different error

error rendering: Error: Cannot find module 'http'

@la3rence
Copy link

la3rence commented Jun 19, 2023

Same here.
I was using the configuration by next.config.js with app router:

module.exports = {
  async rewrites() {
    console.log("rewrites");
    return [
      {
        source: "/test/:path*",
        destination: "/api/proxy",
      },
    ];
  },
};

Same error: Invariant: no match found for request. Pathname '/test/foo' should have matched '/api/proxy'
nodejs runtime works well but edge doesn't.

UPDATE:

I can confirm that this rewrite issue has been fixed for edge runtime in v14.0.4-canary.12.

Thanks @ztanner

@Royal-lobster
Copy link

This issue currently breaks usage of nextjs with next intl. hope it gets fixed or if we can get a work around to this issue

@kevinwolfcr
Copy link
Contributor

Same issue for me. Switching back to nodejs runtime solved the issue.

@lukasburns
Copy link

Same issue here, using export const runtime = 'nodejs' solves this issue. would very much like to make it work with edge

@sam3d
Copy link
Contributor

sam3d commented Nov 6, 2023

Also just bumped into this, forcing it off the edge runtime worked.

Looks like the fix for this isn't very complex, would be amazing to get some maintainer eyes on this

@bram209
Copy link

bram209 commented Nov 8, 2023

same issue, I would like to stay on the edge runtime, did anyone find a workaround (other than switching to nodejs)?

@AlexIsMaking
Copy link

Error: Invariant: no match found for request. Pathname '/one/example' should have matched '/two/example'

I ran into this issue as described here - vercel/ai#740.

But I wasn't using middleware and next 14.0.1 works fine, whereas 14.0.2 (re)introduces the issue.

@kodiakhq kodiakhq bot closed this as completed in #58797 Nov 23, 2023
kodiakhq bot pushed a commit that referenced this issue Nov 23, 2023
### What?
Rewrites to an edge route currently throw an invariant rather than properly serving up the page that is rewritten to.

### Why?
The `NextRequest` object that is provided to the edge route handler contains pathname information only for the "origin" request (e.g., when visiting `/one/example` which rewrites to `/two/example`, the pathname is still `/one/example`. This hits an invariant since the route matcher is unable to find `/one/example` since it does not exist.

### How?
This updates the module wrapper to grab the pathname from the route definition rather than the request object. For dynamic segments, we extract them from `request.nextUrl` since we know that even if `nextUrl` is referencing the origin path, the parameters it has are relevant for the rewrite. 

This adds the `getUtils` utility that's also used in base-server to handle the URL normalization, which also provides an interface for normalizing dynamic params from a `ParsedUrlQuery`.

Closes NEXT-1724
Fixes #48295
Copy link
Contributor

github-actions bot commented Dec 8, 2023

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

@github-actions github-actions bot added the locked label Dec 8, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 8, 2023
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
bug Issue was opened via the bug report template. locked Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.