next-translate-routes: _middleware.ts doesn't work on translated routes

Hello,

I really enjoy this library but I’m facing very weird behavior when using NextJS 12 _middleware.ts function.

Using _middleware.ts simple as:

import type { NextFetchEvent, NextRequest } from 'next/server'

const middleware = (req: NextRequest, ev: NextFetchEvent) => {
  return new Response('Hello, world!')
}

export default middleware;

works when:

- /pages
----/my-page <-- no _routes.json
--------index.tsx
--------_middleware.ts

And shows “Hello, world!”

But when I add _routes.json, middleware stops working.

- /pages
----/my-page
--------index.tsx
--------_routes.json <-- makes middleware not executing
--------_middleware.ts

It’s quite important for me. Thanks!

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 22

Most upvoted comments

The Next.js issue seems to be fixed from next@13.1.7.

I reproduced the bug, and published the repro in this branch. As soon as a middleware is set up, getStaticProps fail for non default locales.

I looked into it but I did not find where it comes from yet.

@pinktonio Did you figure out a solution to this issue? I’m facing the same with Next.js 13.4.19 😞

No unfortunately not. We are currently not using the translated routes until this gets solved.

Some news: I investigated this bug further. It appears to be a Next issue. I reported the issue in the next repo here.

The problem occurs when a matcher is declared in the middleware config. So, as long as the issue is not solved, we have 2 annoying options:

  • either stick with next@12.1 and the previous middleware synthax,
  • or remove the matcher from middleware config, and handle the selection from within the middleware.