Skip to content

Commit 303507e

Browse files
feat: add responsive images with srcset support for optimized loading
- Add widths and sizes props to Image component - Update PostCard, Profile, Banner, AnimeCard, and post pages with responsive images - Generated images stored in dist/ which is gitignored
1 parent 1d9f42a commit 303507e

7 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/components/atoms/Image/Image.astro

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface Props {
1010
basePath?: string;
1111
loading?: "eager" | "lazy";
1212
fetchpriority?: "high" | "low" | "auto";
13+
widths?: number[];
14+
sizes?: string;
1315
}
1416
1517
import { Image as AstroImage } from "astro:assets";
@@ -24,6 +26,8 @@ const {
2426
basePath = "/",
2527
loading = "lazy",
2628
fetchpriority,
29+
widths,
30+
sizes,
2731
} = Astro.props;
2832
const className = Astro.props.class;
2933
@@ -79,6 +83,8 @@ const referrerPolicy = !isLocal && isBilibiliImage ? "no-referrer" : undefined;
7983
style={imageStyle}
8084
loading={loading}
8185
fetchpriority={fetchpriority}
86+
widths={widths}
87+
sizes={sizes}
8288
/>
8389
)
8490
}

src/components/features/anime/AnimeCard.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ const progressPercent =
4545
src={anime.cover}
4646
alt={anime.title}
4747
class="w-full h-full object-cover transition-transform duration-200 group-hover:scale-110"
48+
widths={[200, 400, 600]}
49+
sizes="(max-width: 640px) 50vw, (max-width: 1024px) 25vw, 20vw"
4850
/>
4951
<div
5052
class="absolute inset-0 bg-gradient-to-t from-black/60 via-transparent to-transparent opacity-0 group-hover:opacity-100 transition-opacity duration-300"

src/components/features/posts/PostCard.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,8 @@ const { remarkPluginFrontmatter } = await render(entry);
157157
basePath={getFileDirFromPath(entry.filePath || "")}
158158
alt="Cover Image of the Post"
159159
class="w-full h-full"
160+
widths={[200, 400, 800]}
161+
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
160162
/>
161163
</a>
162164
)

src/components/layout/Banner.astro

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ const hasBannerLink = !!siteConfig.banner.credit.url;
9898
position={siteConfig.banner.position}
9999
loading={index === 0 ? "eager" : "lazy"}
100100
fetchpriority={index === 0 ? "high" : undefined}
101+
widths={[640, 1024, 1536]}
102+
sizes="100vw"
101103
/>
102104
)}
103105
<!-- 桌面端/平板端:使用桌面端专用图片 -->
@@ -117,6 +119,8 @@ const hasBannerLink = !!siteConfig.banner.credit.url;
117119
position={siteConfig.banner.position}
118120
loading={index === 0 ? "eager" : "lazy"}
119121
fetchpriority={index === 0 ? "high" : undefined}
122+
widths={[1024, 1536, 2048, 2560]}
123+
sizes="100vw"
120124
/>
121125
)}
122126
</li>

src/components/widgets/profile/Profile.astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import { i18n } from "../../../i18n/translation";
2828
src={profileConfig.avatar || ""}
2929
alt="Profile Image of the Author"
3030
class="mx-auto lg:w-full h-full lg:mt-0"
31+
widths={[96, 192, 384]}
32+
sizes="(max-width: 1024px) 192px, 128px"
3133
/>
3234
</a>
3335
<div class="px-2">

src/pages/[permalink].astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ const jsonLd = {
218218
src={entry.data.image}
219219
basePath={getFileDirFromPath(entry.filePath || "")}
220220
class="mb-8 rounded-xl banner-container onload-animation"
221+
widths={[640, 1024, 1536, 2048]}
222+
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 80vw, 1200px"
221223
/>
222224
</>
223225
)

src/pages/posts/[...slug].astro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ const jsonLd = {
280280
src={entry.data.image}
281281
basePath={getFileDirFromPath(entry.filePath || "")}
282282
class="mb-8 rounded-xl banner-container onload-animation"
283+
widths={[640, 1024, 1536, 2048]}
284+
sizes="(max-width: 768px) 100vw, (max-width: 1200px) 80vw, 1200px"
283285
/>
284286
</>
285287
)

0 commit comments

Comments
 (0)