Skip to content

Skip billed Google Geolocation API call when browser location is out of bounds - #1472

Merged
lgarofalo merged 1 commit into
masterfrom
fix-out-of-bounds-geolocation-fallback
Sep 3, 2026
Merged

Skip billed Google Geolocation API call when browser location is out of bounds#1472
lgarofalo merged 1 commit into
masterfrom
fix-out-of-bounds-geolocation-fallback

Conversation

@lgarofalo

Copy link
Copy Markdown
Member

Problem

getLocation() in app/utils/location.ts tries the free HTML5 browser Geolocation API first, and only falls back to the billed Google Geolocation API on failure:

export const getLocation = () =>
  getLocationBrowser()
    .catch(() => getLocationGoogle())
    .catch(() => useDefaultSanFranciscoLocation());

However, getLocationBrowser() rejects in two very different situations, and this code couldn't tell them apart:

  1. A real failure (permission denied, unsupported browser, timeout).
  2. The browser successfully returned real, precise coordinates -- they're just outside our supported SF bounding box.

In case (2), we already have a real answer. Calling Google's Geolocation API (which is IP-based and much less precise than GPS) afterwards is very unlikely to produce a meaningfully different result -- it's just an unnecessary billed API call every time this happens. This was flagged as a known @todo in the original code:

/**
 * ...
 * @todo if getLocationBrowser is outside SF, errs and tries to load google as well. Fix
 */

Fix

Added a distinct OutOfBoundsLocationError that getLocationBrowser() throws specifically for case (2), so getLocation() can tell the two rejection reasons apart:

export const getLocation = () =>
  getLocationBrowser().catch((reason) => {
    if (reason instanceof OutOfBoundsLocationError) {
      // Real GPS location, just out of bounds -- skip the billed Google
      // call and go straight to the default.
      return useDefaultSanFranciscoLocation();
    }
    // Any other failure -- still worth trying Google as a fallback.
    return getLocationGoogle().catch(() => useDefaultSanFranciscoLocation());
  });

All other behavior is unchanged: permission denials, unsupported browsers, and timeouts still fall back to Google's Geolocation API (with its existing 2hr cache) exactly as before.

Scope

Standalone, minimal fix targeting just this one known bug/TODO. Doesn't touch the map-loading changes from #1470, CI workflow, or any other geolocation caching behavior (e.g. cache TTL).

Testing

  • npx tsc --noEmit -- passes
  • npx eslint app/utils/location.ts -- passes
  • npx prettier --check app/utils/location.ts -- passes
  • No existing spec coverage for app/utils/location.ts to update.

Replaces #1471, which was opened from a fork and hit an unrelated CI failure (test_e2e can't authenticate to GCP for pull_request runs from forks, since GitHub doesn't pass repo secrets to fork-originated PR workflows). This PR is pushed directly to a branch on this repo instead, so CI (including e2e) runs normally with full secrets access.

…ounds

Previously, if the browser's HTML5 Geolocation API successfully
returned real coordinates that were just outside our supported SF
bounding box, getLocation() treated that identically to a permission
denial or unsupported-browser error, and fell through to call the
(billed) Google Geolocation API as a fallback.

Since we already have a real, precise answer at that point, calling
Google's IP-based (and much less precise) Geolocation API is very
unlikely to produce a meaningfully different result -- it's just an
unnecessary billed request. This adds a distinct OutOfBoundsLocationError
so getLocation() can tell the two cases apart, and skips straight to
the default SF location when the browser's real location is simply
out of bounds. All other rejection reasons (permission denied,
unsupported browser, timeout, etc.) still fall back to Google as
before.

Resolves the @todo left in the original implementation.

@richardxia richardxia left a comment

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.

LGTM

@lgarofalo
lgarofalo merged commit e32624b into master Sep 3, 2026
8 of 9 checks passed
@lgarofalo
lgarofalo deleted the fix-out-of-bounds-geolocation-fallback branch September 3, 2026 05:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants