Skip to content

Instantly share code, notes, and snippets.

@tammyhart
Created September 5, 2024 20:43
Show Gist options
  • Save tammyhart/ca79313eadc50b3e22c0a719d8911158 to your computer and use it in GitHub Desktop.
Save tammyhart/ca79313eadc50b3e22c0a719d8911158 to your computer and use it in GitHub Desktop.
Filter tokens in meta title
export async function generateMetadata({ searchParams }: ThoughtsProps): Promise<Metadata> {
const tagId = searchParams?.tag ?? null
const [tagData, indexData] = await Promise.all([
getStrapiData(`tags?${tagIdQuery(tagId)}`),
getStrapiData("thoughts-index?populate=*"),
])
let tagName = tagData?.data[0]?.name ?? null
tagName = tagName ? `on ${tagName}` : undefined
const seo = indexData?.data.seo
return pageMetadata(seo, ["$tagName", tagName])
}
type Filter = [token: string, replacement?: string]
const filterString = (string: string, filter: Filter) => {
const [token, replacement = ""] = filter
return string.split(token).join(replacement)
}
export const pageMetadata = (seo: Seo, titleFilter?: Filter) => {
const { title, description } = seo ?? {}
return {
...(title && { title: titleFilter ? filterString(title, titleFilter) : title }),
...(description && { description }),
}
}
// input: Thoughts $tagName • Tammy Hart • Frontend Engineering & Design Insights
// output on index: Thoughts • Tammy Hart • Frontend Engineering & Design Insights
// output on when tagName exists: Thoughts on TypeScript • Tammy Hart • Frontend Engineering & Design Insights
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment