Skip to content

Commit

Permalink
Fix wrong ip address and location being displayed on load
Browse files Browse the repository at this point in the history
  • Loading branch information
david-tejada committed Mar 27, 2024
1 parent 3590691 commit 541cf0d
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion functions/fetch-location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ import { Handler } from "@netlify/functions";

const handler: Handler = async (event) => {
const IPIFY_API_KEY = process.env.IPIFY_API_KEY;
const q = event.queryStringParameters?.q;

// When the request is for the user's location we need to pass the user's ip
// address or we would get the location where the Netlify function is run.
const clientIp = event.headers["x-nf-client-connection-ip"];
const isLocalhost = clientIp === "127.0.0.1" || clientIp === "::1";

const q = event.queryStringParameters?.q || isLocalhost ? "" : clientIp;

try {
const domainQuery = q ? `&domain=${q}` : "";
Expand Down

0 comments on commit 541cf0d

Please sign in to comment.