Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Lazy-load sections (#13061)
Damian Legawiecc7ed9408b05e
diff --git a/storefront/app/helpers/spree/page_helper.rb b/storefront/app/helpers/spree/page_helper.rbindex 911fdba2f2..bde2b47ef3 100644--- a/storefront/app/helpers/spree/page_helper.rb+++ b/storefront/app/helpers/spree/page_helper.rb@@ -52,7 +52,7 @@ module Spreepath = section.lazy_path(variables)- turbo_frame_tag(css_id, src: path, loading: 'eager', class: css_class) do+ turbo_frame_tag(css_id, src: path, loading: :lazy, class: css_class, data: { controller: 'prefetch-lazy' }) dorender('/' + section.to_partial_path, **variables)endelsediff --git a/storefront/app/javascript/spree/storefront/application.js b/storefront/app/javascript/spree/storefront/application.jsindex 4d75b0e5cd..04a0b3c5b0 100644--- a/storefront/app/javascript/spree/storefront/application.js+++ b/storefront/app/javascript/spree/storefront/application.js@@ -47,6 +47,7 @@ const controllers = ['no-ui-slider','pdp-desktop-gallery','plp-variant-picker',+ 'prefetch-lazy','product-form','quantity-picker','read-more',diff --git a/storefront/app/javascript/spree/storefront/controllers/prefetch_lazy_controller.js b/storefront/app/javascript/spree/storefront/controllers/prefetch_lazy_controller.jsnew file mode 100644index 0000000000..02b1dc8510--- /dev/null+++ b/storefront/app/javascript/spree/storefront/controllers/prefetch_lazy_controller.js@@ -0,0 +1,26 @@+import { Controller } from "@hotwired/stimulus"++export default class extends Controller {+ static values = {+ rootMargin: { type: Number, default: 200 }+ }++ connect() {+ if (this.element.getAttribute("loading") == "lazy") {+ this.observer = new IntersectionObserver(this.intersect, { rootMargin: `0px 0px ${this.rootMarginValue}px 0px` })+ this.observer.observe(this.element)+ }+ }++ disconnect() {+ this.observer?.disconnect()+ }++ intersect = (entries) => {+ const lastEntry = entries.slice(-1)[0]+ if (lastEntry?.isIntersecting) {+ this.observer.unobserve(this.element) // We only need to do this once+ this.element.setAttribute("loading", "eager")+ }+ }+}\ No newline at end of file