SlideShare a Scribd company logo
1 of 15
Lazy Loading Website Assets
A Technique to Improve Your Page Speed
https://love2dev.com/blog/lazy-loading/
https://twitter.com/CHrisLove
https:./facebook.com/Love2Dev/
https://www.linkedin.com/company/love2dev/
Chris Love
Progressive Web
Apps and SEO
Specialist
Website speed is a crucial aspect of on page SEO everyone can
control. Your goal is to be interactive in under 3 seconds, even
on a basic phone over a 3G connection.
However, most web sites have so many requests and large
payloads this time limit or budget cannot be achieved. In fact, the
average web page takes 22 seconds to load, according to
Google's research.
Why Lazy Load
Website Speed
• Lazy-Load Content Scenarios
• What is IntersectionObever?
• Pagination/Continuous Scroll
• Using the History API
• Loading Scripts As Needed
Lazy-Load Content Scenarios
For simplicity I will just call it a blog. These sites
typically need HTML, CSS, JavaScript, images
and custom fonts. They also tend to be sites you
want free organic traffic from Google, which
means SEO is important.
Content Focused Sites
Complex web applications can be a little more
delicate because they lean on more application
logic. This means they require more JavaScript
files. When these files are loaded matters. While
you can use IntersectionObserver for this, your
application needs a good technique to
dynamically load scripts as needed.
Web Apps
Images require bandwidth and may not even be seen. Deferring
their request means you can prioritize other content, like HTML
and CSS.
Just make sure the images are available to Google.
The noscript tag is a semantic mechanism to tell the browser to
only render the content inside the element if the browser (user
agent) does not support JavaScript. Most visitors will have
JavaScript turned on, so they won't trigger the image to load
initially. But as you will shortly see it will when the non-noscript
image is visible.
Lazy Load Images
Image here
Your Business
Example Code
<img data-srcset="img/example-869x361.jpg 869w, ..."
data-src="img/example-869x361.jpg" class="lazy-image"
sizes="(max-width: 480px) 90vw, 30vw" alt=“…">
<noscript><img src="img/example-869x361.jpg"
alt=“…"></noscript>
Lazy Load Images
The images are not requested
until the data-src and data-srcset
attributes are converted to the
normal src and srcset attributes.
This is done using
IntersectionObserver.
Image here
What is IntersectionObserver?
Triggers a callback when target elements come into view.
How it Works
Supported by all modern browsers and there is a stable polyfil
you can dynamically load for those that don't.
Browser Support
IntersectionObserver
This API triggers a callback when target elements come into
view. This callback allows you to execute code, which in our
scenario is to load assets as needed.
Load as It Enters
Viewport
As observed content approaches the visible viewport the
IntersectionObserver API triggers a callback method. Inside this
method you can execute code to update and load assets on
demand.
Example Code
var config = {
// If the image gets within 50px in the Y axis, start the
download.
rootMargin: '50px 0px',
threshold: 0.1
};
imgObserver = new IntersectionObserver(showImage, config);
imgObserver.observe(yourImage);
Pagination /
Continuous Scroll
A common tactic on news related sites in the past couple of years.
This is where you scroll down an article and as you reach the bottom
a new article is rendered.
Solutions:
• IntersectionObserver
• History API
Using the History API
The main advantage it provides is the ability to
change the URL in the address bar without
triggering a server request that causes a
complete page reload. It works much like the
hash fragment technique, but uses a shareable
URL.
Web Apps
Loading Scripts As Needed
JavaScript is the single biggest cause of poor
page render times. By offloading scripts you
remove them from the rendering process.
Improves Page Load
Create a script element for each script and
appending it to the DOM. Once appended the
browser loads the script just as if it were part of
the initial markup.
Add Scripts on Demand
Requires a little finesse, but can make your UX
so much better, which should improve customer
satisfaction and retention while reducing help
desk calls.
Complex Web Apps
Load minimal script footprint. Gets application
going.
Boot Up Experience
300 x 50 px
Thank you!
Love2Dev

More Related Content

What's hot

What's hot (20)

A Simpler Web App Architecture (jDays 2016)
A Simpler Web App Architecture (jDays 2016)A Simpler Web App Architecture (jDays 2016)
A Simpler Web App Architecture (jDays 2016)
 
10 things you can do to speed up your web app today 2016
10 things you can do to speed up your web app today 201610 things you can do to speed up your web app today 2016
10 things you can do to speed up your web app today 2016
 
Introduction core web vitals
Introduction core web vitalsIntroduction core web vitals
Introduction core web vitals
 
Develop a vanilla.js spa you and your customers will love
Develop a vanilla.js spa you and your customers will loveDevelop a vanilla.js spa you and your customers will love
Develop a vanilla.js spa you and your customers will love
 
Pros and Cons of developing a Thick Clientside App
Pros and Cons of developing a Thick Clientside AppPros and Cons of developing a Thick Clientside App
Pros and Cons of developing a Thick Clientside App
 
DeepCrawl Webinar: Performing SEO on the Edge
DeepCrawl Webinar: Performing SEO on the EdgeDeepCrawl Webinar: Performing SEO on the Edge
DeepCrawl Webinar: Performing SEO on the Edge
 
Single page application
Single page applicationSingle page application
Single page application
 
Website speed optimization techniques
Website speed optimization techniquesWebsite speed optimization techniques
Website speed optimization techniques
 
JavaScript front end performance optimizations
JavaScript front end performance optimizationsJavaScript front end performance optimizations
JavaScript front end performance optimizations
 
Core web vitals is the thing you should focus on if you own a website in 2021
Core web vitals is the thing you should focus on if you own a website in 2021Core web vitals is the thing you should focus on if you own a website in 2021
Core web vitals is the thing you should focus on if you own a website in 2021
 
Advanced front end debugging with ms edge and ms tools
Advanced front end debugging with ms edge and ms toolsAdvanced front end debugging with ms edge and ms tools
Advanced front end debugging with ms edge and ms tools
 
How i acheived a pretty good google page speed insights score
How i acheived a pretty good google page speed insights scoreHow i acheived a pretty good google page speed insights score
How i acheived a pretty good google page speed insights score
 
Core Web Vitals Optimization for any website, especially WordPress
Core Web Vitals Optimization for any website, especially WordPressCore Web Vitals Optimization for any website, especially WordPress
Core Web Vitals Optimization for any website, especially WordPress
 
Prioritize your critical css and images to render your site fast velocity ny...
Prioritize your critical css and images to render your site fast  velocity ny...Prioritize your critical css and images to render your site fast  velocity ny...
Prioritize your critical css and images to render your site fast velocity ny...
 
Server rendering-talk
Server rendering-talkServer rendering-talk
Server rendering-talk
 
Web Performance & Operation - Pagespeed
Web Performance & Operation - PagespeedWeb Performance & Operation - Pagespeed
Web Performance & Operation - Pagespeed
 
Performace optimization (increase website speed)
Performace optimization (increase website speed)Performace optimization (increase website speed)
Performace optimization (increase website speed)
 
Website speed optimization guide for technically advanced webmasters
Website speed optimization guide for technically advanced webmastersWebsite speed optimization guide for technically advanced webmasters
Website speed optimization guide for technically advanced webmasters
 
Implementing a Responsive Image Strategy
Implementing a Responsive Image StrategyImplementing a Responsive Image Strategy
Implementing a Responsive Image Strategy
 
BrightonSEO 2019 - Edge SEO - Using CDNs To Perform SEO On The Edge
BrightonSEO 2019 - Edge SEO - Using CDNs To Perform SEO On The EdgeBrightonSEO 2019 - Edge SEO - Using CDNs To Perform SEO On The Edge
BrightonSEO 2019 - Edge SEO - Using CDNs To Perform SEO On The Edge
 

Similar to Lazy load Website Assets

Single Page WebApp Architecture
Single Page WebApp ArchitectureSingle Page WebApp Architecture
Single Page WebApp Architecture
Morgan Cheng
 
Build single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEMBuild single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEM
connectwebex
 
Pagespeed what, why, and how it works
Pagespeed   what, why, and how it worksPagespeed   what, why, and how it works
Pagespeed what, why, and how it works
Ilya Grigorik
 
Website Performance
Website PerformanceWebsite Performance
Website Performance
Hugo Fonseca
 
Front end optimization
Front end optimizationFront end optimization
Front end optimization
Abhishek Anand
 
Web development today
Web development todayWeb development today
Web development today
Jaydev Gajera
 

Similar to Lazy load Website Assets (20)

Presentation Tier optimizations
Presentation Tier optimizationsPresentation Tier optimizations
Presentation Tier optimizations
 
Optimizing Your WordPress Site: Why speed matters, and how to get there
Optimizing Your WordPress Site: Why speed matters, and how to get thereOptimizing Your WordPress Site: Why speed matters, and how to get there
Optimizing Your WordPress Site: Why speed matters, and how to get there
 
Developing High Performance Web Apps
Developing High Performance Web AppsDeveloping High Performance Web Apps
Developing High Performance Web Apps
 
Responsive Enhancement
Responsive EnhancementResponsive Enhancement
Responsive Enhancement
 
Single Page WebApp Architecture
Single Page WebApp ArchitectureSingle Page WebApp Architecture
Single Page WebApp Architecture
 
Myths & true stories about JavaScript for SEO
Myths & true stories about JavaScript for SEOMyths & true stories about JavaScript for SEO
Myths & true stories about JavaScript for SEO
 
Build single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEMBuild single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEM
 
Build single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEMBuild single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEM
 
Build single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEMBuild single page applications using AngularJS on AEM
Build single page applications using AngularJS on AEM
 
Building high performing web pages
Building high performing web pagesBuilding high performing web pages
Building high performing web pages
 
Pagespeed what, why, and how it works
Pagespeed   what, why, and how it worksPagespeed   what, why, and how it works
Pagespeed what, why, and how it works
 
implement lighthouse-ci with your web development workflow
implement lighthouse-ci with your web development workflowimplement lighthouse-ci with your web development workflow
implement lighthouse-ci with your web development workflow
 
Website Performance
Website PerformanceWebsite Performance
Website Performance
 
12 GoMeasure (sg and kl) - page speed light speed path to conversions - joh...
12   GoMeasure (sg and kl) - page speed light speed path to conversions - joh...12   GoMeasure (sg and kl) - page speed light speed path to conversions - joh...
12 GoMeasure (sg and kl) - page speed light speed path to conversions - joh...
 
Ui dev@naukri-2011
Ui dev@naukri-2011Ui dev@naukri-2011
Ui dev@naukri-2011
 
Minimize website page loading time – 20+ advanced SEO tips
Minimize website page loading time – 20+ advanced SEO tipsMinimize website page loading time – 20+ advanced SEO tips
Minimize website page loading time – 20+ advanced SEO tips
 
Front end optimization
Front end optimizationFront end optimization
Front end optimization
 
PrairieDevCon 2014 - Web Doesn't Mean Slow
PrairieDevCon 2014 -  Web Doesn't Mean SlowPrairieDevCon 2014 -  Web Doesn't Mean Slow
PrairieDevCon 2014 - Web Doesn't Mean Slow
 
Web development today
Web development todayWeb development today
Web development today
 
Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011Developing High Performance Web Apps - CodeMash 2011
Developing High Performance Web Apps - CodeMash 2011
 

More from Chris Love

More from Chris Love (20)

Quick Fetch API Introduction
Quick Fetch API IntroductionQuick Fetch API Introduction
Quick Fetch API Introduction
 
Introduction to Progressive Web Applications
Introduction to Progressive Web ApplicationsIntroduction to Progressive Web Applications
Introduction to Progressive Web Applications
 
Introduction to Progressive Web Applications
Introduction to Progressive Web ApplicationsIntroduction to Progressive Web Applications
Introduction to Progressive Web Applications
 
Progressive Web Apps for Education
Progressive Web Apps for EducationProgressive Web Apps for Education
Progressive Web Apps for Education
 
The server is dead going serverless to create a highly scalable application y...
The server is dead going serverless to create a highly scalable application y...The server is dead going serverless to create a highly scalable application y...
The server is dead going serverless to create a highly scalable application y...
 
A Day Building Fast, Responsive, Extensible Single Page Applications
A Day Building Fast, Responsive, Extensible Single Page ApplicationsA Day Building Fast, Responsive, Extensible Single Page Applications
A Day Building Fast, Responsive, Extensible Single Page Applications
 
Real World Lessons in Progressive Web Application & Service Worker Caching
Real World Lessons in Progressive Web Application & Service Worker CachingReal World Lessons in Progressive Web Application & Service Worker Caching
Real World Lessons in Progressive Web Application & Service Worker Caching
 
Disrupting the application eco system with progressive web applications
Disrupting the application eco system with progressive web applicationsDisrupting the application eco system with progressive web applications
Disrupting the application eco system with progressive web applications
 
Using Responsive Web Design To Make Your Web Work Everywhere - Updated
Using Responsive Web Design To Make Your Web Work Everywhere - UpdatedUsing Responsive Web Design To Make Your Web Work Everywhere - Updated
Using Responsive Web Design To Make Your Web Work Everywhere - Updated
 
Using Responsive Web Design To Make Your Web Work Everywhere
Using Responsive Web Design To Make Your Web Work EverywhereUsing Responsive Web Design To Make Your Web Work Everywhere
Using Responsive Web Design To Make Your Web Work Everywhere
 
Css best practices style guide and tips
Css best practices style guide and tipsCss best practices style guide and tips
Css best practices style guide and tips
 
Using Responsive Web Design To Make Your Web Work Everywhere
Using Responsive Web Design To Make Your Web Work Everywhere Using Responsive Web Design To Make Your Web Work Everywhere
Using Responsive Web Design To Make Your Web Work Everywhere
 
An Introduction to Microsoft Edge
An Introduction to Microsoft EdgeAn Introduction to Microsoft Edge
An Introduction to Microsoft Edge
 
10 things you can do to speed up your web app today stir trek edition
10 things you can do to speed up your web app today   stir trek edition10 things you can do to speed up your web app today   stir trek edition
10 things you can do to speed up your web app today stir trek edition
 
Single page applications the basics
Single page applications the basicsSingle page applications the basics
Single page applications the basics
 
There Is No Mobile: An Introduction To Responsive Web Design
There Is No Mobile: An Introduction To Responsive Web DesignThere Is No Mobile: An Introduction To Responsive Web Design
There Is No Mobile: An Introduction To Responsive Web Design
 
Touch the web
Touch the webTouch the web
Touch the web
 
10 Things You Can Do to Speed Up Your Web App Today
10 Things You Can Do to Speed Up Your Web App Today10 Things You Can Do to Speed Up Your Web App Today
10 Things You Can Do to Speed Up Your Web App Today
 
SPAs Are Easy
SPAs Are EasySPAs Are Easy
SPAs Are Easy
 
A night at the spa
A night at the spaA night at the spa
A night at the spa
 

Recently uploaded

Recently uploaded (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024Top 10 Most Downloaded Games on Play Store in 2024
Top 10 Most Downloaded Games on Play Store in 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 

Lazy load Website Assets

  • 1. Lazy Loading Website Assets A Technique to Improve Your Page Speed https://love2dev.com/blog/lazy-loading/
  • 3. Website speed is a crucial aspect of on page SEO everyone can control. Your goal is to be interactive in under 3 seconds, even on a basic phone over a 3G connection. However, most web sites have so many requests and large payloads this time limit or budget cannot be achieved. In fact, the average web page takes 22 seconds to load, according to Google's research. Why Lazy Load Website Speed
  • 4. • Lazy-Load Content Scenarios • What is IntersectionObever? • Pagination/Continuous Scroll • Using the History API • Loading Scripts As Needed
  • 5. Lazy-Load Content Scenarios For simplicity I will just call it a blog. These sites typically need HTML, CSS, JavaScript, images and custom fonts. They also tend to be sites you want free organic traffic from Google, which means SEO is important. Content Focused Sites Complex web applications can be a little more delicate because they lean on more application logic. This means they require more JavaScript files. When these files are loaded matters. While you can use IntersectionObserver for this, your application needs a good technique to dynamically load scripts as needed. Web Apps
  • 6. Images require bandwidth and may not even be seen. Deferring their request means you can prioritize other content, like HTML and CSS. Just make sure the images are available to Google. The noscript tag is a semantic mechanism to tell the browser to only render the content inside the element if the browser (user agent) does not support JavaScript. Most visitors will have JavaScript turned on, so they won't trigger the image to load initially. But as you will shortly see it will when the non-noscript image is visible. Lazy Load Images Image here Your Business
  • 7. Example Code <img data-srcset="img/example-869x361.jpg 869w, ..." data-src="img/example-869x361.jpg" class="lazy-image" sizes="(max-width: 480px) 90vw, 30vw" alt=“…"> <noscript><img src="img/example-869x361.jpg" alt=“…"></noscript>
  • 8. Lazy Load Images The images are not requested until the data-src and data-srcset attributes are converted to the normal src and srcset attributes. This is done using IntersectionObserver. Image here
  • 9. What is IntersectionObserver? Triggers a callback when target elements come into view. How it Works Supported by all modern browsers and there is a stable polyfil you can dynamically load for those that don't. Browser Support
  • 10. IntersectionObserver This API triggers a callback when target elements come into view. This callback allows you to execute code, which in our scenario is to load assets as needed. Load as It Enters Viewport As observed content approaches the visible viewport the IntersectionObserver API triggers a callback method. Inside this method you can execute code to update and load assets on demand.
  • 11. Example Code var config = { // If the image gets within 50px in the Y axis, start the download. rootMargin: '50px 0px', threshold: 0.1 }; imgObserver = new IntersectionObserver(showImage, config); imgObserver.observe(yourImage);
  • 12. Pagination / Continuous Scroll A common tactic on news related sites in the past couple of years. This is where you scroll down an article and as you reach the bottom a new article is rendered. Solutions: • IntersectionObserver • History API
  • 13. Using the History API The main advantage it provides is the ability to change the URL in the address bar without triggering a server request that causes a complete page reload. It works much like the hash fragment technique, but uses a shareable URL.
  • 14. Web Apps Loading Scripts As Needed JavaScript is the single biggest cause of poor page render times. By offloading scripts you remove them from the rendering process. Improves Page Load Create a script element for each script and appending it to the DOM. Once appended the browser loads the script just as if it were part of the initial markup. Add Scripts on Demand Requires a little finesse, but can make your UX so much better, which should improve customer satisfaction and retention while reducing help desk calls. Complex Web Apps Load minimal script footprint. Gets application going. Boot Up Experience 300 x 50 px