Skip to content

Add middleware to log image api requests - #185

Open
rpeterman-gp wants to merge 1 commit into
mainfrom
feat/add-middleware-logger-for-image-api
Open

rpeterman-gp wants to merge 1 commit into
mainfrom
feat/add-middleware-logger-for-image-api

Conversation

@rpeterman-gp

Copy link
Copy Markdown
Contributor

To Review

  • Code review

@rpeterman-gp
rpeterman-gp requested a review from farski April 30, 2026 17:14
@rpeterman-gp rpeterman-gp self-assigned this Apr 30, 2026
@rpeterman-gp rpeterman-gp added Level: Intermediate Smooth for some, bumpy for others Size: XS 10 minutes to do the work Priority: Normal Cruising speed labels Apr 30, 2026
Comment thread middleware.ts
@@ -0,0 +1,14 @@
import { NextRequest, NextResponse } from 'next/server';

export function middleware(req: NextRequest) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread middleware.ts
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 });

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

req object is being included in the logged object here.

@rpeterman-gp rpeterman-gp changed the title fAdd middleware to log image api requests Add middleware to log image api requests May 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Level: Intermediate Smooth for some, bumpy for others Priority: Normal Cruising speed Size: XS 10 minutes to do the work

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants