웹 성능을 최적화하여 사용자 경험을 향상시키는 것은 중요합니다. 지금 소개드리는 Web Vitals는 구글에서 제공하는 사용자에게 우수한 환경을 제공하는데 필수적인 품질 체크 요소 가이드라고 할 수 있습니다.
현재 프로젝트를 https://pagespeed.web.dev/ 에서 분석한 결과
2524Kb(jpg) -> 1406Kb(webp) -> 1037Kb(webp) jpg에서 avif로 약 58%감축
<img loading="lazy" src="image.jpg" alt="..." />
<iframe loading="lazy" src="video-player.html" title="..."></iframe>loading속성으로 lazy를 부여하여 지연 로딩 처리
<script
defer
type="text/javascript"
src="//www.freeprivacypolicy.com/public/cookie-consent/4.1.0/cookie-consent.js"
charset="UTF-8"
></script>
<script defer type="text/javascript" charset="UTF-8">
// Dom이 로드 된 이후에 실행해주기
document.addEventListener("DOMContentLoaded", function () {
cookieconsent.run({
notice_banner_type: "simple",
consent_type: "express",
palette: "light",
language: "en",
page_load_consent_levels: ["strictly-necessary"],
notice_banner_reject_button_hide: false,
preferences_center_close_button_hide: false,
page_refresh_confirmation_buttons: false,
website_name: "Performance Course",
});
});
</script>
<picture>
<source
media="(max-width: 575px)"
width="575"
height="575"
srcset="images/Hero_Mobile.avif"
/>
<source
media="(min-width: 576px) and (max-width: 960px)"
width="960"
height="770"
srcset="images/Hero_Tablet.avif"
/>
<img
src="images/Hero_Desktop.avif"
width="960"
height="560"
alt="Hero"
/>
</picture>img태그에 width & height속성 부여 -> 미리 설정하여 layout shifting방지
반응형으로 이미지 제공 -> 뷰 포트에 맞는 이미지만 로드
프로젝트 시나리오 : 뷰포트에서 스크롤을 내려서 product section이 보일 때 loadProducts실행 및 무거운연산 처리하기
window.onload = () => {
let productSection = document.querySelector("#all-products");
let status = "idle";
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting && status === "idle") {
status = "loading";
loadProducts().then(() => {
status = "idle";
});
// Simulate heavy operation. It could be a complex price calculation.
for (let i = 0; i < 10000000; i++) {
const temp = Math.sqrt(i) * Math.sqrt(i);
}
}
});
});
observer.observe(productSection);
};scroll이벤트 방식은 scroll이 발생할 때마다 이벤트가 발생하는 문제가 있음 -> Intersection Observer API를 활용하여 특정 지점을 교차할 때 이벤트 발생시키기
alt속성을 부여한 장점
- 이미지 로드 실패 시 대체 텍스트를 제공.
- 검색 엔진에 최적화 할 수 있음.
- 스크린 리더기에 의미를 제공.
<meta name="description" content="hanghae-plus" /><h1>Article Title</h1>
<section>
<h2>Introduction</h2>
<p>...</p>
</section>
<section>
<h2>Overview</h2>
<section>
<h3>Benefits</h3>
<p>...</p>
<section>
<h4>Case Study</h4>
<p>...</p>
</section>
<section>
<h4>Statistics</h4>
<p>...</p>
</section>
</section>
<section>
<h3>Conclusion</h3>
<p>...</p>
</section>
</section>






