Skip to content

[nextjs] Placing middleware.ts in src directory causes middleware to be ignored #8845

Closed
@jskovman

Description

@jskovman

Is there an existing issue for this?

How do you use Sentry?

Sentry Saas (sentry.io)

Which SDK are you using?

@sentry/nextjs

SDK Version

7.64.0

Framework Version

7.64.0

Link to Sentry event

No response

SDK Setup

Sentry.init({
  dsn: "DSN",
  tracesSampleRate: 1,
  debug: false,
});

Steps to Reproduce

Basically, middleware gets bypassed/ignored when the middleware.(ts|js) file is placed in the src folder, something that is supported according to Next.js documentation.

  1. npx create-next-app --example reproduction-template reproduction-app
  2. cd reproduction-app
  3. npx @sentry/wizard -i nextjs
  4. mkdir src
  5. mv app src/
  6. touch src/middleware.ts
  7. Middleware contents:
export function middleware(request) {
throw new Error("This never runs");
}
  1. mkdir -p src/app/api/test
  2. touch src/app/api/test/route.ts
  3. src/app/api/test/route.ts contents:
import { NextResponse } from "next/server";

export function GET() {
  return NextResponse.json(
    {
      success: "middleware not called",
    },
    { status: 200 }
  );
}
  1. yarn build && yarn start
  2. curl http://localhost:3000/api/test

Expected Result

The curl request never reaches the src/api/test/route.ts route due to middleware throwing an error.
According to Next.js's documentation on middleware conventions the middleware.ts file should be able to be placed in both the root and in the src directory.

Actual Result

The curl request reaches the src/api/test/route.ts route.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions