0

I just got introduced into clerk, my setup is a minimal setup with nextjs/typescript, with no other foreign dependency. I'm using the src directory format, and I'm following everything in the tutorial,

[ Server ] Error: Clerk: auth() was called but Clerk can't detect usage of clerkMiddleware(). Please ensure the following:
-  clerkMiddleware() is used in your Next.js Middleware.
- Your Middleware matcher is configured to match this route or page.
- If you are using the src directory, make sure the Middleware file is inside of it.

For more details, see https://clerk.com/docs/quickstarts/nextjs

1 Answer 1

1

This can happen in two situations:

  1. The error is reflecting the actual error - your middleware file is in the wrong place, or the matcher is not triggering on one of your routes, etc. If you follow the quickstart guide accurately with a blank create-next-app setup, you won't get this error. You could create a minimal reproduction by starting with this and building up to where you are at with your app, noting at which point you get the error. This would help to narrow it down. If this still doesn't do it, take the minimal reproduction and send it in to Clerk support and they will get you back an answer typically within a day

  2. You have a broken asset or image path in your app, and are loading auth state from Clerk in your layout. There's an odd behavior in next.js in which, if you have a 404, which is normally caused by a bad image path or something, it will render and return a 404 page into your app's layout. Clerk's middleware is configured to skip running on image/asset paths by default, since typically you don't need to authenticate for them, so what happens is that if you have a broken asset path, Clerk middleware doesn't run, then next.js returns a 404 page into which it renders your layout, which pulls auth state, and that triggers this error in which it says that you're missing middlware, when in reality it's just a bad image src.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.