Trouble querying content with 11ty
30 replies
Last updated: May 1, 2020
Y
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
P
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?
Are you getting any specific errors when querying content?
Apr 30, 2020, 12:25 PM
K
There's also this “just add water” thing 🙂 https://www.sanity.io/create?template=sanity-io%2Fsanity-template-eleventy-blog
Apr 30, 2020, 12:29 PM
Y
Thanks! I haven’t had a look at the boilerplate one so I’ll check it out
Apr 30, 2020, 12:31 PM
Y
the error I’m getting is this one
"Unable to parse entire expression"
Apr 30, 2020, 12:31 PM
M
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
P
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
K
There's also this “just add water” thing 🙂 https://www.sanity.io/create?template=sanity-io%2Fsanity-template-eleventy-blog
Apr 30, 2020, 12:29 PM
B
Thanks! I haven’t had a look at the boilerplate one so I’ll check it out
Apr 30, 2020, 12:31 PM
B
the error I’m getting is this one
"Unable to parse entire expression"
Apr 30, 2020, 12:31 PM
B
Heres a link to the repository https://github.com/benjaminearl/informal-justice-court
Apr 30, 2020, 12:42 PM
P
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
B
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
P
In
Should be:
newsItems.js, an orphan bracket
}seems to have sneaked in:
const projection = groq `{ _id, publishedAt, title, body } }`
const projection = groq `{ _id, publishedAt, title, body }`
Apr 30, 2020, 1:18 PM
B
ahhh sneaky! Thanks, I’ll check it
Apr 30, 2020, 1:33 PM
B
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 --serveI 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
B
I switched to using the boilerplate that Knut sent above but adjusted everything for my site
May 1, 2020, 8:13 AM
P
Does your web root have an
index.njkfile including
layout: layouts/home.njkand is there a
home.njkfile in your
_includes/layouts/folder? Do you have an updated repo to share by any chance? 🙂
May 1, 2020, 9:05 AM
P
Also, do you have this section in the return statement of your
.eleventy.jsfile?
dir: { input: ".", includes: "_includes", data: "_data", output: "_site" }
May 1, 2020, 9:06 AM
B
yes to all of the above!
May 1, 2020, 9:07 AM
B
here is the repo: https://github.com/benjaminearl/test-sanity-eleventy-site/settings
May 1, 2020, 9:07 AM
B
I’ve tried deploying it to netlify but thats also having issues too
May 1, 2020, 9:07 AM
P
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.jsonfile 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
B
yeah thats great, thanks for your help!
May 1, 2020, 9:10 AM
P
What’s the error you’re getting on Netlify?
May 1, 2020, 9:12 AM
B
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
B
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
P
Hi again Ben, just had some time to go through the repo. Have you tried something like this in your
newsItemsList.njkfile?
<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
B
ah I can’t believe I missed that I hadn’t changed ‘post’ to ‘newsItem’
May 1, 2020, 4:02 PM
B
thank you, it worked perfectly
May 1, 2020, 4:02 PM
P
Awesome! 🙂
May 1, 2020, 4:04 PM
Sanity– build remarkable experiences at scale
Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.