Created
September 5, 2024 20:43
-
-
Save tammyhart/ca79313eadc50b3e22c0a719d8911158 to your computer and use it in GitHub Desktop.
Filter tokens in meta title
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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