From 37d7a190d855b6a7f8585fb53bec7899e0fe2ea3 Mon Sep 17 00:00:00 2001 From: somrat sorkar Date: Sun, 24 Dec 2023 13:14:41 +0600 Subject: [PATCH] added ImageMod component for image optimization --- package.json | 12 ++--- src/layouts/PostSingle.astro | 4 +- src/layouts/components/AuthorCard.astro | 4 +- src/layouts/components/BlogCard.astro | 4 +- src/layouts/components/ImageMod.astro | 60 +++++++++++++++++++++++++ src/layouts/components/Logo.astro | 6 +-- src/layouts/partials/CallToAction.astro | 4 +- src/layouts/partials/Testimonial.astro | 5 +-- src/lib/utils/bgImageMod.ts | 41 +++++++++++++++++ src/pages/about.astro | 4 +- src/pages/authors/[single].astro | 4 +- src/pages/index.astro | 6 +-- 12 files changed, 127 insertions(+), 27 deletions(-) create mode 100644 src/layouts/components/ImageMod.astro create mode 100644 src/lib/utils/bgImageMod.ts diff --git a/package.json b/package.json index 14c0dcc..140a0a1 100755 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "astroplate", - "version": "3.0.1", + "version": "3.1.0", "description": "Astro and Tailwindcss boilerplate", "author": "zeon.studio", "license": "MIT", @@ -14,12 +14,12 @@ "remove-darkmode": "node scripts/removeDarkmode.js && yarn format" }, "dependencies": { - "@astrojs/mdx": "^2.0.1", - "@astrojs/react": "^3.0.7", + "@astrojs/mdx": "^2.0.2", + "@astrojs/react": "^3.0.8", "@astrojs/rss": "^4.0.1", "@astrojs/sitemap": "^3.0.3", - "@astrojs/tailwind": "^5.0.3", - "astro": "^4.0.6", + "@astrojs/tailwind": "^5.0.4", + "astro": "^4.0.7", "astro-auto-import": "^0.4.2", "date-fns": "^2.30.0", "date-fns-tz": "^2.0.0", @@ -53,7 +53,7 @@ "prettier-plugin-astro": "^0.12.2", "prettier-plugin-tailwindcss": "^0.5.9", "sass": "^1.69.5", - "tailwindcss": "^3.3.7", + "tailwindcss": "^3.4.0", "tailwind-bootstrap-grid": "^5.1.0", "typescript": "5.3.3" } diff --git a/src/layouts/PostSingle.astro b/src/layouts/PostSingle.astro index 79b4cb4..939c053 100755 --- a/src/layouts/PostSingle.astro +++ b/src/layouts/PostSingle.astro @@ -6,12 +6,12 @@ import { getSinglePage } from "@/lib/contentParser.astro"; import dateFormat from "@/lib/utils/dateFormat"; import similarItems from "@/lib/utils/similarItems"; import { humanize, markdownify, slugify } from "@/lib/utils/textConverter"; -import { Image } from "astro:assets"; import { FaRegClock, FaRegFolder, FaRegUserCircle, } from "react-icons/fa/index.js"; +import ImageMod from "./components/ImageMod.astro"; const COLLECTION_FOLDER = "blog"; const { post } = Astro.props; @@ -29,7 +29,7 @@ const { title, description, author, categories, image, date, tags } = post.data; { image && (
- { image && ( - {title} { image && ( - {title}} + alt={alt} + width={width} + height={height} + loading={loading} + decoding={decoding} + class={className} + format={format} + style={style} + /> + ) +} diff --git a/src/layouts/components/Logo.astro b/src/layouts/components/Logo.astro index 1e6c94a..8b7ba5c 100644 --- a/src/layouts/components/Logo.astro +++ b/src/layouts/components/Logo.astro @@ -1,6 +1,6 @@ --- import config from "@/config/config.json"; -import { Image } from "astro:assets"; +import ImageMod from "./ImageMod.astro"; const { src, srcDarkmode }: { src?: string; srcDarkmode?: string } = Astro.props; @@ -27,7 +27,7 @@ const { theme_switcher }: { theme_switcher: boolean } = config.settings; { src || srcDarkmode || logo || logo_darkmode ? ( <> - {theme_switcher && ( -
-
- { + src = `/public${src}`; + const images = import.meta.glob("/public/images/**/*.{jpeg,jpg,png,gif}"); + + // Check if the source path is valid + if (!src || !images[src]) { + console.error( + `\x1b[31mImage not found - ${src}.\x1b[0m Make sure the image is in the /public/images folder.`, + ); + + return ""; // Return an empty string if the image is not found + } + + // Function to get the image info like width, height, format, etc. + const getImagePath = async (image: string) => { + try { + const imageData = (await images[image]()) as any; + return imageData; + } catch (error) { + return `Image not found - ${src}. Make sure the image is in the /public/images folder.`; + } + }; + + // Get the image data for the specified source path + const image = await getImagePath(src); + + // Optimize the image for development + const ImageMod = await getImage({ + src: image.default, + format: format, + }); + + return ImageMod.src; +}; + +export default bgImageMod; diff --git a/src/pages/about.astro b/src/pages/about.astro index ea26d87..064384d 100644 --- a/src/pages/about.astro +++ b/src/pages/about.astro @@ -1,7 +1,7 @@ --- +import ImageMod from "@/components/ImageMod.astro"; import Base from "@/layouts/Base.astro"; import { markdownify } from "@/lib/utils/textConverter"; -import { Image } from "astro:assets"; import { getEntry } from "astro:content"; const about = await getEntry("about", "-index"); @@ -21,7 +21,7 @@ const { title, description, meta_title, image } = about.data;
{ image && ( - { image && ( - - -