Connecting Sanity to 11ty: tips on serializers, portable text, and fetching data.

7 replies
Last updated: Sep 16, 2021
Does anyone know of a reference or tutorial for connecting Sanity’s to an 11ty’s frontend? The Starters are fine, but I’d like to create it from scratch, and see how everything is connects.
Thanks.
AI Update

I can help you with connecting Sanity to Eleventy from scratch! While there's an official guide available, let me point you to the best resources and give you an overview of how everything connects.

The main tutorial you're looking for is the official Sanity guide on getting started with the 11ty Blog Starter by Bryan Robinson. While it starts with the template, it does a great job explaining the fundamental concepts of how the pieces fit together, which is exactly what you need to understand for building from scratch.

How Everything Connects

Here's the basic architecture you'll be working with:

1. Sanity Studio (Content Management)

  • Lives in a /studio/ directory (or separate repo)
  • Defines your content schemas using JavaScript objects
  • Provides the editing interface for your content
  • Syncs content to Sanity's hosted APIs in real-time

2. Eleventy (Static Site Generator)

  • Lives in your /web/ directory (or main project folder)
  • Fetches content from Sanity using the JavaScript client
  • Uses 11ty's global data pattern to pull content at build time

3. The Connection Point: Global Data Files

This is the key piece! In your _data directory, you create JavaScript files that query Sanity. Here's a simplified example of what a posts.js file might look like:

const sanityClient = require('@sanity/client')
const groq = require('groq')

const client = sanityClient({
  projectId: 'your-project-id',
  dataset: 'production',
  useCdn: true
})

module.exports = async function() {
  const query = groq`*[_type == "post"]{
    title,
    slug,
    body
  }`
  
  const posts = await client.fetch(query)
  return posts
}

4. Using the Data in Templates

Once you have the data, you can use it in your Nunjucks (or Liquid/Handlebars) templates:

{% for post in posts %}
  <h2>{{ post.title }}</h2>
{% endfor %}

Building From Scratch Steps

  1. Set up Sanity Studio: Run npm create sanity@latest to initialize a new Sanity project
  2. Define your schemas: Create content types in /schemas/
  3. Set up 11ty: Initialize an 11ty project with npm init -y and install 11ty
  4. Install Sanity client: npm install @sanity/client groq
  5. Create data files: Add JavaScript files to _data/ that query Sanity using GROQ
  6. Build templates: Use the data in your 11ty templates
  7. Handle Portable Text: Use @portabletext/to-html or similar to render rich text content

Additional Resources

The Sanity + Eleventy landing page explains the benefits of this stack. The guide walks through practical examples like adding excerpts to blog posts, which demonstrates the full data flow from schema → query → template.

For GROQ queries (Sanity's query language), check out the GROQ documentation to understand how to fetch exactly the data you need.

The beauty of this setup is that 11ty's flexibility means you can structure your project however you want - the key is just understanding that data flow from Sanity → JavaScript data files → 11ty templates. Once you grasp that pattern, you can build anything! The starter template is actually a great reference for seeing these patterns in action, even if you're building from scratch.

Hey there! Unfortunately, I don't think anyone has published a guide for connecting to 11ty from scratch. The closest would be this guide , which sort of dissects how the blog starter works.
Thank you, but this article but omits how the code in the
utils
and
_data
foldres came to be. I’m missing a key ideas around serializers, portable text, fetching data, etc.
I got everything working, but do not quite understand how it works together.
Thank you, but this article but omits how the code in the
utils
and
_data
came to be. I’m missing a key ideas around serializers, portable text, fetching data, etc.
I got everything working, but do not quite understand how it works together.
When I figure it out, I’ll write my own article. 🙂
Those are definitely things I struggled to understand when I first got here, as well, and aren't specific to 11ty.
When it comes to fetching data and using the JS client in general,
this page of the docs will help you get a better understanding of it. There's a lot going on there, though, so for now it's probably better to focus on: installation , configuring your client , and performing queries . Speaking of performing queries, you can learn how to use GROQ fairly quickly with this guide .
Portable Text and serializers are some of the hardest things to understand when you start off.
This is a good introduction to setting up your first custom serializers.
Those are definitely things I struggled to understand when I first got here, as well, and aren't specific to 11ty.
When it comes to fetching data and using the JS client in general,
this page of the docs will help you get a better understanding of it. There's a lot going on there, though, so for now it's probably better to focus on: installation , configuring your client , and performing queries . Speaking of performing queries, you can learn how to use GROQ fairly quickly with this guide .
Portable Text and serializers are some of the hardest things to understand when you start off.
This is a good introduction to setting up your first custom serializers.
Thank you for the thoughtful response. I will look into your suggestions.

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?