Middleware for _next/image
endpoint
#40061
-
Hi there, Does anyone know if/how there is a way to run middleware on I've been reading the documentation and I can see that the middleware runs on pages and assets in the public directory. In my testing I don't see the middleware catching anything for I've tried testing middleware inside the |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
Hi, Starting with next 12.2.x, you can use middleware only at the root, and it also sees
With this page, configured domains in import type { NextPage } from "next";
import Image from "next/image";
const Home: NextPage = () => {
return (
<div>
<Image
src="https://picsum.photos/200/300"
alt="random"
width={200}
height={300}
/>
</div>
);
};
export default Home; Then this middleware should do the trick for you: import { NextResponse, type NextRequest } from "next/server";
export function middleware(req: NextRequest) {
const { pathname, search } = req.nextUrl;
console.log("Middleware", { pathname, search });
// Middleware { pathname: '/_next/image', search: '?url=https%3A%2F%2Fpicsum.photos%2F200%2F300&w=256&q=75' }
return NextResponse.next();
} Remember, starting with |
Beta Was this translation helpful? Give feedback.
Hi,
Starting with next 12.2.x, you can use middleware only at the root, and it also sees
_next
internal calls!With this page, configured domains in
next.config.js
:Then this middleware should do the trick for you:
im…