emptygif
Ngôn ngữ: Tiếng Việt

Tất cả ngôn ngữ và framework

React Router (framework mode, ex-Remix) TypeScript

Đoạn mã TypeScript cho React Router (framework mode, ex-Remix), trả lời các yêu cầu tới /pixel.gif bằng GIF rỗng, được giải mã một lần khi khởi động.

React Router (framework mode, ex-Remix) TypeScript

Cài đặt
npx create-react-router@latest
Tệp
pixel.ts
Chạy
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',
    },
  });
}

Chúng tôi đã chạy đoạn mã này và so sánh các byte nó trả về.

Kiểm tra

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

Trang chính thức: React Router (framework mode, ex-Remix)

Thêm với TypeScript