DEV Community

Discussion on: Using Mixpanel via proxy with Next.js rewrites

Collapse
 
irfancoder profile image
Irfan Ismail

I tried to implement this, but found that, tracking no longer provides the geolocation of the event. I assume this is because of the proxy, where the IP of the request is being overidden server-side. Any idea how to solve this?

Collapse
 
petrbrzek profile image
Petr Brzek • Edited

I have dealt with the same and it can be solved via middleware.

import { NextResponse } from 'next/server'
import requestIp from 'request-ip'

export const config = {
  matcher: '/mp/:path*',
}

export function middleware(request) {
  const requestHeaders = new Headers(request.headers)
  requestHeaders.set('X-REAL-IP', requestIp.getClientIp(request) || request.ip)

  return NextResponse.rewrite(request.nextUrl, {
    request: {
      headers: requestHeaders,
    },
  })
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
rain2o profile image
Joel Rainwater

Ah, nice one. Thanks @petrbrzek that looks like a good solution. I’ll have to try that out next time.

Sorry, I didn’t get notifications of these comments.