ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [GDSC/Front-End] React lazy loading
    Front-end/GDSC 2022. 11. 8. 15:59

     

     

    ํŽ˜์ด์Šค๋ถ์˜ lazy loading

     

    ๐Ÿ“Œ lazy loading

    ํ•„์š” ์‹œ์ ๊นŒ์ง€ ๊ฐ์ฒด์˜ ์ดˆ๊ธฐํ™”๋ฅผ ์—ฐ๊ธฐ์‹œํ‚ค๊ธฐ ์œ„ํ•ด ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ์— ํ”ํžˆ ์‚ฌ์šฉ๋˜๋Š” ๋””์ž์ธ ํŒจํ„ด์˜ ํ•˜๋‚˜์ด๋‹ค.

    ํŽ˜์ด์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์‹œ์ ์— ๋‹น์žฅ ํ•„์š”ํ•˜์ง€ ์•Š์€ ๋ฆฌ์†Œ์Šค๋“ค์€ ๋‚˜์ค‘์— ๋ถˆ๋Ÿฌ์˜ค์ž!

     

    ๐Ÿ“Œ React image placeholder

    ๋Œ€์—ญํญ ์‚ฌ์šฉ๋Ÿ‰์„ ์ค„์ด๊ณ  ์‚ฌ์šฉ์ž ๊ฒฝํ—˜์„ ๊ฐœ์„ ํ•˜๋ฉฐ ํŽ˜์ด์ง€ ๋กœ๋”ฉ ์†๋„๋ฅผ ๋†’์ด๊ธฐ ์œ„ํ•ด ๋ทฐํฌํŠธ ์™ธ๋ถ€์—์„œ ์ด๋ฏธ์ง€ ๋กœ๋“œ๋ฅผ ์—ฐ๊ธฐํ•œ๋‹ค.

     

     

    1๏ธโƒฃ ์ฝ”๋“œ ์ถ”๊ฐ€

    ์ด๋ฏธ์ง€๋ฅผ ์ง€์—ฐ๋กœ๋”ฉ ํ•˜๋Š” ๋ฐฉ๋ฒ• ์ค‘ ๊ฐ€์žฅ ์‰ฌ์šด ๊ฒƒ์€ img ์š”์†Œ ์•ˆ์— loading=" lazy" ์†์„ฑ์„ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

    <img src="image.jpg" alt=" " loading="lazy" />

     

     

    2๏ธโƒฃ ์ด๋ฏธ์ง€ ๋ธ”๋Ÿฌ ์ฒ˜๋ฆฌ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ

    React Lazy Load Image Component ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์‚ฌ์šฉ

     

    react-lazy-load-image-component

    React Component to lazy load images using a HOC to track window scroll position.. Latest version: 1.5.6, last published: 12 hours ago. Start using react-lazy-load-image-component in your project by running `npm i react-lazy-load-image-component`. There are

    www.npmjs.com

     

     

    GitHub - Evavic44/react-lazyload: ๐Ÿฆฅ A tutorial to illustrate how to lazyload images in a react app using react lazy load imag

    ๐Ÿฆฅ A tutorial to illustrate how to lazyload images in a react app using react lazy load image component library - GitHub - Evavic44/react-lazyload: ๐Ÿฆฅ A tutorial to illustrate how to lazyload images ...

    github.com

     

     

     

    3๏ธโƒฃ intersection observer ์‚ฌ์šฉ

    ์ง€์ •๋œ ์˜์—ญ์— ๋Œ€ํ•ด ๊ต์ฐจ(intersection) ํ–ˆ๋Š”์ง€๋ฅผ ๊ฐ์ง€ํ•˜๊ณ  ํ•ด๋‹น ์‹œ์ ์— ์›ํ•˜๋Š” ์ž‘์—…์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ค€๋‹ค. 

     

    react lazyloading ์ด๋ฏธ์ง€ ์ปดํฌ๋„ŒํŠธ ๊ตฌํ˜„ํ•˜๊ธฐ

    nukeguys.github.io

     

     

    Intersection Observer API - Web APIs | MDN

    The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document's viewport.

    developer.mozilla.org

     

     

     

    ๐Ÿ‘‡ DOMContentLoaded ์ด๋ฒคํŠธ์—์„œ lazy์˜ ๋“ฑ๊ธ‰์ด ์žˆ๋Š” ๋ชจ๋“  <img> ์š”์†Œ์— ๋Œ€ํ•œ DOM์„ ์กฐํšŒํ•œ๋‹ค.

    document.addEventListener("DOMContentLoaded", function() {
      var lazyImages = [].slice.call(document.querySelectorAll("img.lazy"));
    
      if ("IntersectionObserver" in window) {
        let lazyImageObserver = new IntersectionObserver(function(entries, observer) {
          entries.forEach(function(entry) {
            if (entry.isIntersecting) {
              let lazyImage = entry.target;
              lazyImage.src = lazyImage.dataset.src;
              lazyImage.srcset = lazyImage.dataset.srcset;
              lazyImage.classList.remove("lazy");
              lazyImageObserver.unobserve(lazyImage);
            }
          });
        });
    
        lazyImages.forEach(function(lazyImage) {
          lazyImageObserver.observe(lazyImage);
        });
      } else {
        // Possibly fall back to event handlers here
      }
    });

     

    ๐Ÿ‘‡ img ํƒœ๊ทธ์˜ class 

    <img class="lazy" src="placeholder-image.jpg" 
    data-src="image-to-lazy-load-1x.jpg" 
    data-srcset="image-to-lazy-load-2x.jpg 2x, image-to-lazy-load-1x.jpg 1x" 
    alt="I'm an image!">
    1. JavaScript์—์„œ ์š”์†Œ๋ฅผ ์„ ํƒํ•˜๋Š” class ์†์„ฑ
    2. ํŽ˜์ด์ง€๊ฐ€ ์ฒ˜์Œ ๋กœ๋“œ๋  ๋•Œ ํ‘œ์‹œ๋  ์ž๋ฆฌ ํ‘œ์‹œ์ž ์ด๋ฏธ์ง€๋ฅผ ์ฐธ์กฐํ•˜๋Š” src ์†์„ฑ
    3. ์š”์†Œ๊ฐ€ ๋ทฐํฌํŠธ์— ์žˆ์„ ๋•Œ ๋กœ๋“œํ•  ์ด๋ฏธ์ง€์˜ URL์„ ํฌํ•จํ•˜๋Š” ์ž๋ฆฌ ํ‘œ์‹œ์ž ์†์„ฑ์ธ data-src ๋ฐ data-srcset ์†์„ฑ

     

     

    4๏ธโƒฃ CSS ์ด๋ฏธ์ง€

     

    ์ด๋ฏธ์ง€ ์ง€์—ฐ ๋กœ๋”ฉ

    ์ด ๊ฒŒ์‹œ๋ฌผ์—์„œ๋Š” ์ง€์—ฐ ๋กœ๋”ฉ๊ณผ ์ด๋ฏธ์ง€ ์ง€์—ฐ ๋กœ๋”ฉ ์‹œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์˜ต์…˜์— ๋Œ€ํ•ด ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค.

    web.dev

    - CSS background-image ์†์„ฑ(๋ฐ ๊ธฐํƒ€ ์†์„ฑ)์„ ํ†ตํ•ด ์ด๋ฏธ์ง€๋ฅผ ํ˜ธ์ถœ

     

    -  ๋ฌธ์„œ์™€ CSS ๊ฐ์ฒด ๋ชจ๋ธ ๋ฐ ๋ Œ๋” ํŠธ๋ฆฌ๊ฐ€ ๊ตฌ์ถ•๋˜๋ฉด ๋ธŒ๋ผ์šฐ์ €๋Š” ์™ธ๋ถ€ ๋ฆฌ์†Œ์Šค๋ฅผ ์š”์ฒญํ•˜๊ธฐ ์ „์— CSS๊ฐ€ ๋ฌธ์„œ์— ์–ด๋–ป๊ฒŒ ์ ์šฉ๋˜๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.

    ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์™ธ๋ถ€ ๋ฆฌ์†Œ์Šค๋ฅผ ํฌํ•จํ•˜๋Š” CSS ๊ทœ์น™์ด ํ˜„์žฌ ๊ตฌ์„ฑ๋œ ๋ฌธ์„œ์— ์ ์šฉ๋˜์ง€ ์•Š๋Š”๋‹ค๊ณ  ํŒ๋‹จํ•œ ๊ฒฝ์šฐ ๋ธŒ๋ผ์šฐ์ €๋Š” ์ด๋ฅผ ์š”์ฒญํ•˜์ง€ ์•Š์Œ

     

    - JavaScript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์š”์†Œ๊ฐ€ ๋ทฐํฌํŠธ ๋‚ด์— ์žˆ๋Š”์ง€ ํ™•์ธํ•œ ๋‹ค์Œ ๋ฐฐ๊ฒฝ ์ด๋ฏธ์ง€๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ์Šคํƒ€์ผ๋ง์„ ์ ์šฉํ•˜๋Š” ์š”์†Œ์— ํด๋ž˜์Šค๋ฅผ ์ ์šฉํ•˜์—ฌ CSS์—์„œ ์ด๋ฏธ์ง€ ๋กœ๋“œ๋ฅผ ์—ฐ๊ธฐํ•˜๋Š” ๋ฐ ์ด ์ถ”์ธก ๋™์ž‘์„ ์‚ฌ์šฉ

     

    <div class="lazy-background">
      <h1>Here's a hero heading to get your attention!</h1>
      <p>Here's hero copy to convince you to buy a thing!</p>
      <a href="/buy-a-thing">Buy a thing!</a>
    </div>

     

    ๐Ÿ‘‡ ์š”์†Œ๊ฐ€ ๋ทฐํฌํŠธ์— ์žˆ์„ ๋•Œ visible ํด๋ž˜์Šค๋ฅผ ํ†ตํ•ด div.lazy-background ์š”์†Œ์˜ background-image ์†์„ฑ์„ ๊ฒฉ๋ฆฌํ•  ์ˆ˜ ์žˆ๋‹ค

    .lazy-background {
      background-image: url("hero-placeholder.jpg"); /* Placeholder image */
    }
    
    .lazy-background.visible {
      background-image: url("hero.jpg"); /* The final image */
    }

     

    ๐Ÿ‘‡ ์š”์†Œ๊ฐ€ ๋ทฐํฌํŠธ์— ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ณ (Intersection Observer) ์ด๋ฏธ์ง€๋ฅผ ๋กœ๋“œํ•˜๋Š” ์‹œ์ ์—์„œ visible ํด๋ž˜์Šค๋ฅผ div.lazy-background ์š”์†Œ์— ์ถ”๊ฐ€

    document.addEventListener("DOMContentLoaded", function() {
      var lazyBackgrounds = [].slice.call(document.querySelectorAll(".lazy-background"));
    
      if ("IntersectionObserver" in window) {
        let lazyBackgroundObserver = new IntersectionObserver(function(entries, observer) {
          entries.forEach(function(entry) {
            if (entry.isIntersecting) {
              entry.target.classList.add("visible");
              lazyBackgroundObserver.unobserve(entry.target);
            }
          });
        });
    
        lazyBackgrounds.forEach(function(lazyBackground) {
          lazyBackgroundObserver.observe(lazyBackground);
        });
      }
    });

     

        <link rel="stylesheet" href="/style.css" />
        <script src="lazy.js"></script>
      </head>
      <body>
        <div class="wrapper">
          <div class="lazy-background one"></div>
          <div class="lazy-background two"></div>
          <div class="lazy-background three"></div>
        </div>
      </body>

     

     

     

    'Front-end > GDSC' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

    [GDSC/์ŠˆํŒŒ์ธ๋“œ] ๊ฐœ๋ฐœ ์ผ์ง€ #1-2์ฃผ ์ฐจ  (0) 2023.01.09
    [GDSC/Front-End] JWT ๋กœ๊ทธ์ธ  (0) 2022.12.06
Designed by Tistory.