Skip to content
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

Not working from Next.js middleware (edge runtime) when not using src folder #9631

Closed
pladaria opened this issue Nov 21, 2023 · 7 comments · Fixed by #9637
Closed

Not working from Next.js middleware (edge runtime) when not using src folder #9631

pladaria opened this issue Nov 21, 2023 · 7 comments · Fixed by #9637
Assignees

Comments

@pladaria
Copy link

Environment

SaaS (https://sentry.io/)

Steps to Reproduce

  1. npx create-next-app sentry-test. Create a new NextJS app with default values except the src directory (select No)
  • ✔ Would you like to use src/ directory? … No
  1. cd sentry-test. Enter to the directory
  2. npx @sentry/wizard@latest -i nextjs. Run the sentry wizard
  3. create a middleware.ts file (inside ./) that sends some traces to sentry, for example:
// ./middleware.ts
import {NextResponse} from 'next/server';
import * as Sentry from '@sentry/nextjs';

import type {NextRequest} from 'next/server';

export const middleware = (request: NextRequest): NextResponse => {
    Sentry.captureException(new Error('Sentry Example Middleware Error'));
    Sentry.captureMessage('Sentry Example Middleware Message', {
        level: 'info',
        extra: {
            method: request.method,
            path: request.nextUrl.pathname,
        },
    });
    console.log(request.method + ' ' + request.nextUrl.pathname);
    return NextResponse.next();
};
  1. yarn dev. Run the dev server and open http://localhost:3000

Expected Result

Receive issues from the middleware

We can verify that the middleware gets executed by looking at the console.logs from the terminal

Actual Result

Not receiving issues from middleware

Product Area

Unknown

Link

No response

DSN

No response

Version

No response

@getsantry
Copy link

getsantry bot commented Nov 21, 2023

Assigning to @getsentry/support for routing ⏲️

@getsantry
Copy link

getsantry bot commented Nov 21, 2023

Routing to @getsentry/product-owners-issues for triage ⏲️

@lobsterkatie lobsterkatie transferred this issue from getsentry/sentry Nov 21, 2023
@lforst
Copy link
Member

lforst commented Nov 22, 2023

Hi, can you try running a production build (next build && next start) instead of a dev build and try your setup? We generally design the SDK for production setups and some features (like edge runtime which middleware uses) are not available on dev for technical reasons.

@lforst lforst self-assigned this Nov 22, 2023
@pladaria
Copy link
Author

pladaria commented Nov 22, 2023

Hi, can you try running a production build (next build && next start) instead of a dev build and try your setup?

without src folder:

  • dev build: not working
  • prod build: not working

with src folder

same STR but selecting Yes when asked to use the src folder and storing the middleware.ts inside src

  • dev build: working
  • prod build: working

@lforst
Copy link
Member

lforst commented Nov 22, 2023

I unfortunately cannot reproduce this. On my Next.js 14 application without src folder and middleware.ts in the root of the project, events captured inside the middleware are successfully reported.

Can you please share a reproduction example? Either a github repo we can clone or a stackblitz or similar would be great. Thanks!

@pladaria
Copy link
Author

pladaria commented Nov 22, 2023

Ok, I've created a repo, following the reported STR for each case:

https://github.com/pladaria/sentry-middleware-issue-reproduction-repo

There are two folders, with-src and without-src, each with a NextJS setup and a middleware.

After running both apps (dev and prod builds), I only get issues from the with-src project:

image

Verified that the middlewares are executed by looking at the console output logs

@lforst
Copy link
Member

lforst commented Nov 22, 2023

Thank you for the reproduction! You are indeed right and found a very stupid bug. We don't correctly detect the existence of the middleware.ts file if there is no pages folder. We will fix this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants