Skip to content

Commit ed8d9e2

Browse files
committed
feat(seo): enhance json-ld schema with image dimensions
- Enhanced the JSON-LD schema for blog posts to include 'width' and 'height' properties for the 'heroImage'. This provides more detailed image metadata to search engines, improving the potential for rich snippet representation. - Added the 'image-size' package to 'devDependencies' to dynamically read image dimensions during the build process.
1 parent 58990e0 commit ed8d9e2

4 files changed

Lines changed: 54 additions & 33 deletions

File tree

CHANGELOG

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,17 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8-
## [1.0.11] - 2026-01-11
8+
## [1.0.12] - 2026-01-11
9+
10+
### Added
11+
12+
- **feat(seo):** Enhanced the JSON-LD schema for blog posts to include `width` and `height` properties for the `heroImage`. This provides more detailed image metadata to search engines, improving the potential for rich snippet representation.
913

14+
### Changed
15+
16+
- **build(deps):** Added the `image-size` package to `devDependencies` to dynamically read image dimensions during the build process.
17+
18+
## [1.0.11] - 2026-01-11
1019
### Fixed
1120

1221
- **fix(sitemap):** Resolved an issue where non-ASCII characters in blog tags were incorrectly URL-encoded in the sitemap (e.g., `%E3%82%A2...`). The `slugifyTag` utility now correctly transliterates and sanitizes tags to create clean, readable URLs.

package-lock.json

Lines changed: 14 additions & 26 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"csso": "^5.0.5",
3838
"glob": "^11.0.3",
3939
"html-minifier-terser": "^7.2.0",
40+
"image-size": "^2.0.2",
4041
"imagemin": "^9.0.1",
4142
"imagemin-avif": "^0.1.6",
4243
"imagemin-mozjpeg": "^10.0.0",

src/components/common/Schema.astro

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
---
2+
import { imageSize } from 'image-size';
3+
import path from 'path';
4+
import fs from 'fs';
5+
26
export interface Props {
37
title: string;
48
description: string;
@@ -27,23 +31,42 @@ const siteUrl = Astro.site ? Astro.site.href : 'https://astro-batavia.pages.dev'
2731
const canonicalUrl = new URL(Astro.url.pathname, siteUrl).href;
2832
const imageUrl = image ? new URL(image, siteUrl).href : new URL('/og-default.jpg', siteUrl).href;
2933
34+
let imageDimensions = null;
35+
if (image) {
36+
const imagePath = path.join(process.cwd(), 'public', image);
37+
if (fs.existsSync(imagePath)) {
38+
try {
39+
imageDimensions = imageSize(imagePath);
40+
} catch (e) {
41+
console.error(`Error getting image dimensions for ${imagePath}:`, e);
42+
}
43+
}
44+
}
45+
3046
let schema: Record<string, any> = {
3147
"@context": "https://schema.org",
3248
};
3349
3450
if (type === 'article' || type === 'blog') {
51+
let imageSchema: Record<string, any> = {
52+
"@type": "ImageObject",
53+
"url": imageUrl,
54+
"caption": imageAlt || title,
55+
"representativeOfPage": "true"
56+
};
57+
58+
if (imageDimensions) {
59+
imageSchema.width = imageDimensions.width;
60+
imageSchema.height = imageDimensions.height;
61+
}
62+
3563
schema = {
3664
...schema,
3765
"@type": "BlogPosting",
3866
"headline": title,
3967
"description": description,
4068
"url": canonicalUrl,
41-
"image": {
42-
"@type": "ImageObject",
43-
"url": imageUrl,
44-
"caption": imageAlt || title,
45-
"representativeOfPage": "true"
46-
},
69+
"image": imageSchema,
4770
"datePublished": date ? new Date(date).toISOString() : new Date().toISOString(),
4871
"dateModified": modifiedTime ? new Date(modifiedTime).toISOString() : (date ? new Date(date).toISOString() : new Date().toISOString()),
4972
"author": {

0 commit comments

Comments
 (0)