-
Hey - I wonder how I do dynamic metadata for posts and blog posts in V3 I managed to do this in v2 using I found #10467 (comment) but getting error "Module not found: Error: Can't resolve '@docusaurus/plugin-content-blog/client' in '~/dev/trpc/www/src/theme/BlogPostPage/Metadata'" V2 referenceBelow is how I did it in v2 Doc item// www/src/theme/DocItem/Metadata/index.tsx
import { PageMetadata } from '@docusaurus/theme-common';
import { useDoc } from '@docusaurus/theme-common/internal';
import React from 'react';
import { docsParams } from '../../../../og-image/utils/zodParams';
import { useEnv } from '../../../utils/useEnv';
export default function DocItemMetadata(): React.JSX.Element {
const { metadata } = useDoc();
const { title, description } = metadata;
const env = useEnv();
const ogImg = `${env.OG_URL}/api/docs?${docsParams.toSearchString({
title,
description,
permalink: metadata.permalink,
})}`;
return <PageMetadata title={title} description={description} image={ogImg} />;
} Blog post// www/src/theme/BlogPostPage/Metadata/index.tsx
import { PageMetadata } from '@docusaurus/theme-common';
import { useBlogPost } from '@docusaurus/theme-common/internal';
import React from 'react';
import { blogParams } from '../../../../og-image/utils/zodParams';
import { useEnv } from '../../../utils/useEnv';
export default function BlogPostPageMetadata(): React.JSX.Element {
const { metadata } = useBlogPost();
const { title, description, date, tags, authors, frontMatter } = metadata;
const { keywords } = frontMatter;
const author = authors[0];
const env = useEnv();
const ogImg = `${env.OG_URL}/api/blog?${blogParams.toSearchString({
title: metadata.title,
description: metadata.description,
/* eslint-disable @typescript-eslint/no-non-null-assertion */
authorName: author.name!,
authorTitle: author.title!,
authorImg: author.imageURL!,
/* eslint-enable @typescript-eslint/no-non-null-assertion */
date,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
readingTimeInMinutes: metadata.readingTime!,
})}`;
return (
<PageMetadata
title={title}
description={description}
keywords={keywords}
image={ogImg}
>
<meta property="og:type" content="article" />
<meta property="article:published_time" content={date} />
{authors.some((author) => author.url) && (
<meta
property="article:author"
content={authors
.map((author) => author.url)
.filter(Boolean)
.join(',')}
/>
)}
{tags.length > 0 && (
<meta
property="article:tag"
content={tags.map((tag) => tag.label).join(',')}
/>
)}
</PageMetadata>
);
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
Actually, I reswizzled both of them with eject and installed |
Beta Was this translation helpful? Give feedback.
Actually, I reswizzled both of them with eject and installed
@docusaurus/plugin-content-blog
- seems to have fixed it for now with those as a reference! 👀