Explore best practices, tutorials, case studies, and insights on leveraging AWS’s vast ecosystem to build, deploy, and manage applications in the cloud
The Design Pattern category explores reusable solutions to common software design challenges, helping developers write efficient, maintainable, and scalable code
The Security category focuses on best practices, tools, and frameworks essential for protecting applications, data, and infrastructure in an increasingly digital world
Comprehensive Guide to Token-Based Authentication & Secure Access Control
Master token-based authentication, OAuth2, API security, and access control strategies like RBAC and ABAC for secure healthcare applications. Learn best practices for token storage, rotation, MFA, and more.
LIKE vs Full-Text Search: SQL Performance and Use Cases
Explore the differences between SQL’s LIKE operator and Full-Text Search. Learn their syntax, performance, use cases, and advanced features for optimizing database queries
10 Advanced JavaScript Tricks Every Developer Should Master
Discover 10 advanced JavaScript techniques, including tail call optimization, currying, proxies, debouncing, throttling, and more. Learn how to write efficient, maintainable, and optimized code for modern web development.
Mastering 7 Types of Loading in Frontend Development: Best Practices and Examples
Explore the 7 key types of loading in frontend development, including eager, lazy, code splitting, skeleton, progressive, infinite scrolling, and shimmer loading. Learn how each works, why they matter, best practices, and React.js code examples to optimize your web performance
3 min read
28 Views
1. Eager Loading
Eager loading fetches resources or data immediately when the application or page is rendered. It works by downloading resources like images, scripts, or data requests as soon as the browser encounters them during page load. This is critical for above-the-fold content or essential functionality.
Why Use It:
To ensure critical resources are available immediately and to avoid delays in rendering key content.
Best Practices:
• Use eager loading only for critical resources.
• Avoid using it for large or non-essential resources to prevent slowing down the initial page load.
React.js Code Example:
import React from "react";
const EagerLoadedImage = () => {
return (
<img
src="/images/hero.jpg"
alt="Hero"
style={{ width: "100%", height: "auto" }}
/>
);
};
export default EagerLoadedImage;
2. Lazy Loading
Lazy loading delays the loading of resources until they are needed, such as when they are scrolled into view. It works by using browser APIs like IntersectionObserver or native attributes (loading=”lazy”) to load resources only when they become visible.
Why Use It:
To improve initial load time and reduce bandwidth usage by loading resources only when necessary.
Best Practices:
• Use lazy loading for images, videos, or components that aren’t visible above the fold.
• Combine with responsive loading techniques for better performance.
React.js Code Example:
import React, { useState, useEffect } from "react";
const LazyLoadedImage = () => {
const [isVisible, setIsVisible] = useState(false);
const imgRef = React.useRef();
useEffect(() => {
const observer = new IntersectionObserver(([entry]) => {
if (entry.isIntersecting) {
setIsVisible(true);
}
});
if (imgRef.current) {
observer.observe(imgRef.current);
}
return () => {
if (imgRef.current) {
observer.unobserve(imgRef.current);
}
};
}, []);
return (
<div ref={imgRef} style={{ minHeight: "300px" }}>
{isVisible && (
<img src="/images/lazy-image.jpg" alt="Lazy" style={{ width: "100%" }} />
)}
</div>
);
};
export default LazyLoadedImage;
3. Code Splitting
Code splitting divides your code into smaller chunks that are loaded on demand rather than in one large bundle. It works using bundlers like Webpack or Vite that automatically split your JavaScript files into smaller bundles.
Why Use It:
To reduce the size of the initial load and improve performance, especially for large applications.
Best Practices:
• Use dynamic imports for routes or components that aren’t needed during the initial load.
• Optimize with React’s React.lazy and Suspense for a seamless user experience.
React.js Code Example:
import React, { Suspense, lazy } from "react";
const LazyComponent = lazy(() => import("./LazyComponent"));
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<LazyComponent />
</Suspense>
);
};
export default App;
4. Skeleton Loading
Skeleton loading uses a placeholder to mimic the structure of the content being loaded. It works by displaying a skeleton UI that disappears when the actual content is rendered.
Why Use It:
To improve perceived performance and provide a better user experience during loading states.
Best Practices:
• Use skeleton loading for components or data that take longer to load, such as user profiles or images.
• Keep the skeleton design simple and visually similar to the final content.
React.js Code Example:
import React, { useState, useEffect } from "react";
const SkeletonLoader = () => {
const [isLoading, setIsLoading] = useState(true);
useEffect(() => {
const timer = setTimeout(() => setIsLoading(false), 2000);
return () => clearTimeout(timer);
}, []);
return isLoading ? (
<div className="skeleton" style={{ width: "100%", height: "200px", background: "#ddd" }} />
) : (
<img src="/images/content.jpg" alt="Content" style={{ width: "100%" }} />
);
};
export default SkeletonLoader;
5. Progressive Loading
Progressive loading involves loading resources in stages, such as downloading lower-quality images first and then replacing them with higher-quality versions. It works by using techniques like progressive JPEGs or chunked data transfer.
Why Use It:
To provide users with partial content quickly, improving the perceived loading speed.
Best Practices:
• Use progressive images for media-heavy pages.
• Combine with lazy loading to load higher-quality resources as needed.
React.js Code Example:
import React, { useState } from "react";
const ProgressiveImage = ({ lowResSrc, highResSrc }) => {
const [loaded, setLoaded] = useState(false);
return (
<div>
<img
src={lowResSrc}
alt="Low Res"
style={{ filter: "blur(10px)", position: "absolute" }}
/>
<img
src={highResSrc}
alt="High Res"
onLoad={() => setLoaded(true)}
style={{ opacity: loaded ? 1 : 0, transition: "opacity 0.5s" }}
/>
</div>
);
};
export default ProgressiveImage;
6. Infinite Scrolling
Infinite scrolling loads content continuously as users scroll down the page. It works by dynamically fetching and appending new data to the DOM when the user reaches the bottom of the page.
Why Use It:
To improve user experience in applications with large datasets, like social media feeds or e-commerce.
Best Practices:
• Use for content-heavy pages where users expect continuous scrolling.
• Implement proper cleanup to avoid performance issues.
React.js Code Example:
import React, { useState, useEffect } from "react";
const InfiniteScroll = ({ fetchData }) => {
const [data, setData] = useState([]);
useEffect(() => {
const handleScroll = () => {
if (window.innerHeight + window.scrollY >= document.body.offsetHeight) {
fetchData().then(newData => setData(prev => [...prev, ...newData]));
}
};
window.addEventListener("scroll", handleScroll);
return () => window.removeEventListener("scroll", handleScroll);
}, [fetchData]);
return (
<ul>
{data.map((item, index) => (
<li key={index}>{item}</li>
))}
</ul>
);
7. Shimmer Loading
Shimmer loading shows an animated gradient placeholder while content is being loaded. It works by displaying shimmering effects to simulate loading activity.
Why Use It:
To create a more dynamic and visually appealing loading experience.
Best Practices:
• Use for components like cards or lists to keep the user engaged.
• Ensure the shimmer animation is lightweight and doesn’t hinder performance.
React.js Code Example:
import React from "react";
const ShimmerLoader = () => {
return (
<div
style={{
width: "100%",
height: "200px",
background: "linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%)",
backgroundSize: "200% 100%",
animation: "shimmer 1.5s infinite",
}}
/>
);
};
export default ShimmerLoader;
Related Posts
Discover 10 advanced JavaScript techniques, including tail call optimization, currying, proxies, debouncing, throttling, and more. Learn how to write efficient, maintainable, and optimized code for modern web development.
Prepare for your Senior Website Frontend Engineer interview with these 10 advanced questions and answers covering component architecture, state management, performance optimization, accessibility, and more. Ace your next interview!
Learn how to create stunning gradient text animations with smooth hover effects using simple CSS. Perfect for beginners, this guide provides step-by-step instructions and customization tips to enhance your website design
The FoldedTag component is a versatile UI element designed to add a modern, folded-corner aesthetic to your React applications. By leveraging the power of CSS clip-path, this component creates intricate shapes like folded corners and pentagons, ensuring a unique and eye-catching design.
In this article, we’ll break down a beautifully crafted CSS technique that creates a gradient border effect using a combination of pseudo-elements, gradients, and advanced masking techniques. Let’s dive into the mechanics and understand how this effect works.
Subscribe to our newsletter
Get the latest posts delivered right to your inbox