惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
有赞技术团队
有赞技术团队
宝玉的分享
宝玉的分享
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
V
V2EX
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
D
Docker
T
The Blog of Author Tim Ferriss
罗磊的独立博客
博客园 - 叶小钗
酷 壳 – CoolShell
酷 壳 – CoolShell
Blog — PlanetScale
Blog — PlanetScale
月光博客
月光博客
J
Java Code Geeks
Jina AI
Jina AI
博客园 - 【当耐特】
C
Check Point Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
腾讯CDC
Last Week in AI
Last Week in AI
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
Visual Studio Blog

Nx Blog

Sharing Tailwind CSS Styles Across Apps in a Monorepo | Nx Blog How SiriusXM Stays Competitive by Iterating and Getting to Market Fast | Nx Blog Agentic Experience Is the New Developer Experience | Nx Blog Nx Joins the Linux Foundation and the Agentic AI Foundation | Nx Blog A Monorepo Is NOT a Monolith | Nx Blog Why we deleted (most of) our MCP tools | Nx Blog Teach Your AI Agent How to Work in a Monorepo | Nx Blog How Broadcom stays efficient and nimble with monorepos | Nx Blog Why Monorepos are King in the Age of AI | Nx Blog Nx 2026 Roadmap: Expanding Agent Autonomy, Improving Performance, Better Polyglot and More | Nx Blog End to End Autonomous AI Agent Workflows with Nx | Nx Blog Autonomous Agents at Scale | Nx Blog Scaling 700+ Projects: How Nx Became a 'No-Brainer' for Caseware | Nx Blog Configure Tailwind v4 with Angular in an Nx Monorepo | Nx Blog The Missing Multiplier for AI Agent Productivity | Nx Blog A Year of Nx Webinars | Nx Blog Wrapping Up 2025 | Nx Blog Nx 22.3 Release: Angular 21 Support, tsgo Compiler, and Prettier v3 | Nx Blog Nx Cloud Release: Agent Resource Usage | Nx Blog Nx Platform Outperforms DIY Cache by 5x | Nx Blog An Nx Carol: Past, Present, and Future of Your Monorepo | Nx Blog Nx 22.1 Release: Terminal UI on Windows, Storybook 10, Vitest 4, and more! | Nx Blog The Compounding Effect: How Nx Features Multiply Performance Gains | Nx Blog 10 Monorepo Myths Debunked: Separating Fact from Fiction | Nx Blog Nx Cloud Release: Enterprise Task Analytics | Nx Blog Watch and Rebuild Storybook Dependencies with Nx | Nx Blog Book - React for Enterprise: Timeless Architecture for Enterprise Apps | Nx Blog Beyond Remote Cache: Unlock 70% More CI Performance | Nx Blog Nx 22 Release: Expanding the build platform | Nx Blog What's the Point of Generating All This Code If You Can't Merge It? | Nx Blog
Determine your User Location with Netlify Edge Functions ...
Nicholas Cunningham · 2023-05-27 · via Nx Blog

Today, we will explore how to use @nx/netlify serverless functions to determine a user's location. This can be an incredibly useful feature in various applications, such as customizing user experiences based on their region, displaying localized content, or tracking user demographics for marketing purposes. In this post, we'll walk you through a step-by-step guide on implementing this functionality using @nx/netlify serverless functions.

Before we get started though, here's a video introduction to the new @nx/netlify package:

Step 1: Set up your Nx workspace with Netlify

To get started, you need to have an Nx workspace. If you haven't already, create a new Nx workspace by running:

npx create-nx-workspace user-location --preset=@nx/netlify

This should create a default function inside src/functions/hello/hello.ts, which can be safely deleted if necessary.

Step 2: Create a serverless function

mkdir src/functions/user-location
touch src/functions/user-location/user-location.ts

Step 3: Determine the user's location

To determine the user's location, we will leverage the request.headers object, specifically the x-forwarded-for header containing the user's IP address. We can then use an IP geolocation API like ipapi (https://ipapi.co/) to fetch location data based on this IP address.

Note in Node.js 18, the experimental global fetch API is available by default. If you are using a node version lower than 18 you can install node-fetch to handle API requests:

Now, update the user-location.ts file with the following code:

import { Handler } from "@netlify/functions";
import fetch from "node-fetch"; // Can be removed if node >= 18

export const handler: Handler = async (event, _) => {
  const ip = event.headers["x-forwarded-for"];
  const url = `https://ipapi.co/${ip}/json/`;
  try {
    const response = await fetch(url);
    const data = await response.json();
    return {
      statusCode: 200,
      body: JSON.stringify({
        location: {
          city: data.city,
          region: data.region,
          country: data.country,
        },
      }),
    };
  } catch (error) {
    return { statusCode: 500, body: `Error fetching user location` };
  }
};

Step 4: Deploy your serverless function

When we created our workspace, the initial scaffolding generated a deploy-target inside our project.json.

A target is a specific task you can run for a project.
You can think of it as a script/command that does a specific job. The most common targets are "build", "serve", "test", "lint", "deploy", etc. For more information regarding project.json you can read about it at project-configuration

We can start off by creating our site on Netlify by running:

After you have answered all the prompts your site should be created. A .netlify folder should be created with references to your newly created site.

Now, to deploy your serverless function run:

Finally, navigate to your Netlify site's Functions tab, and you should see your user-location function deployed and ready to use!

For example, ours can be found at: https://644a9b17d0299b00b581b33f--find-user-location.netlify.app/.netlify/functions/user-location

{ "location": { "city": "Miami", "region": "Florida", "country": "US" } }

By following these steps, you've successfully used @nx/netlify serverless function to determine a user's location!

Wrapping up

Never used Nx before? Learn more about Nx here.
Official recipe from Nx
Github example

Learn more

🧠 Nx Docs
👩‍💻 Nx GitHub
💬 Nx Official Discord Server 📹 Nrwl Youtube Channel
🚀 Speed up your CI