ข้ามไปเนื้อหาหลัก

Astro 5: ใช้ entry.id แทน entry.slug ใน Content Collections

Astro 5 เปลี่ยน API — entry.slug ถูกลบออก ใช้ entry.id แทนสำหรับ glob loader

ย้ายจาก Astro 4 มา Astro 5 แล้วเจอ error:

Property 'slug' does not exist on type 'DataEntry'

ใน Astro 5 ที่ใช้ glob() loader, ไม่มี .slug อีกต่อไป — ให้ใช้ .id แทน

// Astro 4 (เก่า)
return entries.map(e => ({ params: { slug: e.slug } }));
const href = `/resources/${entry.slug}`;

// Astro 5 (ใหม่)
return entries.map(e => ({ params: { slug: e.id } }));
const href = `/resources/${entry.id}`;

entry.id ใน glob loader คือ path ของไฟล์ relative จาก base directory โดยตัด extension ออก เช่น css-grid.mdid = "css-grid"

สิ่งที่ต้องตรวจ: getStaticPaths(), link href ทุกที่ใน listing pages, และ findIndex() ที่ใช้เปรียบ entry