Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Fix incorrect blog metadata

Blue Mouseda83350367fb
diff --git a/web/app/pages/Blog/Post.tsx b/web/app/pages/Blog/Post.tsx
index 5a290771c..b2033d371 100644
--- a/web/app/pages/Blog/Post.tsx
+++ b/web/app/pages/Blog/Post.tsx
@@ -112,31 +112,38 @@ export default function PostSlug() {
<ArticleNav articleRef={articleRef} headings={post.headings} />
)}
{post.title ? (
- <script
- type='application/ld+json'
- dangerouslySetInnerHTML={{
- __html: serializeBlogSchema([
- blogPostSchema(
- post,
- location.pathname,
- getOgImageUrl(
- post.seoTitle || post.title || 'Blog',
- post.seoDescription ||
- post.intro ||
- t('description.blog'),
+ <>
+ <script
+ type='application/ld+json'
+ dangerouslySetInnerHTML={{
+ __html: serializeBlogSchema(
+ blogPostSchema(
+ post,
+ location.pathname,
+ getOgImageUrl(
+ post.seoTitle || post.title || 'Blog',
+ post.seoDescription ||
+ post.intro ||
+ t('description.blog'),
+ ),
),
),
- ...(!post.standalone
- ? [
- blogBreadcrumbs(
- post.title,
- `https://swetrix.com${location.pathname}`,
- ),
- ]
- : []),
- ]),
- }}
- />
+ }}
+ />
+ {!post.standalone && (
+ <script
+ type='application/ld+json'
+ dangerouslySetInnerHTML={{
+ __html: serializeBlogSchema(
+ blogBreadcrumbs(
+ post.title,
+ `https://swetrix.com${location.pathname}`,
+ ),
+ ),
+ }}
+ />
+ )}
+ </>
) : null}
</main>
</div>
diff --git a/web/app/routes/blog._index.tsx b/web/app/routes/blog._index.tsx
index 37be3b241..41d388398 100644
--- a/web/app/routes/blog._index.tsx
+++ b/web/app/routes/blog._index.tsx
@@ -81,26 +81,29 @@ export default function Posts() {
<script
type='application/ld+json'
dangerouslySetInnerHTML={{
- __html: serializeBlogSchema([
- {
- '@context': 'https://schema.org',
- '@type': 'CollectionPage',
- '@id': 'https://swetrix.com/blog#collection',
- url: 'https://swetrix.com/blog',
- name: t('blogPage.title'),
- description: t('blogPage.description'),
- mainEntity: {
- '@type': 'ItemList',
- itemListElement: visiblePosts.map((post, index) => ({
- '@type': 'ListItem',
- position: index + 1,
- name: post.title,
- url: `https://swetrix.com${post.standalone ? '/' : '/blog/'}${post.slug}`,
- })),
- },
+ __html: serializeBlogSchema({
+ '@context': 'https://schema.org',
+ '@type': 'CollectionPage',
+ '@id': 'https://swetrix.com/blog#collection',
+ url: 'https://swetrix.com/blog',
+ name: t('blogPage.title'),
+ description: t('blogPage.description'),
+ mainEntity: {
+ '@type': 'ItemList',
+ itemListElement: visiblePosts.map((post, index) => ({
+ '@type': 'ListItem',
+ position: index + 1,
+ name: post.title,
+ url: `https://swetrix.com${post.standalone ? '/' : '/blog/'}${post.slug}`,
+ })),
},
- blogBreadcrumbs(),
- ]),
+ }),
+ }}
+ />
+ <script
+ type='application/ld+json'
+ dangerouslySetInnerHTML={{
+ __html: serializeBlogSchema(blogBreadcrumbs()),
}}
/>
{visiblePosts.length === 0 && (
diff --git a/web/app/utils/blogMetadata.ts b/web/app/utils/blogMetadata.ts
index 25b1e65be..3c754d737 100644
--- a/web/app/utils/blogMetadata.ts
+++ b/web/app/utils/blogMetadata.ts
@@ -160,7 +160,9 @@ export function blogArticleMeta(post?: BlogMetadata | null) {
]
}
-export function serializeBlogSchema(value: unknown): string {
+export function serializeBlogSchema(
+ value: Record<string, unknown> & { '@context': string },
+): string {
return JSON.stringify(value)
.replace(/</g, '\\u003c')
.replace(/\u2028|\u2029/g, '')
diff --git a/web/scripts/tests/blog-metadata.test.mjs b/web/scripts/tests/blog-metadata.test.mjs
index 4785ad957..e974fc443 100644
--- a/web/scripts/tests/blog-metadata.test.mjs
+++ b/web/scripts/tests/blog-metadata.test.mjs
@@ -117,6 +117,7 @@ test('describes the blog hierarchy and safely serializes article titles', () =>
assert.equal(crumbs[1].item, 'https://swetrix.com/blog')
assert.equal(blogBreadcrumbs().itemListElement.length, 2)
const encoded = serializeBlogSchema({
+ '@context': 'https://schema.org',
headline: '</script><script>alert(1)</script>',
})
assert.equal(encoded.includes('<'), false)
@@ -125,3 +126,21 @@ test('describes the blog hierarchy and safely serializes article titles', () =>
'</script><script>alert(1)</script>',
)
})
+
+test('keeps blog schemas readable by consumers expecting a top-level context', () => {
+ const article = blogPostSchema(
+ { title: 'Article', date: 'September 24, 2026' },
+ '/blog/article',
+ 'https://swetrix.com/cover.png',
+ )
+ const breadcrumbs = blogBreadcrumbs(
+ 'Article',
+ 'https://swetrix.com/blog/article',
+ )
+
+ for (const schema of [article, breadcrumbs, blogBreadcrumbs()]) {
+ const parsed = JSON.parse(serializeBlogSchema(schema))
+ assert.equal(parsed['@context'].toLowerCase(), 'https://schema.org')
+ assert.deepEqual(parsed, schema)
+ }
+})