From fbf58f347f6481bb3be35c3776eb2ad35183633a Mon Sep 17 00:00:00 2001 From: Ben Dichter Date: Fri, 9 Oct 2026 12:59:11 -0400 Subject: [PATCH] Load Swiper only on the home page The Swiper stylesheet and script (48 KB compressed) were loaded on every page, and only the home page has carousels. They are now included only there, and main.js checks that the library is present before creating the carousels. Co-Authored-By: Claude Opus 5.5 (1M context) --- assets/js/main.js | 110 +++++++++--------- layouts/partials/essentials/head.html | 4 +- .../partials/essentials/script-footer.html | 4 +- 3 files changed, 62 insertions(+), 56 deletions(-) diff --git a/assets/js/main.js b/assets/js/main.js index 84e07cb..3c6f36d 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -46,65 +46,67 @@ document.addEventListener("DOMContentLoaded", function () { - // Swiper Init - const heroSwiper = new Swiper(".heroSwiper", { - slidesPerView: 1, - loop: true, - pagination: { - el: ".swiper-pagination", - dynamicBullets: true, - }, - }); + // Swiper Init (the library is only loaded on pages that have a carousel) + if (typeof Swiper !== "undefined") { + const heroSwiper = new Swiper(".heroSwiper", { + slidesPerView: 1, + loop: true, + pagination: { + el: ".swiper-pagination", + dynamicBullets: true, + }, + }); - const newsSwiper = new Swiper(".newsSwiper", { - slidesPerView: 1, - spaceBetween: 10, - pagination: { - el: ".swiper-pagination", - dynamicBullets: true, - }, - navigation: { - nextEl: ".swiper-button-next", - prevEl: ".swiper-button-prev", - }, - breakpoints: { - 576: { - slidesPerView: 2, - spaceBetween: 20, + const newsSwiper = new Swiper(".newsSwiper", { + slidesPerView: 1, + spaceBetween: 10, + pagination: { + el: ".swiper-pagination", + dynamicBullets: true, }, - 768: { - slidesPerView: 3, - spaceBetween: 20, - } - }, - }); + navigation: { + nextEl: ".swiper-button-next", + prevEl: ".swiper-button-prev", + }, + breakpoints: { + 576: { + slidesPerView: 2, + spaceBetween: 20, + }, + 768: { + slidesPerView: 3, + spaceBetween: 20, + } + }, + }); - const eventsSwiper = new Swiper(".eventsSwiper", { - slidesPerView: 1, - spaceBetween: 10, - pagination: { - el: ".swiper-pagination", - dynamicBullets: true, - }, - navigation: { - nextEl: ".swiper-button-next", - prevEl: ".swiper-button-prev", - }, - breakpoints: { - 576: { - slidesPerView: 2, - spaceBetween: 30, + const eventsSwiper = new Swiper(".eventsSwiper", { + slidesPerView: 1, + spaceBetween: 10, + pagination: { + el: ".swiper-pagination", + dynamicBullets: true, }, - 768: { - slidesPerView: 3, - spaceBetween: 30, + navigation: { + nextEl: ".swiper-button-next", + prevEl: ".swiper-button-prev", }, - 1200: { - slidesPerView: 3, - spaceBetween: 40 - } - }, - }); + breakpoints: { + 576: { + slidesPerView: 2, + spaceBetween: 30, + }, + 768: { + slidesPerView: 3, + spaceBetween: 30, + }, + 1200: { + slidesPerView: 3, + spaceBetween: 40 + } + }, + }); + } // PageFind Init window.addEventListener('DOMContentLoaded', (event) => { diff --git a/layouts/partials/essentials/head.html b/layouts/partials/essentials/head.html index a3f6b1b..8d3e50c 100644 --- a/layouts/partials/essentials/head.html +++ b/layouts/partials/essentials/head.html @@ -15,11 +15,13 @@ {{/* Netlify Identity Widget */}} -{{/* Swiper JS */}} +{{/* Swiper JS (only the home page has carousels) */}} +{{ if .IsHome }} +{{ end }} {{/* Favicon */}} diff --git a/layouts/partials/essentials/script-footer.html b/layouts/partials/essentials/script-footer.html index 2b295a4..cf23655 100644 --- a/layouts/partials/essentials/script-footer.html +++ b/layouts/partials/essentials/script-footer.html @@ -1,5 +1,7 @@ -{{/* Swiper JS */}} +{{/* Swiper JS (only the home page has carousels) */}} +{{ if .IsHome }} +{{ end }} {{/* Scripts */}} {{ with resources.Get "js/plugins.js" }}