1
0
mirror of https://github.com/oceanprotocol/market.git synced 2024-12-02 05:57:29 +01:00

add dataset search (#1358)

* First-class integration of market with Google Dataset Search.

* Move schema to new SEO section

* fix Unsafe usage of optional chaining
This commit is contained in:
Corrie Sloot 2022-07-27 11:32:42 -04:00 committed by GitHub
parent e04cd8196c
commit ed665befc7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 0 deletions

View File

@ -0,0 +1,77 @@
import { useAsset } from '@context/Asset'
import { useMarketMetadata } from '@context/MarketMetadata'
import useNetworkMetadata, {
filterNetworksByType
} from '@hooks/useNetworkMetadata'
import removeMarkdown from 'remove-markdown'
const DatasetSchema = (): object => {
const { asset, isInPurgatory } = useAsset()
const { networksList } = useNetworkMetadata()
const { appConfig } = useMarketMetadata()
const networksMain = filterNetworksByType(
'mainnet',
appConfig.chainIdsSupported,
networksList
)
// only show schema on main nets
const isMainNetwork = networksMain.includes(asset?.chainId)
const isDataset = asset?.metadata?.type === 'dataset'
if (!asset || !isMainNetwork || !isDataset || isInPurgatory) return null
let isDownloadable = false
if (asset?.services && Array.isArray(asset?.services)) {
for (const service of asset.services) {
if (service?.type === 'access') {
isDownloadable = true
break
}
}
}
// https://developers.google.com/search/docs/advanced/structured-data/dataset
const datasetSchema = {
'@context': 'https://schema.org/',
'@type': 'Dataset',
name: asset?.metadata?.name,
description: removeMarkdown(
asset?.metadata?.description?.substring(0, 5000) || ''
),
keywords: asset?.metadata?.tags,
datePublished: asset?.metadata?.created,
dateModified: asset?.metadata?.updated,
license: asset?.metadata?.license,
...(asset?.accessDetails?.type === 'free'
? { isAccessibleForFree: true }
: {
isAccessibleForFree: false,
paymentAccepted: 'Cryptocurrency',
currenciesAccepted: asset?.accessDetails?.baseToken?.symbol,
offers: {
'@type': 'Offer',
price: asset?.accessDetails?.price,
priceCurrency: asset?.accessDetails?.baseToken?.symbol
}
}),
creator: {
'@type': 'Organization',
name: asset?.metadata?.author
},
...(isDownloadable && {
distribution: [
{
'@type': 'DataDownload',
encodingFormat: ''
}
]
})
}
return datasetSchema
}
export { DatasetSchema }

View File

@ -3,6 +3,7 @@ import Head from 'next/head'
import { isBrowser } from '@utils/index'
import { useMarketMetadata } from '@context/MarketMetadata'
import { DatasetSchema } from './DatasetSchema'
export default function Seo({
title,
@ -22,6 +23,8 @@ export default function Seo({
? `${title} - ${siteContent?.siteTitle}`
: `${siteContent?.siteTitle}${siteContent?.siteTagline}`
const datasetSchema = DatasetSchema()
return (
<Head>
<html lang="en" />
@ -64,6 +67,12 @@ export default function Seo({
<meta name="twitter:creator" content="@oceanprotocol" />
)}
<meta name="twitter:card" content="summary_large_image" />
{datasetSchema && (
<script type="application/ld+json" id="datasetSchema">
{JSON.stringify(datasetSchema).replace(/</g, '\\u003c')}
</script>
)}
</Head>
)
}

View File

@ -64,6 +64,18 @@ export default function AssetActions({
)
: await getFileDidInfo(asset?.id, asset?.services[0]?.id, providerUrl)
fileInfoResponse && setFileMetadata(fileInfoResponse[0])
// set the content type in the Dataset Schema
const datasetSchema = document.scripts?.namedItem('datasetSchema')
if (datasetSchema) {
const datasetSchemaJSON = JSON.parse(datasetSchema.innerText)
if (datasetSchemaJSON?.distribution[0]['@type'] === 'DataDownload') {
const contentType = fileInfoResponse[0]?.contentType
datasetSchemaJSON.distribution[0].encodingFormat = contentType
datasetSchema.innerText = JSON.stringify(datasetSchemaJSON)
}
}
setFileIsLoading(false)
} catch (error) {
LoggerInstance.error(error.message)