What Are Type Taxonomies and Why They Matter
Taxonomy pages are essential for organizing and navigating content-rich websites. Whether you're building a blog with categories, an e-commerce site with product classifications, or a resource library with topic hierarchies, the combination of Gatsby and Sanity.io provides a powerful solution for creating fast, scalable, and manageable taxonomy systems.
This guide walks you through the complete process of building taxonomy pages with Gatsby and Sanity.io, from setting up your content schema to generating dynamic pages that scale with your content. By the end, you'll have a robust taxonomy system that your content editors can easily manage through Sanity's intuitive interface while delivering excellent performance through Gatsby's static generation.
Type taxonomies organize concepts in hierarchical "is-a" relationships, where each child category describes a more specialized type of thing than its parent category. Unlike flat categorization, taxonomies create meaningful hierarchies that help users navigate from general topics to specific content, while also providing valuable structure for search, recommendations, and content management. This approach is particularly valuable for AI-powered content platforms that need sophisticated content organization.
Everything you need to build powerful taxonomy systems
SKOS-Compliant Taxonomies
Use the Sanity Taxonomy Manager plugin for standards-compliant controlled vocabularies with hierarchical relationships.
Programmatic Page Generation
Automatically generate taxonomy pages using gatsby-node.js based on your Sanity schema structure.
Dynamic Navigation
Build navigation menus and cross-links that update automatically as taxonomy content changes.
GraphQL Data Layer
Leverage Gatsby's GraphQL layer to query taxonomy relationships and content efficiently.
Setting Up Your Sanity Schema for Taxonomies
Before building taxonomy pages in Gatsby, you need to design and implement your taxonomy structure in Sanity. This involves creating schemas for taxonomy documents and relating your content to taxonomy terms.
In Sanity, taxonomies are typically stored as separate document types that content editors can reference from other documents. The reference field type in Sanity creates relationships between documents by storing a _ref field that points to another document's _id. This relational approach maintains data integrity and enables powerful querying across your content ecosystem.
For more complex taxonomy needs, consider using the Sanity Taxonomy Manager plugin, which provides a specialized interface for managing SKOS-compliant vocabularies. This plugin adds two document types to your Sanity Studio: SKOS Concept and SKOS Concept Scheme, along with tools for creating, editing, and organizing taxonomy terms in a hierarchical structure.
1// schemas/taxonomy.js2export default {3 name: 'taxonomy',4 title: 'Taxonomy',5 type: 'document',6 fields: [7 {8 name: 'title',9 title: 'Title',10 type: 'string',11 validation: Rule => Rule.required()12 },13 {14 name: 'slug',15 title: 'Slug',16 type: 'slug',17 options: { source: 'title', maxLength: 96 }18 },19 {20 name: 'description',21 title: 'Description',22 type: 'text',23 rows: 324 },25 {26 name: 'parent',27 title: 'Parent Category',28 type: 'reference',29 to: [{type: 'taxonomy'}]30 }31 ]32}Installing and Configuring Sanity Taxonomy Manager
The Sanity Taxonomy Manager plugin provides a specialized interface for managing controlled vocabularies using SKOS standards. This plugin adds two document types to your Sanity Studio: SKOS Concept and SKOS Concept Scheme.
Concept Schemes allow you to create multiple taxonomies within a single project while maintaining a single source of truth for each concept. This is useful when your content spans different domains that share some vocabulary but also have domain-specific terms. For example, a technology news site might have a shared concept scheme for publication types while maintaining separate concept schemes for hardware categories and software categories.
SKOS Concepts represent individual terms within your taxonomy, with built-in support for preferred labels, alternative labels (synonyms), definitions, and semantic relationships. Each concept can be related to other concepts through broader/narrower (parent/child) relationships, related matches (associations), and exact/close/narrow/broad matches.
Hierarchical Depth in SKOS supports multi-level taxonomies, typically up to 5 levels deep. This depth is sufficient for most content organization needs while maintaining performance and semantic integrity. A typical hierarchy might look like: Resources > Guides > Technology > Web Development > Frontend Frameworks.
When integrating taxonomies with content documents, use the reference fields provided by the Taxonomy Manager plugin to ensure only valid taxonomy terms can be selected. This prevents editors from accidentally creating broken references or using inappropriate terms, maintaining the integrity of your content organization system.
Configuring Gatsby with gatsby-source-sanity
With your Sanity taxonomy schema in place, the next step is configuring Gatsby to source and query your taxonomy data. The gatsby-source-sanity plugin handles this integration seamlessly.
After configuration, the plugin creates nodes in Gatsby's GraphQL data layer for each document type in your Sanity schema. For taxonomy documents, you'll see nodes like SanityTaxonomy for top-level category nodes and connected content documents with taxonomy references.
The plugin resolves references automatically, meaning you can query related data in a single GraphQL query without multiple API calls. This resolution happens at build time, so your pages are fully generated from the resolved data. The nested structure allows you to retrieve full taxonomy hierarchies along with content, enabling rich page templates that display breadcrumb navigation, related categories, and filtered content views.
Setting watchMode to true enables live reload during development, while the graphqlTag option specifies which GraphQL tag to use for queries. This configuration is essential for building modern web applications with real-time content updates.
1// gatsby-config.js2module.exports = {3 plugins: [4 {5 resolve: 'gatsby-source-sanity',6 options: {7 projectId: 'your-project-id',8 dataset: 'production',9 token: process.env.SANITY_TOKEN,10 graphqlTag: 'default',11 watchMode: true,12 overlayDrafts: false13 }14 }15 ]16}Programmatic Page Creation with gatsby-node.js
Gatsby's node API enables programmatic page creation based on your Sanity data. This is where taxonomy pages come to life, with each taxonomy term generating a page that lists associated content.
The context object passes data to your page template, including the taxonomy ID which allows the template to query for content associated with that specific taxonomy term. For multi-level taxonomies, you might want URLs that reflect the hierarchy, creating paths like /technology/programming/javascript/ that clearly communicate the taxonomy structure to both users and search engines.
When querying content that references taxonomies, you can traverse the relationships in a single GraphQL query. The elemMatch filter ensures that only content referencing this specific taxonomy term are included in the results. This pattern can be extended to support more complex filtering, such as including content from child taxonomy terms for comprehensive category pages.
Building taxonomy systems with Gatsby and Sanity.io creates a powerful foundation for AI-powered content management that scales with your content growth.
1// gatsby-node.js2exports.createPages = async ({graphql, actions, reporter}) => {3 const {createPage} = actions4 5 const result = await graphql(`6 {7 allSanityTaxonomy {8 nodes {9 id10 slug { current }11 title12 }13 }14 }15 `)16 17 const taxonomyTemplate = path.resolve('./src/templates/taxonomy.js')18 19 result.data.allSanityTaxonomy.nodes.forEach(taxonomy => {20 if (taxonomy.slug?.current) {21 createPage({22 path: `/taxonomy/${taxonomy.slug.current}`,23 component: taxonomyTemplate,24 context: {25 id: taxonomy.id,26 slug: taxonomy.slug.current27 }28 })29 }30 })31}Best Practices and Common Patterns
Building taxonomy systems involves many design decisions. These best practices will help you create maintainable, performant solutions.
Maintaining Taxonomy Integrity
As your taxonomy grows, establish governance practices to prevent drift and inconsistency. Document naming conventions and hierarchy rules in a style guide that content editors can reference. Schedule periodic reviews of taxonomy usage to identify terms that aren't being applied consistently or categories that contain too few items to be useful. When adding new taxonomy terms, consider how they relate to existing terms. New concepts should fit naturally into the hierarchy, with clear parent-child relationships. Avoid creating deep hierarchies that make navigation cumbersome--five levels should be the maximum.
Performance at Scale
For sites with thousands of taxonomy pages, consider incremental builds to regenerate only changed pages rather than rebuilding the entire site. Implement pagination for taxonomy pages with many items to keep page sizes manageable. Use proper caching headers for taxonomy pages that don't change frequently, reducing load on your build infrastructure. Optimize GraphQL queries with fragments or selective field loading to improve build performance.
Troubleshooting Common Issues
References Not Resolving: Verify gatsby-source-sanity configuration and rebuild after schema changes. Check that reference fields are properly defined in your Sanity schema.
Duplicate Pages: Handle edge cases like taxonomy terms without slugs or terms whose slugs conflict with other routes. Use Gatsby's pathPrefix configuration if needed.
Missing Content: Use GraphQL introspection to verify the shape of your data and check that filter conditions match the actual data structure.
Build Performance: Use Gatsby's GraphQL query execution analysis to identify bottlenecks and optimize with fragments or selective field loading.
Implementing a robust taxonomy system is essential for enterprise content platforms that require sophisticated content organization and navigation.
Frequently Asked Questions
What is the difference between a taxonomy and tags?
Taxonomies are hierarchical, controlled vocabularies with defined relationships between terms. Tags are typically flat, free-form labels without enforced structure. Taxonomies provide better navigation and organization, while tags offer flexibility.
How deep should my taxonomy hierarchy go?
Most use cases are well-served by 3-4 levels of hierarchy. Sanity's Taxonomy Manager supports up to 5 levels, which helps maintain semantic integrity while accommodating complex categorization needs.
Can I use Gatsby with multiple Sanity taxonomies?
Yes, you can create multiple Concept Schemes in Sanity to organize different taxonomies. Each scheme maintains its own hierarchy while allowing cross-references between schemes when needed.
How do I handle internationalization with taxonomies?
You can maintain separate taxonomies per locale with translated titles, or use Sanity's internationalization features to store translations within single documents. Plan early to avoid costly migrations.
What's the best way to migrate existing content to a new taxonomy?
Plan a migration strategy that maps existing categories to new taxonomy terms. Use Sanity's import tools to bulk-update references. Test the migration on a staging dataset before production.
Sources
- Sanity.io: Type Taxonomy Setup & Use - Official documentation on setting up SKOS-compliant type taxonomies using the Sanity Taxonomy Manager plugin
- Gatsby: Sourcing from Sanity - Official documentation on configuring gatsby-source-sanity plugin and page creation patterns
- Sanity Taxonomy Manager - Plugin documentation and installation instructions for managing controlled vocabularies
- CSS-Tricks: How to Make Taxonomy Pages With Gatsby and Sanity.io - Comprehensive tutorial covering the complete workflow from schema setup to page generation