0Pricing
Edge Computing with Cloudflare Workers & Deno · บทเรียน

พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น

ใช้ความสามารถของระบบใกล้ผู้ใช้เพื่อส่งมอบเนื้อหาและบริการที่ปรับตามตำแหน่งของผู้ใช้

พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น เป็นบทเรียน Edge Computing with Cloudflare Workers & Deno ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Edge Computing with Cloudflare Workers & Deno และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Edge Geolocation & Localization

Ever wonder how websites seem to know your location or prefer your local language? That's often thanks to geolocation and localization, especially when powered by the edge!

Geolocation is finding a user's physical location. Localization is adapting content (like language, currency, or date formats) based on that location.

The edge is perfect for this! By processing requests closer to the user, we can quickly determine location and deliver tailored experiences with minimal delay.

How Edge Knows Your Location

When you connect to a website, your device sends an IP address. Edge networks like Cloudflare use this IP to perform a quick lookup, determining your approximate geographic location.

This information is then made available to your code running on the edge. It's fast, automatic, and doesn't rely on browser-based GPS, which often requires user permission.

Access Geolocation Data

Cloudflare Workers provide geolocation data through the request.cf object. This object is automatically populated for every incoming request hitting the edge.

It contains useful properties like country code, city, region, and even timezone, making it incredibly easy to build location-aware applications without extra APIs.

Demo: Simple Country Detection

Let's write a simple Cloudflare Worker that detects the user's country and responds with a greeting. This uses the request.cf.country property.

Try running this example to see your country code!

export default {
  async fetch(request) {
    const country = request.cf.country; // e.g., "US", "DE", "JP"
    return new Response(`Hello from ${country || 'an unknown location'}!`);
  }
};

Detailed Location Info

The request.cf object offers more than just the country code. You can access more granular details for deeper localization:

  • cf.city: User's city (e.g., "San Francisco")
  • cf.region: User's region/state (e.g., "CA")
  • cf.timezone: User's timezone (e.g., "America/Los_Angeles")
  • cf.latitude, cf.longitude: Approximate coordinates

Use these to create highly personalized experiences.

Demo: Personalized Greeting

Let's enhance our Worker to provide a more personalized greeting using city and country information from the request.cf object.

This demonstrates how easily you can use multiple geolocation data points.

export default {
  async fetch(request) {
    const { city, country } = request.cf;
    if (city && country) {
      return new Response(`Welcome, traveler from ${city}, ${country}!`);
    } else if (country) {
      return new Response(`Welcome, traveler from ${country}!`);
    } else {
      return new Response('Welcome, traveler!');
    }
  }
};

Localization: Language & Content

Beyond simple greetings, you can use geolocation data to serve different versions of your content. This is key for true localization.

  • Language: Determine the user's preferred language (e.g., from Accept-Language header or cf.country) and serve translated text.
  • Currency: Display prices in the local currency based on the user's country.
  • Content Variants: Show different promotions or product availability based on regional restrictions.

Demo: Dynamic Language

Here's an example of how a Worker can provide a dynamic welcome message based on the user's country. For simplicity, we'll map a few countries to specific languages.

In a real application, you'd integrate with a more robust i18n (internationalization) library.

export default {
  async fetch(request) {
    const country = request.cf.country;
    let message;
    switch (country) {
      case 'US':
        message = 'Hello!';
        break;
      case 'FR':
        message = 'Bonjour!';
        break;
      case 'DE':
        message = 'Guten Tag!';
        break;
      default:
        message = 'Welcome!';
    }
    return new Response(message);
  }
};

Best Practices for Localization

To make your edge localization robust and user-friendly, consider these tips:

  • Fallback Content: Always have a default language or content version for unknown locations.
  • User Overrides: Allow users to manually select their preferred language/location, overriding the auto-detected setting.
  • Caching: Cache localized content variants effectively to maximize performance benefits.
  • Privacy: Be mindful of privacy regulations (like GDPR) when using location data.

Quick Check: Edge Localization

Edge computing significantly enhances geolocation and localization. Which of the following statements accurately describe benefits or methods of using edge capabilities for localization?

Recap & Next Steps

In this lesson, we explored how geolocation and localization are powerful capabilities at the edge.

  • You learned how edge networks provide location data automatically.
  • We saw how to access this data in Cloudflare Workers using the request.cf object.
  • You built Workers that deliver personalized and localized content based on user location.

Leveraging the edge for these features greatly improves user experience and application performance. Keep experimenting with these concepts to build truly global applications!

คำถามที่พบบ่อย

บทเรียน “พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Edge Computing with Cloudflare Workers & Deno ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Edge Computing with Cloudflare Workers & Deno มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น”

ใช้ความสามารถของระบบใกล้ผู้ใช้เพื่อส่งมอบเนื้อหาและบริการที่ปรับตามตำแหน่งของผู้ใช้ คุณปฏิบัติ Edge Computing with Cloudflare Workers & Deno ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Edge Computing with Cloudflare Workers & Deno หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Edge Computing with Cloudflare Workers & Deno บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Edge Computing with Cloudflare Workers & Deno นี้ได้ไหม

ได้ บทเรียน Edge Computing with Cloudflare Workers & Deno ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ไมโครเซอร์วิสใกล้ผู้ใช้
  2. สถาปัตยกรรมขับเคลื่อนด้วยเหตุการณ์
  3. พิกัดภูมิศาสตร์และการปรับให้เหมาะกับท้องถิ่น
  4. ออบเจ็กต์คงทนและการประสานงานที่มีสถานะ
← กลับไปที่ Edge Computing with Cloudflare Workers & Deno