Skip to content

Matcher-based middleware is not invoked when i18n is configured #516

@sladkoff

Description

@sladkoff

Versions

  • next: 14.2.11
  • open-next: 3.1.2

Setup

I have this middleware.ts which works in the standalone build of nextjs:

import { MiddlewareConfig, NextRequest, NextResponse } from 'next/server'

export async function middleware(req: NextRequest) {
  console.log('MIDDLEWARE')

  let url = req.nextUrl.clone()

  const baseUrl = "https://example.com/".split('://')

  url.protocol = baseUrl[0]
  url.hostname = baseUrl[1]
  url.port = baseUrl[0] === 'https' ? '443' : '80'
  url.pathname = url.pathname.replace(/^\/api\/test/, '')

  console.log(`Proxying ${req.url} to ${url.toString()}`)

  return NextResponse.rewrite(url)
}

export const config: MiddlewareConfig = {
  matcher: '/api/test/:path*',
}

I can go to localhost:3001/api/test/ in the browser and reach https://example.com/.

Problem

I have created this local open-next.local.config.ts:

// open-next.local.config.ts -
// A good practice would be to use a different name so that it doesn't conflict
// with your existing open-next.config.ts i.e. open-next.local.config.ts
import type { OpenNextConfig } from 'open-next/types/open-next.d.ts'

const config = {
  default: {
    override:{
      // We use a custom wrapper so that we can use static assets and image optimization locally
      wrapper: () => import('./dev/wrapper').then(m => m.default),
      // ISR and SSG won't work properly locally without this - Remove if you only need SSR
      // incrementalCache: () => import('./dev/incrementalCache').then(m => m.default),
      // ISR requires a queue to work properly - Remove if you only need SSR or SSG
      // queue: () => import('./dev/queue').then(m => m.default),
      converter: 'node',
    }
  },
  // middleware: {
  //   external: true,
  // },
  // You don't need this part if you don't use image optimization or don't need it in your test
  imageOptimization: {
    // Image optimization only work on linux, and you have to use the correct architecture for your system
    arch: 'x64',
    override: {
      wrapper: 'node',
      converter: 'node',
    }
    // If you need to test with local assets, you'll have to override the imageLoader as well
  },

  dangerous: {
    // We disable the cache tags as it will usually not be needed locally for testing
    // It's only used for next/cache revalidateTag and revalidatePath
    // If you need it you'll have to override the tagCache as well
    disableTagCache: true,


    // You can uncomment this line if you only need to test SSR
    //disableIncrementalCache: true,
  },
  // You can override the build command so that you don't have to rebuild the app every time
  // You need to have run the default build command at least once
  // buildCommand: 'echo "no build command"',
  edgeExternals: ['./dev/wrapper'],
} satisfies OpenNextConfig

export default config

I build and start the local open-next server and go to http://localhost:3001/api/test/ -> I receive the 404 page of my nextjs project and the server logs never show the line "MIDDLEWARE".

The same happens when the open-next app is deployed to AWS.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions