Cannot set region in Vercel Next.js middleware deployment

Hi all!

I am planning to deploy a Next.js application with a Upstash ratelimiter in the middleware and would want to have the middleware run as close to the Redis instance as possible.

I have tried setting the region with preferredRegion

export const preferredRegion = 'iad1'

export const config = {
    matcher: [
        '/((?!api|_next/static|_next/image|favicon|assets).*)',
    ],
}

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
    return NextResponse.next()
}

and with config.regions like so

export const config = {
    matcher: [
        '/((?!api|_next/static|_next/image|favicon|assets).*)',
    ],
    regions: ['iad1'],
}

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
    return NextResponse.next()
}

None of these options seem to work as middleware is still executed in the datacenter closest to me (I verified this with a API request to a a public endpoint that returns the IP of the middleware).

Should it be possible to set the region of the middleware or not?

I though also about running multiple Redis instances and using multiregionRateLimit but that would incur an additional cost.

PS. I’m testing this on Hobby plan.

I could be wrong but I think middleware always runs on the edge, executing in the data center closest to the user making the request.

I guess so. I’ll resort to using the ratelimiter for API requests as then I can ensure the Ratelimiter is run in a function close to the Redis instance and use Vercel WAF Ratelimiter for general IP ratelimiting instead.