dusunax / blog

🏗️ 2024 blog building in progress 🏗️
https://blog-dusuna.vercel.app
0 stars 0 forks source link

(not planned) feat: 썸네일 이미지 로딩 개선_240510 #10

Closed dusunax closed 1 month ago

dusunax commented 1 month ago

Todo


이미지 리사이징_240510

기존 PDFThumbnail 개선하기

기존 썸네일 컴포넌트 문제점

(퍼포먼스 악화) pdf파일 전체를 읽은 후 리스트 컴포넌트 로딩

작업

PDFThumbnail 컴포넌트 as is

"use client";
import { useState } from "react";
import { Document, Page, pdfjs } from "react-pdf"; // 🚫 deprecated 라이브러리

import { FileType } from "@/app/gallery/page";
import ThumbnailTagBox from "./ThumbnailTagBox";

// 🚫 cdn 리소스를 다운로드
pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`; 

export default function PDFThumbnail({ file }: { file: FileType }) {
  const [numPages, setNumPages] = useState<number>(0);

  function onDocumentLoadSuccess({ numPages }: { numPages: number }) {
    setNumPages(numPages); // 🚫 pdf파일이 로딩된 후 연산된 page 수를 출력
  }

  return (
    <div
      className="flex-1 rounded-t-md overflow-hidden h-[230px] p-0 flex justify-center relative"
      style={{ fontSize: 0 }}
    >
      <Document file={file.filePath} onLoadSuccess={onDocumentLoadSuccess}> 
        <Page height={240} pageNumber={1} /> // 🚫 pdf파일이 로딩된 후 첫 페이지를 출력
      </Document>
      <ThumbnailTagBox file={file} numPages={numPages} />
    </div>
  );
}

Item 만들기~

{
  id: string; // uuid
  kind: string; // "pdf" | "image"
  name: string;
  info: {
    pageCount: number;
  };
  thumbnail: {
    base64: string;
    image240: string;
    image420: string; // default
    image640: string;
  };
};

썸네일 구하기

dusunax commented 1 month ago

react-pdf

https://www.npmjs.com/package/react-pdf

React

React version Newest compatible React-PDF version
≥16.8 latest
≥16.3 5.x
≥15.5 4.x

Next.js

If you use Next.js, you may need to add the following to your next.config.js:

module.exports = {
+ webpack: (config) => {
+   config.resolve.alias.canvas = false;

+   return config;
+ },
}
dusunax commented 1 month ago

6월 일정으로 인해, 5월말까지 1차적으로 작업을 끝내야 합니다!