Watch a live product demo 👀 See how Sanity powers richer commerce experiences

Trouble querying content with 11ty

30 replies
Last updated: May 1, 2020
Heya, I’m trying to make my first site using sanity. I want to use Eleventy as my frontend but I’m having trouble querying the content from my database. Are there any specific guides for eleventy and sanity?
Apr 30, 2020, 12:21 PM
Hi Ben, welcome! 👋 And cool choice with Eleventy (we recently opened an 11ty channel in case you want to join). There’s an Eleventy + Sanity blog boilerplate example over here: https://github.com/kmelve/eleventy-sanity-blog-boilerplate
Are you getting any specific errors when querying content?
Apr 30, 2020, 12:25 PM
Apr 30, 2020, 12:29 PM
Thanks! I haven’t had a look at the boilerplate one so I’ll check it out
Apr 30, 2020, 12:31 PM
the error I’m getting is this one
"Unable to parse entire expression"
Apr 30, 2020, 12:31 PM
Do you have any repo you can share (in DM if you prefer)? Or specific code causing that error if it’s clear where it comes from?
Apr 30, 2020, 12:33 PM
Do you have any repo you can share (in DM if you prefer)? Or specific code causing that error if it’s clear where it comes from?
Apr 30, 2020, 12:33 PM
Apr 30, 2020, 12:29 PM
Thanks! I haven’t had a look at the boilerplate one so I’ll check it out
Apr 30, 2020, 12:31 PM
the error I’m getting is this one
"Unable to parse entire expression"
Apr 30, 2020, 12:31 PM
Apr 30, 2020, 12:42 PM
Thanks Ben! Did it originally work when you made the initial commit? You used Sanity’s 11ty starter for this, correct (the one Knut shared)?
Apr 30, 2020, 12:45 PM
No I started with a clean sanity, structured the database and then was beginning to figure out how to get the data to eleventy which is where I got stuck. But I did have a look at the one that Knut shared to try and help me
Apr 30, 2020, 12:47 PM
In
newsItems.js
, an orphan bracket
}
seems to have sneaked in:
const projection = groq `{
  _id,
  publishedAt,
  title,
  body
  }
}`
Should be:

const projection = groq `{
  _id,
  publishedAt,
  title,
  body
}`
Apr 30, 2020, 1:18 PM
ahhh sneaky! Thanks, I’ll check it
Apr 30, 2020, 1:33 PM
Hey, sorry to keep bothering you. The bracket was indeed the problem but now I’m having trouble previewing the rendered html content in eleventy. When I run
npx @11ty/eleventy --serve
I get an error about the directory for home.njk not existing and when I go to the localhost:8080 it only says
Cannot GET /
Do you have any ideas whats causing this?
May 1, 2020, 8:12 AM
I switched to using the boilerplate that Knut sent above but adjusted everything for my site
May 1, 2020, 8:13 AM
Does your web root have an
index.njk
file including
layout: layouts/home.njk
and is there a
home.njk
file in your
_includes/layouts/
folder? Do you have an updated repo to share by any chance? 🙂
May 1, 2020, 9:05 AM
Also, do you have this section in the return statement of your
.eleventy.js
file?
dir: {
      input: ".",
      includes: "_includes",
      data: "_data",
      output: "_site"
    }
May 1, 2020, 9:06 AM
yes to all of the above!
May 1, 2020, 9:07 AM
I’ve tried deploying it to netlify but thats also having issues too
May 1, 2020, 9:07 AM
The latter would make sense as it might be lacking some Netlify-specific config. It seems to be set up for Vercel (previously known as Zeit Now) - see the
now.json
file for example (although it has instructions for Netlify in the readme). I’ll be able to have a look at the repo in around an hour if that’s OK.
May 1, 2020, 9:09 AM
yeah thats great, thanks for your help!
May 1, 2020, 9:10 AM
What’s the error you’re getting on Netlify?
May 1, 2020, 9:12 AM
its managing the deploy alright but then when I open the studio page sanity stays on the loading screen until it says that there might be a problem with the internet connection
May 1, 2020, 9:15 AM
When I run eleventy from inside the web folder then it works, but from the root it doesn’t
May 1, 2020, 9:21 AM
Hi again Ben, just had some time to go through the repo. Have you tried something like this in your
newsItemsList.njk
file?
<ol reversed class="newsitemslist" style="counter-reset: start-from {{ newsitemslist.length + 1 }}">
{% for newsItem in newsitemslist | reverse %}
  {% set currentNewsItem = newsItem.data.newsItem %}
  <li class="newsitemslist-item{% if newsItem.url == url %} newsitemslist-item-active{% endif %}">
    <a href="{{ newsItem.url | url }}" class="newsitemslist-link">{% if currentNewsItem.title %}{{ currentNewsItem.title }}{% else %}<code>{{ newsItem.url }}</code>{% endif %}</a>
    <time class="newsitemslist-date" date="{{ currentNewsItem.publishedAt }}">{{ currentNewsItem.publishedAt|readableDate }}</time>
  </li>
{% endfor %}
</ol>
May 1, 2020, 3:58 PM
ah I can’t believe I missed that I hadn’t changed ‘post’ to ‘newsItem’
May 1, 2020, 4:02 PM
thank you, it worked perfectly
May 1, 2020, 4:02 PM
Awesome! 🙂
May 1, 2020, 4:04 PM

Sanity– build remarkable experiences at scale

The Sanity Composable Content Cloud is the modern content platform that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Related answers

Get more help in the community Slack

TopicCategoriesFeaturedRepliesLast Updated
After adding the subtitle and running this code npm run graphql-deploy It does nothingSep 15, 2020
how to limit a reference to just one entry in Studio reference input side versus the default as-many-entries-as-you-fill-in-an-array...Sep 18, 2020
Is it possible to fetch more than one "_type" using GROQ?Nov 2, 2020
I want to add a view with the Structure builder (S.view.component) where I list similar documents based on the title. What...Sep 23, 2020
Is there a structure builder example where the format of each preview for the document list is modified?Feb 3, 2021
I have an array of references to a country schema type but it always just returns NULL values for meJan 30, 2021
Hi, I need help with a query for getting the url of an image asset. Here is what I've been trying, but I only get the _ref...Dec 1, 2020
Sanity UI looks brilliant :smiley: Is something like the current date picker possible at the moment? I’m not sure if anicon...Dec 21, 2020
Hey everyone. I have been coding and may have potentially accidentally deleted something. Does anyone know how to resolve...Dec 26, 2020
Hello everyone and happy new year :raised_hands::skin-tone-2:, I have a problem with outputting Portable Text :disappointed:...Jan 1, 2021

Related contributions

Atria
- Made with Sanity

Atria is powering a movement for proactive, preventive healthcare informed by cutting-edge science and technology.

Non-Linear Studio
Go to Atria

OCEG Web Site
- Made with Sanity

Membership site with gated content

Stephane Legay
Go to OCEG Web Site

KIL-boken
- Made with Sanity

A simple one-pager created to showcase an upcoming book release. (Norwegian content)

Håvard Brynjulfsen
Go to KIL-boken