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

Next.js Middleware not working in Custom Server example after build process #6946

Open
sebastianpulak opened this issue Jun 26, 2024 · 5 comments
Assignees
Labels
keep status: verified If an issue has been reproduced v2

Comments

@sebastianpulak
Copy link

Link to reproduction

No response

Describe the Bug

I've cloned a ecommerce template (https://github.com/payloadcms/payload/tree/main/templates/ecommerce) to start working on a Next.js project. When I added a simple middleware as you often do in Next.js applications, it only runs on development using yarn dev but after building the project using yarn build && yarn serve it no longer works - it's not fired.

My middleware.ts file is placed inside /src folder and looks like this:

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

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function middleware(request: NextRequest) {
  // eslint-disable-next-line no-console
  console.log('middleware hit: ', request.url)
  return NextResponse.next()
}

export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico|favicon.svg|icon.png|sitemap.xml|robots.txt).*)',
  ],
}

Development:
image

After building the project it no longer fires (console.log is not visible).

Any idea how to make middleware work with Custom server?

To Reproduce

  1. Clone the ecommerce template https://github.com/payloadcms/payload/tree/main/templates/ecommerce by running command npx create-payload-app@latest my-project -t ecommerce
  2. Add a middleware.ts file inside src/ folder (same level as server.ts file) with following content:
import type { NextRequest } from 'next/server'
import { NextResponse } from 'next/server'

// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export function middleware(request: NextRequest) {
  // eslint-disable-next-line no-console
  console.log('middleware hit: ', request.url)
  return NextResponse.next()
}

export const config = {
  matcher: [
    '/((?!api|_next/static|_next/image|favicon.ico|favicon.svg|icon.png|sitemap.xml|robots.txt).*)',
  ],
}
  1. Run yarn build && yarn serve
  2. Visit http://localhost:3000

You won't be able to see a console.log inside a middleware file as opposite to running a project by yarn dev

Payload Version

2.22.0

Adapters and Plugins

No response

@sebastianpulak sebastianpulak added status: needs-triage Possible bug which hasn't been reproduced yet v2 labels Jun 26, 2024
@omonina
Copy link

omonina commented Jul 4, 2024

I am coping with similar thing.
Support - any luck with reproducing this issue?

@github-actions github-actions bot removed the status: needs-triage Possible bug which hasn't been reproduced yet label Jul 26, 2024
@denolfe
Copy link
Member

denolfe commented Jul 30, 2024

@sebastianpulak Looking into recreating this issue now.

@denolfe denolfe added the status: verified If an issue has been reproduced label Jul 30, 2024
@denolfe
Copy link
Member

denolfe commented Jul 30, 2024

I was able to recreate this issue but have not been able to pinpoint a cause. A blank Next.js app of the same version does not have this issue. It likely has something to do with how we are invoking the nextBuild command.

@MauriceMarkslag
Copy link

MauriceMarkslag commented Nov 8, 2024

@denolfe Hey Elliot, has there been any progress/updates regarding this issue? We are running into the exact same problem. Thanks in advance!

@denolfe denolfe added the keep label Dec 3, 2024
@r1tsuu
Copy link
Member

r1tsuu commented Dec 6, 2024

Pretty sure it's the same problem as here - #3653, just for the ecommerce template. You need to change this line #3653

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
keep status: verified If an issue has been reproduced v2
Projects
None yet
Development

No branches or pull requests

5 participants