New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@gearbox-built/sanity-structured-data

Package Overview
Dependencies
Maintainers
0
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@gearbox-built/sanity-structured-data

A Sanity plugin that enables easy implementation of JSON-LD structured data using Schema.org vocabulary.

0.4.0
latest
Source
npm
Version published
Weekly downloads
74
1133.33%
Maintainers
0
Weekly downloads
 
Created
Source

⚡️ Sanity Structured Data

A Sanity plugin that enables easy implementation of JSON-LD structured data using Schema.org vocabulary.

Inspired by Operation Nation's sanity-plugin-schema-markup plugin.

Installation

npm install @gearbox-built/sanity-structured-data

Sanity Usage

Add it as a plugin in sanity.config.ts (or .js):

Basic Implementation

// sanity.config.ts
import {defineConfig} from 'sanity'
import {structuredData} from '@gearbox-built/sanity-structured-data'

export default defineConfig({
  //...
  plugins: [structuredData()],
})

Add the structuredData schemaField to your document:

const page = {
  type: 'document',
  name: 'Page',
  fields: [
    {
      title: 'Structured Data',
      name: 'structuredData',
      type: 'structuredData',
    },
  ],
}

Or if you're using @gearbox-built/sanity-schema-tool:

import {F} from '@gearbox-built/sanity-schema-tool'
const page = F.document({
  name: 'Page',
  fields: [F.field('structuredData')],
})

Config Options

structuredData({
  apiVersion: '',
  types: {
    article: {},
    webSite: {},
    webPage: {},
    //...
  },
  queries: {
    company: '',
    websiteDescription: '',
    //...
  },
})
apiVersion

Default: v2021-03-25

Sanity API version.

types

Schema type overrides map. Replaces Sanity schema for the respective Schema.org schema type.

Note: Directly overrides default schema structure. Use Types Helpers for default schema structure.

queries

Query overrides map. Default GROQ queries for schema fields that query the Sanity dataset for values (query fields).

Helpers

To make customization more simple, default schema, types, fields, utilites.

Types

Default schema types (ie. WebPage, WebSite, etc.).

import {types} from '@gearbox-built/sanity-structured-data'
const {webPage, videoObject} = types

console.log(webPage)
// {
//   type: 'object',
//   name: 'WebPageDataType',
//   title: 'WebPage',
//   fields: [ ... ]
//   //...
// }
Fields

Default schema types and custom Schema Tool fields as field fuctions.

Supports merging props with full TypeScript support.

import {fields} from '@gearbox-built/sanity-structured-data'
const {webPage} = fields

console.log(webPage({title: 'Definitely Not WebPage'}))
// {
//   type: 'object',
//   name: 'WebPageDataType',
//   title: 'Definitely Not WebPage', // ← Prop merged
//   fields: [ ... ]
//   //...
// }
Structured Data Config

For more complex configuration needs, abstracting configuration may be desired. To assist with this, you can use the structuredDataConfig helper:

// sanity.config.ts
import {defineConfig} from 'sanity'
import {structuredData} from '@gearbox-built/sanity-structured-data'
import {config} from './structuredDataConfig'

export default defineConfig({
  //...
  plugins: [structuredData(config)],
})
// structuredDataConfig.ts
import {structuredDataConfig, types} from '@gearbox-built/sanity-structured-data'
import {F} from '@gearbox-built/sanity-schema-tool'

// WebSite schema additions
const {fields: webSiteFields, ...webSiteType} = types.webSite

const webSiteAdditionalFields = [
  F.string({name: 'about', description: 'The subject matter of the content.'}),
]

const webSite = {
  ...webSiteType,
  title: 'Definitely Not WebSite',
  fields: webSiteFields.toSpliced(2, 0, ...webSiteAdditionalFields), // Order fields
}

export const config = structuredDataConfig({
  types: {
    webSite, // Replaces default WebSite schema
  },
  queries: {
    company: `*[_type == 'settings'][0].companyName`,
  },
})

Frontend Component

Render your structured data markup in your React/Next.js app. Can be used with or without the structuredData Sanity schema.

import StructuredData, {Schema} from '@gearbox-built/sanity-structured-data/component'

type Props = {
  structuredData: Schema[]
}

const projectId = process.env.NEXT_PUBLIC_SANITY_PROJECT_ID
const dataset = process.env.NEXT_PUBLIC_SANITY_DATASET

const SchemaMarkup = ({structuredData}: Props) => (
  <StructuredData data={structuredData} projectId={projectId} dataset={dataset} />
)

export default SchemaMarkup

License

MIT © Gearbox Development Inc.

Develop & test

This plugin uses @sanity/plugin-kit with default configuration for build & watch scripts.

See Testing a plugin in Sanity Studio on how to run this plugin with hotreload in the studio.

Keywords

sanity

FAQs

Package last updated on 04 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts