Skip to content

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

New issue

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

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

Already on GitHub? Sign in to your account

Closed
3 tasks done
jskovman opened this issue Aug 18, 2023 · 3 comments
Closed
3 tasks done
Assignees
Labels
hacktoberfest Meta: Help Wanted Package: nextjs Issues related to the Sentry Nextjs SDK

Comments

@jskovman
Copy link

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.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 Aug 18, 2023
@github-actions github-actions bot added the Package: nextjs Issues related to the Sentry Nextjs SDK label Aug 18, 2023
@lforst
Copy link
Member

lforst commented Aug 23, 2023

sourcemaps around middleware (especially in Vercel edge runtime) are still very funky. We need to fix this.

@lforst
Copy link
Member

lforst commented Nov 7, 2023

Fixed with #9454

(please not that sourcemaps for edge on Vercel are still broken - this needs support from Vercel's side)

@lforst lforst closed this as completed Nov 7, 2023
@Lms24
Copy link
Member

Lms24 commented Nov 8, 2023

This was released in version 7.78.0 of @sentry/nextjs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
hacktoberfest Meta: Help Wanted Package: nextjs Issues related to the Sentry Nextjs SDK
Projects
Archived in project
Development

No branches or pull requests

4 participants