@earth-app/collegedb
    Preparing search index...

    Function getClosestRegionFromIP

    • Determines the closest D1 region based on an IP address. Uses IP geolocation to estimate the user's location and find the nearest D1 region.

      This function uses Cloudflare's CF object which provides geolocation data in Cloudflare Workers environment. Falls back to 'wnam' if geolocation fails.

      Parameters

      • request: Request

        The incoming Request object (contains CF geolocation data in Cloudflare Workers)

      Returns D1Region

      The closest D1Region based on IP geolocation

      // In a Cloudflare Worker
      export default {
      async fetch(request: Request, env: Env) {
      const userRegion = getClosestRegionFromIP(request);

      initialize({
      kv: env.KV,
      strategy: 'location',
      targetRegion: userRegion, // Automatically optimized for user location
      shardLocations: { ... },
      shards: { ... }
      });
      }
      };