๐Ÿฆฅ๋ฆฌ์•กํŠธ lazy loading ์ ์šฉ

Yongยท2023๋…„ 2์›” 14์ผ
0

React

๋ชฉ๋ก ๋ณด๊ธฐ
4/4
post-custom-banner

์–ธ์ œ ํ•„์š”ํ•œ ๊ฒƒ์ผ๊นŒ?

React ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๊ฐœ๋ฐœํ•  ๋•Œ JavaScript ๋ฒˆ๋“ค์˜ ์‚ฌ์ด์ฆˆ๋ฅผ ๊ณ ๋ คํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

ํฐ ๊ทœ๋ชจ์˜ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—๋Š” ์—ฌ๋Ÿฌ๊ฐ€์ง€ ๋ฉ”์„œ๋“œ,๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ, ์œ ํ‹ธ๋ฆฌํ‹ฐ ๋“ฑ์ด ํฌํ•จ๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ์‚ฌ์ด์ฆˆ๊ฐ€ ์ปค์ง€๋ฉด ์ปค์งˆ ์ˆ˜๋ก ์›นํŽ˜์ด์ง€์— ๋ฐฉ๋ฌธํ–ˆ์„ ๋•Œ ๋กœ๋“œํ•ด์•ผํ•  ํŒŒ์ผ๋“ค์˜ ํฌ๊ธฐ๊ฐ€ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ํŽ˜์ด์ง€ ์„ฑ๋Šฅ์— ์ข‹์ง€ ๋ชปํ•œ ์˜ํ–ฅ์„ ๋ฏธ์น˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋•Œ React.lazy ๋ฉ”์„œ๋“œ๋Š” ๋ฒˆ๋“ค์„ ์—ฌ๋Ÿฌ ์ฒญํฌ๋กœ ๋‚˜๋ˆŒ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ณด์—ฌ์ฃผ๊ธฐ์œ„ํ•œ ์ฒญํฌ๋งŒ ๋กœ๋”ฉํ•˜๊ณ  ํ•„์š”์— ๋”ฐ๋ผ์„œ ์ฒญํฌ๋“ค์„ ๋ถˆ๋Ÿฌ์˜ต๋‹ˆ๋‹ค.

Lazy Loading ์ ์šฉ

ํ•จ์ˆ˜ ํ˜ธ์ถœ์— ์ ์šฉํ•˜๊ธฐ

๋จผ์ € ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋กœ ๋ ˆ์ด์ง€ ๋กœ๋”ฉ์„ ์ ์šฉ ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

// lazy loading ์ ์šฉ ์ „
import React from "react";
import {sum} from "./sum";

const MainPage = () => {
  return (
    <div>
      <h1>๋ฉ”์ธํŽ˜์ด์ง€</h1>
      <button onClick={() => alert(sum(1, 1))}>๋กœ๋”ฉ</button>
    </div>
  );
};

export default MainPage;

MainPage ์ปดํฌ๋„ŒํŠธ์—์„œ sum์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ importํ•˜๊ณ  button์„ ํด๋ฆญํ–ˆ์„ ๋•Œ sum๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฒ„ํŠผ์„ ํด๋ฆญํ–ˆ์„ ๋•Œ๋งŒ sum ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋„๋ก ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

// lazy loading ์ ์šฉ ํ›„
import React from "react";
// import {sum} from "./sum";

const MainPage = () => {
  return (
    <div>
      <h1>๋ฉ”์ธํŽ˜์ด์ง€</h1>
      <button
        onClick={() => {
          import("./sum.js").then((module) => {
            alert(module.sum(1, 1));
          });
        }}
      >
        ๋กœ๋”ฉ
      </button>
    </div>
  );
};

export default MainPage;

lazy loading์ด ๋˜๊ณ  ์žˆ๋Š์ง€ ๋ณด๊ธฐ ์œ„ํ•ด์„œ ๊ฐœ๋ฐœ์ž๋„๊ตฌ์˜ ๋„คํŠธ์›Œํฌ ํƒญ์„ ์ฐธ๊ณ  ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.


๋ฒ„ํŠผ์„ ํด๋ฆญํ•˜๋Š” ์ฆ‰์‹œ sum.js ๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๊ฒƒ์„ ์•Œ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋ฆฌ์•กํŠธ ์ปดํฌ๋„ŒํŠธ์— ์ ์šฉํ•˜๊ธฐ

react-router-dom v6๋ฅผ ์˜ˆ์‹œ๋กœ ๋ผ์šฐํŒ…์— ๋ ˆ์ด์ง€ ๋กœ๋”ฉ์„ ์ ์šฉํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.
(์ฝ”๋“œ๋ฅผ ์ค‘๊ฐ„ ์ค‘๊ฐ„ ์ƒ๋žต์œผ๋‹ˆ ์ฐธ๊ณ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค)

import { lazy, Suspense } from "react";

import MainPage from "./pages/main";

// ๊ธฐ์กด์˜ ์ปดํฌ๋„ŒํŠธ import ๋ฐฉ์‹์œผ๋กœ CampaignPage ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
// import CampaignPage from "./pages/campaign";

// lazy๋กœ CampaignPage ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
const CampaignPage = lazy(() => import("./pages/campaign"));

import Root from "./pages/Root";

const router = createBrowserRouter([
  {
    path: "/",
    element: <Root />,
    errorElement: <ErrorPage />,
    children: [
      {
        path: "",
        element: <MainPage />,
      },
      {
        path: "campaign",
        element: (
          // Suspense๋กœ ๋กœ๋”ฉ ๋ถ€๋ถ„ ๊ตฌํ˜„
          <Suspense fallback={<p>๋กœ๋”ฉ...</p>}>
            <CampaignPage />
          </Suspense>
        ),
        loader: async () => {
          const response = await fetch(
            `/api/campaign`
          );
          const resData = await response.json();
          return resData.contentsData;
        },
      },
    ],
  },
]);

lazy๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ Suspense๋ฅผ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜์—ฌ์„œ React์—๊ฒŒ ๋ ˆ์ด์ง€ ๋กœ๋”ฉํ•˜๊ณ  ์žˆ์Œ์„ ์•Œ๋ ค ์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์•„์ง ๋ Œ๋”๋ง ์ค€๋น„๊ฐ€ ๋˜์ง€ ์•Š์•˜์„ ๋•Œ, ๋กœ๋”ฉ ์Šคํ”ผ๋„ˆ ๊ฐ™์€ ์ง€์‹œ๊ธฐ๋ฅผ ๋ Œ๋”๋ง ํ•˜๋„๋ก fallback์— ์ฝ”๋“œ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งˆ๋ฌด๋ฆฌ

Lazy Loading์„ ์ ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ๋‹ค์–‘ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์Œ์—๋Š” ์‘์šฉ๋ฒ•์„ ์•Œ๋ ค๋“œ๋ฆด ์ˆ˜ ์žˆ๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

profile
If I can do it, you can do it.
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€