emptygif
ภาษา: ไทย

ภาษาและเฟรมเวิร์กทั้งหมด

React Router (framework mode, ex-Remix) TypeScript

ตัวอย่างโค้ด TypeScript สำหรับ React Router (framework mode, ex-Remix) ที่ตอบคำขอไปยัง /pixel.gif ด้วย GIF ว่าง ซึ่งถอดรหัสเพียงครั้งเดียวตอนเริ่มทำงาน

React Router (framework mode, ex-Remix) TypeScript

ติดตั้ง
npx create-react-router@latest
ไฟล์
pixel.ts
รัน
npm run dev
// app/routes.ts:  route('/pixel.gif', 'routes/pixel.ts')
// app/routes/pixel.ts - a resource route: a loader and no default export.
const GIF = Uint8Array.from(atob('R0lGODlhAQABAIAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='), (c) => c.charCodeAt(0));

export function loader() {
  return new Response(GIF, {
    headers: {
      'Content-Type': 'image/gif',
      'Content-Length': String(GIF.length),
      'Cache-Control': 'no-store',
    },
  });
}

เรารันตัวอย่างโค้ดนี้และเทียบไบต์ที่ได้กลับมาแล้ว

ตรวจสอบ

curl -sI http://localhost:8080/pixel.gif

เว็บไซต์ทางการ: React Router (framework mode, ex-Remix)

เพิ่มเติมใน TypeScript