generated from sanity-io/template-nextjs-personal-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor package.json to add next-seo dependency
- Loading branch information
1 parent
0262e25
commit 7bbdaba
Showing
9 changed files
with
362 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
import { NextSeo } from 'next-seo' | ||
import { | ||
MetaTag as NextSeoMetaTag, | ||
OpenGraph as NextSeoOpenGraph, | ||
} from 'next-seo/lib/types' | ||
import type { PropsWithChildren } from 'react' | ||
import React, { useMemo } from 'react' | ||
|
||
import { | ||
CustomImageType, | ||
MetaAttributeType, | ||
MetaTagType, | ||
OpenGraphType, | ||
SeoType, | ||
} from '../../types/index' | ||
|
||
export const getOpenGraph = (args: OpenGraphType) => { | ||
const { description, image, title, _type, siteName, url } = args | ||
const getImage = image ? resolveImage(image) : null | ||
const values = { | ||
_type, | ||
description, | ||
siteName, | ||
url, | ||
title, | ||
images: [{ url: getImage ?? '' }], | ||
} | ||
return values as NextSeoOpenGraph | ||
} | ||
|
||
export const getMetaObjects = ( | ||
tags: MetaTagType[], | ||
allowIndexing?: boolean, | ||
) => { | ||
const tagArray: NextSeoMetaTag[] = [] | ||
tags.map((tag) => { | ||
const excludeTag = | ||
!allowIndexing && | ||
!!tag.metaAttributes?.find( | ||
(i) => | ||
i?.attributeValueString?.includes('noindex') || | ||
i?.attributeValueString?.includes('nofollow'), | ||
) | ||
if (!excludeTag) { | ||
const metaTag = getMetaAttribute(tag?.metaAttributes) | ||
if (metaTag) { | ||
tagArray.push(metaTag) | ||
} | ||
} | ||
}) | ||
return tagArray | ||
} | ||
|
||
export const resolveImage = (image?: CustomImageType) => { | ||
return image?.asset?.url ?? '' | ||
} | ||
|
||
export const getMetaAttribute = (attrs: MetaAttributeType[] | undefined) => { | ||
if (!attrs) { | ||
return null | ||
} | ||
const obj: Record<string, string> = {} | ||
attrs.map((i) => { | ||
Object.assign(obj, { | ||
[i?.attributeKey as string]: | ||
i.attributeType == 'image' | ||
? resolveImage(i?.attributeValueImage) | ||
: i.attributeValueString, | ||
}) | ||
}) | ||
return obj as unknown as NextSeoMetaTag | ||
} | ||
|
||
interface CustomNextSeoProps { | ||
seo: SeoType | null | ||
slug: string | ||
} | ||
|
||
const CustomNextSeo: React.FC<PropsWithChildren<CustomNextSeoProps>> = ({ | ||
seo, | ||
children, | ||
slug, | ||
}) => { | ||
const { | ||
additionalMetaTags, | ||
metaDescription, | ||
metaTitle, | ||
twitter, | ||
nofollowAttributes, | ||
seoKeywords, | ||
} = seo || {} | ||
|
||
const tags = useMemo( | ||
() => (additionalMetaTags ? getMetaObjects(additionalMetaTags) : []), | ||
[additionalMetaTags], | ||
) | ||
const openGraph = useMemo( | ||
() => (seo?.openGraph ? getOpenGraph(seo?.openGraph) : undefined), | ||
[seo], | ||
) | ||
const url = | ||
(process.env.NEXT_PUBLIC_APP_URL ?? 'https://ece21.vercel.app') + | ||
(slug?.startsWith('/') ? slug : `/${slug}`) | ||
|
||
return ( | ||
<> | ||
<NextSeo | ||
themeColor="" | ||
twitter={{ | ||
handle: twitter?.creator, | ||
site: twitter?.site, | ||
cardType: twitter?.cardType, | ||
}} | ||
nofollow={nofollowAttributes} | ||
noindex={nofollowAttributes} | ||
openGraph={openGraph} | ||
canonical={url || ''} | ||
additionalMetaTags={( | ||
(seoKeywords && seoKeywords?.length > 0 | ||
? [{ name: 'keywords', content: seoKeywords?.join(', ') }] | ||
: []) as NextSeoMetaTag[] | ||
).concat(tags ?? [])} | ||
title={metaTitle ?? 'ঋণাত্মক - ২১'} | ||
description={ | ||
metaDescription ?? | ||
'A documentary application of HSTU: ECE21 batch! Bought to you by ঋণাত্মক - ২১.' | ||
} | ||
/> | ||
{children} | ||
</> | ||
) | ||
} | ||
|
||
export default CustomNextSeo |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
Oops, something went wrong.