File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change @@ -26,8 +26,10 @@ export default class extends WorkerEntrypoint {
2626 async fetch ( request ) {
2727 return runWithCloudflareRequestContext ( request , this . env , this . ctx , async ( ) => {
2828 const startTime = Date . now ( ) ;
29+ const middlewareRequest = new Request ( request . url , request ) ;
30+ middlewareRequest . headers . set ( 'x-open-next-continent' , request . cf ?. continent || '' ) ;
2931 // - `Request`s are handled by the Next server
30- const reqOrResp = await middlewareHandler ( request , this . env , this . ctx ) ;
32+ const reqOrResp = await middlewareHandler ( middlewareRequest , this . env , this . ctx ) ;
3133 if ( reqOrResp instanceof Response ) {
3234 const duration = Date . now ( ) - startTime ;
3335 const logMessage = formatLog (
Original file line number Diff line number Diff line change @@ -28,6 +28,17 @@ export function shouldTrackEvents(headers?: Awaited<ReturnType<typeof nextHeader
2828export async function serveProxyAnalyticsEvent ( req : Request ) {
2929 const requestURL = new URL ( req . url ) ;
3030
31+ // Fill geolocation data from request headers either from OpenNext or Vercel
32+ const country =
33+ req . headers . get ( 'x-open-next-country' ) || req . headers . get ( 'x-vercel-ip-country' ) ;
34+ const latitude =
35+ req . headers . get ( 'x-open-next-latitude' ) || req . headers . get ( 'x-vercel-ip-latitude' ) ;
36+ const longitude =
37+ req . headers . get ( 'x-open-next-longitude' ) || req . headers . get ( 'x-vercel-ip-longitude' ) ;
38+ // OpenNext doesn't provide continent info, we add it manually in our custom worker
39+ const continent =
40+ req . headers . get ( 'x-open-next-continent' ) || req . headers . get ( 'x-vercel-ip-continent' ) ;
41+
3142 const org = requestURL . searchParams . get ( 'o' ) ;
3243 const site = requestURL . searchParams . get ( 's' ) ;
3344 if ( ! org || ! site ) {
@@ -43,6 +54,10 @@ export async function serveProxyAnalyticsEvent(req: Request) {
4354 method : 'POST' ,
4455 headers : {
4556 'Content-Type' : 'application/json' ,
57+ ...( country ? { 'x-location-country' : country } : { } ) ,
58+ ...( latitude ? { 'x-location-latitude' : latitude } : { } ) ,
59+ ...( longitude ? { 'x-location-longitude' : longitude } : { } ) ,
60+ ...( continent ? { 'x-location-continent' : continent } : { } ) ,
4661 } ,
4762 body : req . body ,
4863 } ) ;
You can’t perform that action at this time.
0 commit comments