{"id":7287,"date":"2025-07-21T08:53:06","date_gmt":"2025-07-21T08:53:06","guid":{"rendered":"https:\/\/impulsebyte.com\/blogs\/?p=7287"},"modified":"2025-07-21T09:33:08","modified_gmt":"2025-07-21T09:33:08","slug":"improve-page-speed-using-intersectionobserver-api-in-codeigniter-4","status":"publish","type":"post","link":"https:\/\/impulsebyte.com\/blogs\/pageload-speed\/improve-page-speed-using-intersectionobserver-api-in-codeigniter-4\/","title":{"rendered":"Improving Page Speed and Core Web Vitals with IntersectionObserver in CodeIgniter 4"},"content":{"rendered":"\n<p><a href=\"https:\/\/impulsebyte.com\/blogs\/shopify\/asynchronous-loading-of-third-party-scripts-with-barrel-asyncload\/\" data-type=\"post\" data-id=\"7240\">Page Speed<\/a> depends reducing your page load time from 9.6s to under 2.0s and passing Core Web Vitals requires both backend optimizations and efficient frontend strategies. The <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Intersection_Observer_API\" data-type=\"link\" data-id=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/API\/Intersection_Observer_API\" target=\"_blank\" rel=\"noreferrer noopener\">IntersectionObserver API<\/a> is a smart way to defer loading of offscreen elements (like images, iframes, or heavy scripts), improving metrics like Largest Contentful Paint (LCP), First Input Delay (FID), and Cumulative Layout Shift (CLS).<\/p>\n\n\n\n<figure class=\"wp-block-image size-large is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"427\" src=\"https:\/\/impulsebyte.com\/blogs\/wp-content\/uploads\/2025\/07\/pexels-andy-wilson-2150315531-33059163-1-1024x427.webp\" alt=\"Page Speed using intersectionObserver in CodeIgniter 4\" class=\"wp-image-7301\" title=\"Page Speed using intersectionObserver in CodeIgniter 4\" srcset=\"https:\/\/impulsebyte.com\/blogs\/wp-content\/uploads\/2025\/07\/pexels-andy-wilson-2150315531-33059163-1-1024x427.webp 1024w, https:\/\/impulsebyte.com\/blogs\/wp-content\/uploads\/2025\/07\/pexels-andy-wilson-2150315531-33059163-1-300x125.webp 300w, https:\/\/impulsebyte.com\/blogs\/wp-content\/uploads\/2025\/07\/pexels-andy-wilson-2150315531-33059163-1-768x320.webp 768w, https:\/\/impulsebyte.com\/blogs\/wp-content\/uploads\/2025\/07\/pexels-andy-wilson-2150315531-33059163-1.webp 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption class=\"wp-element-caption\">Source: <a href=\"https:\/\/www.pexels.com\/photo\/high-speed-night-train-in-motion-33059163\/\" data-type=\"link\" data-id=\"https:\/\/www.pexels.com\/photo\/high-speed-night-train-in-motion-33059163\/\" target=\"_blank\" rel=\"noreferrer noopener\">https:\/\/www.pexels.com\/photo\/high-speed-night-train-in-motion-33059163\/<\/a><\/figcaption><\/figure>\n\n\n\n<p>Below is a streamlined guide to implement lazy loading using IntersectionObserver in your CodeIgniter 4 project.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"why-use-intersectionobserver\">Why Use IntersectionObserver API to increase Page Speed?<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Efficient Lazy Loading:<\/strong> Loads images or resources only when they are close to entering the viewport.<\/li>\n\n\n\n<li><strong>Better User Experience:<\/strong> Faster initial render, since not all elements are fetched at once.<\/li>\n\n\n\n<li><strong>Improved Core Web Vitals:<\/strong> Reduces LCP and mitigates layout shifts by controlling loading behavior.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-process-in-codeigniter-4\">Implementation Process in CodeIgniter 4<\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">1. Render &#8220;Lazy&#8221; Images in Your Views<\/h2>\n\n\n\n<p>Update your image tags to use a placeholder <code>src<\/code> and move the real image source to a <code>data-src<\/code> attribute.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;!-- In your CodeIgniter 4 view file -->\n&lt;img class=\"lazy\" src=\"\/images\/placeholder.jpg\" data-src=\"\/images\/real-image.jpg\" alt=\"Descriptive Alt\"><\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #6A737D\">&lt;!-- In your CodeIgniter 4 view file --&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">&lt;<\/span><span style=\"color: #22863A\">img<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">class<\/span><span style=\"color: #24292E\">=<\/span><span style=\"color: #032F62\">&quot;lazy&quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">src<\/span><span style=\"color: #24292E\">=<\/span><span style=\"color: #032F62\">&quot;\/images\/placeholder.jpg&quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">data-src<\/span><span style=\"color: #24292E\">=<\/span><span style=\"color: #032F62\">&quot;\/images\/real-image.jpg&quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">alt<\/span><span style=\"color: #24292E\">=<\/span><span style=\"color: #032F62\">&quot;Descriptive Alt&quot;<\/span><span style=\"color: #24292E\">&gt;<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>\/images\/placeholder.jpg<\/code><\/strong>: Small, low-quality placeholder or a blank image.<\/li>\n\n\n\n<li><strong><code>data-src<\/code><\/strong>: The actual image you want to load lazily.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">2. Add the IntersectionObserver Script<\/h2>\n\n\n\n<p>Add the script below just before your closing <code>&lt;\/body&gt;<\/code> tag for deferred execution:<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;script>\ndocument.addEventListener(\"DOMContentLoaded\", function() {\n  const lazyImages = document.querySelectorAll('img.lazy');\n  if ('IntersectionObserver' in window) {\n    let imgObserver = new IntersectionObserver(function(entries, observer) {\n      entries.forEach(function(entry) {\n        if (entry.isIntersecting) {\n          const img = entry.target;\n          img.src = img.dataset.src;\n          img.classList.remove('lazy');\n          imgObserver.unobserve(img);\n        }\n      });\n    }, {\n      rootMargin: '200px 0px', \/\/ preload before they enter the viewport\n      threshold: 0.01\n    });\n    lazyImages.forEach(function(img) {\n      imgObserver.observe(img);\n    });\n  } else {\n    \/\/ Fallback for unsupported browsers\n    lazyImages.forEach(function(img) {\n      img.src = img.dataset.src;\n      img.classList.remove('lazy');\n    });\n  }\n});\n&lt;\/script>\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #24292E\">&lt;<\/span><span style=\"color: #22863A\">script<\/span><span style=\"color: #24292E\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">document.addEventListener(&quot;DOMContentLoaded&quot;, function() {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  const lazyImages <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> document.<\/span><span style=\"color: #6F42C1\">querySelectorAll<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&#39;img.lazy&#39;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  <\/span><span style=\"color: #6F42C1\">if<\/span><span style=\"color: #24292E\"> (<\/span><span style=\"color: #032F62\">&#39;IntersectionObserver&#39;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">in<\/span><span style=\"color: #24292E\"> window) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    let imgObserver <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">new<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">IntersectionObserver<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #D73A49\">function<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #E36209\">entries<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #E36209\">observer<\/span><span style=\"color: #24292E\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      entries.<\/span><span style=\"color: #6F42C1\">forEach<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #D73A49\">function<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #E36209\">entry<\/span><span style=\"color: #24292E\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        <\/span><span style=\"color: #D73A49\">if<\/span><span style=\"color: #24292E\"> (entry.isIntersecting) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">          <\/span><span style=\"color: #D73A49\">const<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">img<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> entry.target;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">          img.src <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> img.dataset.src;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">          img.classList.<\/span><span style=\"color: #6F42C1\">remove<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&#39;lazy&#39;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">          imgObserver.<\/span><span style=\"color: #6F42C1\">unobserve<\/span><span style=\"color: #24292E\">(img);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    }, {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      rootMargin: <\/span><span style=\"color: #032F62\">&#39;200px 0px&#39;<\/span><span style=\"color: #24292E\">, <\/span><span style=\"color: #6A737D\">\/\/ preload before they enter the viewport<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      threshold: <\/span><span style=\"color: #005CC5\">0.01<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    lazyImages.forEach(<\/span><span style=\"color: #6F42C1\">function<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #E36209\">img<\/span><span style=\"color: #24292E\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      imgObserver.<\/span><span style=\"color: #6F42C1\">observe<\/span><span style=\"color: #24292E\">(img);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  } else {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #6A737D\">\/\/ Fallback for unsupported browsers<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    lazyImages.forEach(<\/span><span style=\"color: #6F42C1\">function<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #E36209\">img<\/span><span style=\"color: #24292E\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      img.src <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> img.dataset.src;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">      img.classList.<\/span><span style=\"color: #6F42C1\">remove<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&#39;lazy&#39;<\/span><span style=\"color: #24292E\">);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    });<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">});<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">&lt;\/<\/span><span style=\"color: #22863A\">script<\/span><span style=\"color: #24292E\">&gt;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>Key Points:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Switches real image into <code>src<\/code> as elements approach the viewport.<\/li>\n\n\n\n<li>The <code>rootMargin<\/code> of <code>200px<\/code> preloads images slightly before scrolling into view for better visual experience.<\/li>\n\n\n\n<li>Removes <code>.lazy<\/code> class to avoid repeated processing.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">3. Additional Optimizations to make better Page Speed<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Compress Images<\/strong>: Use modern formats (WebP), reduce file sizes.<\/li>\n\n\n\n<li><strong>Minify CSS\/JS:<\/strong> Avoid render-blocking resources.<\/li>\n\n\n\n<li><strong>Defer Non-Essential Scripts:<\/strong> Move heavy scripts to load after critical content.<\/li>\n\n\n\n<li><strong>Use Efficient Fonts:<\/strong> Limit to minimum font weights\/styles.<\/li>\n\n\n\n<li><strong>Server-side Enhancements:<\/strong> Enable GZIP compression, set far-future cache headers, and use a CDN for static assets.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"summary-table-steps-and-impact\">Summary Table: Steps and Impact<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>Step<\/td><td>Implementation<\/td><td>Core Web Vital Improved<\/td><\/tr><tr><td>Lazy load images\/Els<\/td><td>IntersectionObserver<\/td><td>LCP, FID, CLS<\/td><\/tr><tr><td>Compress &amp; Optimize Images<\/td><td>TinyPNG, WebP, etc.<\/td><td>LCP<\/td><\/tr><tr><td>Defer\/Async JS<\/td><td><code>&lt;script defer&gt;<\/code><\/td><td>FID, LCP<\/td><\/tr><tr><td>Minimize CSS\/JS<\/td><td>Minifiers, splitting<\/td><td>FID, LCP<\/td><\/tr><tr><td>CDN\/Cache Static Content<\/td><td>CDN, server headers<\/td><td>All<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example-in-codeigniter-4\">Example in CodeIgniter 4<\/h2>\n\n\n\n<p>Suppose you have a controller and a view for a portfolio\/gallery:<\/p>\n\n\n\n<p><strong>Controller (<code>app\/Controllers\/Gallery.php<\/code>):<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(2 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>public function index()\n{\n    $data&#91;'images'&#93; = &#91;\n        '\/images\/1.jpg',\n        '\/images\/2.jpg',\n        '\/images\/3.jpg',\n    &#93;;\n    return view('gallery', $data);\n}\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D73A49\">public<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">function<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">index<\/span><span style=\"color: #24292E\">()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">{<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    $data&#91;<\/span><span style=\"color: #032F62\">&#39;images&#39;<\/span><span style=\"color: #24292E\">&#93; <\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #24292E\"> &#91;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        <\/span><span style=\"color: #032F62\">&#39;\/images\/1.jpg&#39;<\/span><span style=\"color: #24292E\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        <\/span><span style=\"color: #032F62\">&#39;\/images\/2.jpg&#39;<\/span><span style=\"color: #24292E\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">        <\/span><span style=\"color: #032F62\">&#39;\/images\/3.jpg&#39;<\/span><span style=\"color: #24292E\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    &#93;;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">    <\/span><span style=\"color: #D73A49\">return<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">view<\/span><span style=\"color: #24292E\">(<\/span><span style=\"color: #032F62\">&#39;gallery&#39;<\/span><span style=\"color: #24292E\">, $data);<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">}<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p><strong>View (<code>app\/Views\/gallery.php<\/code>):<\/strong><\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:.875rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#24292e;--cbp-line-number-width:calc(1 * 0.6 * .875rem);line-height:1.25rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#fff\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewBox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#FF5F56\" stroke=\"#E0443E\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#FFBD2E\" stroke=\"#DEA123\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#27C93F\" stroke=\"#1AAB29\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" style=\"color:#24292e;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><pre class=\"code-block-pro-copy-button-pre\" aria-hidden=\"true\"><textarea class=\"code-block-pro-copy-button-textarea\" tabindex=\"-1\" aria-hidden=\"true\" readonly>&lt;?php foreach ($images as $image): ?>\n  &lt;img class=\"lazy\" src=\"\/images\/placeholder.jpg\" data-src=\"&lt;?= esc($image) ?>\" alt=\"Gallery Image\">\n&lt;?php endforeach; ?>\n&lt;!-- Place IntersectionObserver JS here (see script above) -->\n<\/textarea><\/pre><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki github-light\" style=\"background-color: #fff\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #D73A49\">&lt;?<\/span><span style=\"color: #005CC5\">php<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">foreach<\/span><span style=\"color: #24292E\"> ($images <\/span><span style=\"color: #D73A49\">as<\/span><span style=\"color: #24292E\"> $image)<\/span><span style=\"color: #D73A49\">:<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">?&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #24292E\">  <\/span><span style=\"color: #D73A49\">&lt;<\/span><span style=\"color: #005CC5\">img<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">class=<\/span><span style=\"color: #032F62\">&quot;lazy&quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">src<\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #032F62\">&quot;\/images\/placeholder.jpg&quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">data<\/span><span style=\"color: #D73A49\">-<\/span><span style=\"color: #005CC5\">src<\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #032F62\">&quot;&lt;?= esc(<\/span><span style=\"color: #24292E\">$image<\/span><span style=\"color: #032F62\">) ?&gt;&quot;<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">alt<\/span><span style=\"color: #D73A49\">=<\/span><span style=\"color: #032F62\">&quot;Gallery Image&quot;<\/span><span style=\"color: #D73A49\">&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D73A49\">&lt;?<\/span><span style=\"color: #005CC5\">php<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #D73A49\">endforeach<\/span><span style=\"color: #24292E\">; <\/span><span style=\"color: #D73A49\">?&gt;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D73A49\">&lt;!--<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">Place<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">IntersectionObserver<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">JS<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #6F42C1\">here<\/span><span style=\"color: #24292E\"> (<\/span><span style=\"color: #005CC5\">see<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">script<\/span><span style=\"color: #24292E\"> <\/span><span style=\"color: #005CC5\">above<\/span><span style=\"color: #24292E\">) <\/span><span style=\"color: #D73A49\">--&gt;<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"final-tips\">Final Tips<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Regularly run speed tests (Google PageSpeed Insights\/Lighthouse).<\/li>\n\n\n\n<li>Check Core Web Vitals in Google Search Console.<\/li>\n\n\n\n<li>Continuously monitor and optimize as content and plugins evolve.<\/li>\n<\/ul>\n\n\n\n<p>By effectively using IntersectionObserver for lazy-loading, along with complementary frontend and backend optimizations, you can significantly boost your site performance and meet Core Web Vitals standards.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Page Speed depends reducing your page load time from 9.6s to under 2.0s and passing Core Web Vitals requires both backend optimizations and efficient frontend strategies. The IntersectionObserver API is &hellip; <a title=\"Improving Page Speed and Core Web Vitals with IntersectionObserver in CodeIgniter 4\" class=\"hm-read-more\" href=\"https:\/\/impulsebyte.com\/blogs\/pageload-speed\/improve-page-speed-using-intersectionobserver-api-in-codeigniter-4\/\"><span class=\"screen-reader-text\">Improving Page Speed and Core Web Vitals with IntersectionObserver in CodeIgniter 4<\/span>Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[114,140],"tags":[],"class_list":["post-7287","post","type-post","status-publish","format-standard","hentry","category-pageload-speed","category-codeigniter"],"_links":{"self":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/comments?post=7287"}],"version-history":[{"count":10,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7287\/revisions"}],"predecessor-version":[{"id":7305,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/posts\/7287\/revisions\/7305"}],"wp:attachment":[{"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/media?parent=7287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/categories?post=7287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/impulsebyte.com\/blogs\/wp-json\/wp\/v2\/tags?post=7287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}