Add middleware to log image api requests - #185
rpeterman-gp wants to merge 1 commit into
Conversation
| @@ -0,0 +1,14 @@ | |||
| import { NextRequest, NextResponse } from 'next/server'; | |||
|
|
|||
| export function middleware(req: NextRequest) { | |||
There was a problem hiding this comment.
If this can only log image requests, I think it'd be good to log all the important parts of the request (headers, path, etc), not just the image URL. If this middleware technique can be used to log all requests coming into the server, I'd be in favor of trying that, since that I think is still the end goal. We can reduce things later if we find that it's too noisy.
Matching the JSON format of existing logs would also be good.
There was a problem hiding this comment.
It should be logging the entire req object. I am kind of in the dark as to what is being logged since dev env do not run the middleware. Need to see it in action in prod server to know for sure if a) it gets run and b) what is in the req object.
There was a problem hiding this comment.
Based on my testing locally, this does not appear to log requests to /_next/image (I do see other requests being logged in the middleware). Claude has some other suggestions for ways to hook into the image optimizer request path, but I didn't really dig into them.
| if (req.nextUrl.pathname === '/_next/image') { | ||
| const imageUrl = req.nextUrl.searchParams.get('url'); | ||
| // eslint-disable-next-line no-console | ||
| console.log({ message: `Image requested: ${imageUrl}`, req }); |
There was a problem hiding this comment.
req object is being included in the logged object here.
To Review