From 08e67ec2e34b36e355129adb2be6d14903a0b0e3 Mon Sep 17 00:00:00 2001 From: "Calvin A. Allen" Date: Thu, 29 Jan 2026 10:17:53 -0500 Subject: [PATCH] fix(blog): use data attributes for lightbox year/slug instead of URL parsing The URL doesn't contain the year, so the lightbox couldn't find the original images. Now we pass year and slug from the server-side props via data attributes on the article element. --- src/layouts/PostLayout.astro | 44 ++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/layouts/PostLayout.astro b/src/layouts/PostLayout.astro index d4ead42..96b7cd6 100644 --- a/src/layouts/PostLayout.astro +++ b/src/layouts/PostLayout.astro @@ -31,10 +31,13 @@ const postUrl = `https://www.codingwithcalvin.net/${slug}/`; // Get the image URL for OG tags - use the src property from ImageMetadata const ogImageUrl = image?.src; + +// Extract year for lightbox original image paths +const year = date.getFullYear(); --- -
+
@@ -123,9 +126,10 @@ const ogImageUrl = image?.src; if (e.key === 'Escape') lightbox.classList.remove('active'); }); - // Get the current post slug from the URL - const pathParts = window.location.pathname.split('/').filter(Boolean); - const slug = pathParts[pathParts.length - 1] || pathParts[pathParts.length - 2]; + // Get year and slug from data attributes + const article = document.querySelector('article[data-year][data-slug]'); + const year = article?.dataset.year; + const slug = article?.dataset.slug; // Make prose images clickable to open lightbox with original document.querySelectorAll('.prose img').forEach((img) => { @@ -141,27 +145,23 @@ const ogImageUrl = image?.src; // The optimized path is like /_astro/filename.hash.webp // We need to map it back to /originals/YEAR/SLUG/filename.png const match = src.match(/\/_astro\/([^.]+)\./); - if (match) { + if (match && year && slug) { const baseName = match[1]; - // Try common extensions - const year = window.location.pathname.match(/\/(\d{4})\//)?.[1]; - if (year && slug) { - // Try to load the original - const originalPath = `/originals/${year}/${slug}/${baseName}.png`; - lightboxImg.src = originalPath; - lightboxImg.alt = alt; - lightbox.classList.add('active'); - - // Fallback to optimized if original fails + // Try to load the original + const originalPath = `/originals/${year}/${slug}/${baseName}.png`; + lightboxImg.src = originalPath; + lightboxImg.alt = alt; + lightbox.classList.add('active'); + + // Fallback to optimized if original fails + lightboxImg.onerror = () => { + // Try jpg + lightboxImg.src = `/originals/${year}/${slug}/${baseName}.jpg`; lightboxImg.onerror = () => { - // Try jpg - lightboxImg.src = `/originals/${year}/${slug}/${baseName}.jpg`; - lightboxImg.onerror = () => { - // Fall back to optimized version - lightboxImg.src = src; - }; + // Fall back to optimized version + lightboxImg.src = src; }; - } + }; } else { // Use the src as-is if we can't parse it lightboxImg.src = src;