1

I created a new Next.js@13.0.5 project with Turbopack and added middleware.ts file to the project's root directory. When I sent a request(e.g. /api/search?query=Hello), the middleware wasn't executed. Where is the location of middleware.ts file in the latest Next.js?

My middleware looks like:

import { NextRequest } from 'next/server';
import { query } from 'express-validator';
import validateRequest from '@/lib/request-validation';

export function middleware(request: NextRequest) {
  console.log('middleware', request.nextUrl.pathname);

  if (
    request.method === 'GET' &&
    request.nextUrl.pathname.startsWith('/search')
  ) {
    const res = validateRequest([
      query('query').isString(),
      query('page').isNumeric(),
      query('perPage').isNumeric(),
    ]);

    return res;
  }
}
juliomalves
  • 42,130
  • 20
  • 150
  • 146
tronx.dev
  • 138
  • 1
  • 11

1 Answers1

0

Hi there I recommend to do this in your package.json and don't use turbopack actually since middleware doesn't work with turbopack

"dev": "next dev",
"turbo": "next dev --turbo",

source: https://github.com/vercel/next.js/issues/42921