0Pricing
Caching Strategies: Redis + CDN + Edge Computing · บทเรียน

ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ

ทำความเข้าใจว่าฟังก์ชันขอบเครือข่ายเชื่อมเข้ากับวงจรชีวิตของคำขอตรงจุดใด และจุดทริกเกอร์ใดที่เปิดให้คุณแก้ไขคำขอและการตอบกลับที่ขอบเครือข่าย

ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ เป็นบทเรียน Caching Strategies: Redis + CDN + Edge Computing ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Caching Strategies: Redis + CDN + Edge Computing และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

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

The Edge Request Lifecycle

When a request reaches a CDN edge, it passes through distinct phases. Edge functions can attach to specific phases to inspect or transform the request and response.

  • Viewer request: as the request arrives
  • Origin request: before fetching from origin
  • Origin response: after origin replies
  • Viewer response: before returning to the user

Viewer Request Trigger

The viewer request trigger runs on every incoming request, even cache hits. Use it for lightweight logic like redirects, header normalization, or A/B routing.

Origin Request Trigger

The origin request trigger runs only on cache misses, just before the edge contacts the origin. It is ideal for rewriting the origin path or choosing a different backend.

function handler(event) {
  var req = event.request;
  req.uri = "/v2" + req.uri;
  return req;
}

Origin Response Trigger

The origin response trigger fires after the origin replies but before the response is cached. Use it to adjust cache headers or inject security headers.

Viewer Response Trigger

The viewer response trigger runs before the response goes to the user, on both hits and misses. Use it for final header tweaks like adding CORS or HSTS.

function handler(event) {
  var res = event.response;
  res.headers["x-edge"] = { value: "true" };
  return res;
}

Triggers and the Cache

Knowing which triggers run on cache hits matters for performance and cost. Viewer triggers run every time; origin triggers run only on misses. Put heavy logic on origin triggers to avoid paying for it on every hit.

Short-Circuiting a Request

An edge function can return a response directly instead of forwarding it, short-circuiting the lifecycle. This is great for maintenance pages, auth gates, or geo-blocks.

function handler(event) {
  var country = event.request.headers["cloudfront-viewer-country"];
  if (country && country.value === "XX") {
    return { statusCode: 403, body: "Blocked" };
  }
  return event.request;
}

Modifying Requests vs Responses

Request triggers can change the URI, headers, and query string before routing. Response triggers can change status codes and headers. Choose the trigger that matches what you need to mutate.

Performance Constraints

Edge functions run inside tight limits on CPU time and memory because they execute on every request at scale. Keep them small, avoid network calls in viewer triggers, and prefer synchronous logic.

  • Tiny scripts equal predictable latency
  • Heavy logic belongs on origin triggers or full edge runtimes

Choosing the Right Trigger

A quick decision guide:

  • Redirect or auth gate: viewer request
  • Rewrite origin path: origin request
  • Adjust cache TTL: origin response
  • Add response headers for everyone: viewer response

Debugging Lifecycle Triggers

Test each trigger in isolation. Log the phase and inspect headers added by the CDN (like the viewer country) to confirm your function runs where you expect.

Quick Check

Confirm your understanding of edge trigger points.

Recap

You learned the four edge function trigger points across the request lifecycle: viewer request, origin request, origin response, and viewer response. You saw which run on cache hits versus misses, how to short-circuit a request, and how to pick the right trigger while respecting edge runtime limits.

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

บทเรียน “ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Caching Strategies: Redis + CDN + Edge Computing ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Caching Strategies: Redis + CDN + Edge Computing มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ”

ทำความเข้าใจว่าฟังก์ชันขอบเครือข่ายเชื่อมเข้ากับวงจรชีวิตของคำขอตรงจุดใด และจุดทริกเกอร์ใดที่เปิดให้คุณแก้ไขคำขอและการตอบกลับที่ขอบเครือข่าย คุณปฏิบัติ Caching Strategies: Redis + CDN + Edge Computing ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Caching Strategies: Redis + CDN + Edge Computing หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Caching Strategies: Redis + CDN + Edge Computing บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Caching Strategies: Redis + CDN + Edge Computing นี้ได้ไหม

ได้ บทเรียน Caching Strategies: Redis + CDN + Edge Computing ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. การแคชเนื้อหาไดนามิกที่ขอบเครือข่าย
  2. ฟังก์ชันไร้เซิร์ฟเวอร์ที่ขอบเครือข่าย
  3. การนำฟังก์ชันที่ขอบเครือข่ายไปใช้งาน
  4. ทริกเกอร์ของฟังก์ชันขอบเครือข่ายและวงจรชีวิตคำขอ
← กลับไปที่ Caching Strategies: Redis + CDN + Edge Computing