Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> JavaScript

ตั้งค่า CDN ให้ส่งไฟล์เดียวกันไม่ว่า URL จะเป็นอะไร (Single File Delivery)

ใช้ CloudFlare Workers หรือ Edge Rules

CDN ปกติ Serve Static Files แต่สามารถ Config ให้ Rewrite/Reroute ได้

วิธีการ

  1. CloudFlare Workers: เขียน Script Intercept Request ทุกตัว Respond ไฟล์ index.html เดียวกัน
  2. Page Rules / Edge Rules: Redirect ทุก Path ไป /index.html (301/302 หรือ Internal Rewrite)
  3. SPA Hosting (Netlify, Vercel, Firebase): ตั้ง /* /index.html 200 ใน _redirects หรือ vercel.json

ตัวอย่าง CloudFlare Worker

addEventListener('fetch', event => {
    event.respondWith(fetch('https://your-bucket/index.html'));
});

เหมาะสำหรับ Single Page Apps (React, Vue, Angular) ที่ใช้ Client-side Routing