eleventy
eleventy copied to clipboard
Serverless templates not rendering correct data and associated build issues
I'm trying to create a live preview system for a CMS
Consider this data:
[
{
"name": "post1",
"content": "Content for Post 1"
},
{
"name": "post2",
"content": "Content for Post 2"
},
{
"name": "post3",
"content": "Content for Post 3"
}
]
And this template:
---
layout: _page-skeleton.njk
pagination:
data: posts
alias: post
size: 1
# serverless: eleventy.serverless.path.id
# addAllPagesToCollections: true
permalink:
build: "/posts/{{ post.name | slug }}/"
preview: "/preview/posts/:id"
---
<h1>Hello from: {{post.name}}</h1>
<p>{{post.content}}</p>
It builds /posts/post1/
, /posts/post2/
, /posts/post3/
as expected.
The serverless routes on /preview/posts/post1/
, /preview/posts/post2/
, /preview/posts/post3/
all work, however they all show the data from post1
. I think this is expected?
As I understand it from the documentation I should uncomment serverless: eleventy.serverless.path.id
which should match id
to the slug and therefore render the correct content.
However when I do that, two things happen:
- Only
post1
gets builtpost2
andpost3
do not, no error is given, which was unexpected. Make sure you dorm -rf _site netlify netlify.toml .netlify && netlify dev
otherwise you can get caught out testing this. - None of the serverless
/preview/posts/
routes render. I get an"error": "json.filter is not a function"
error in the browser andCould not find pagination data, went looking for: post2 (via Error)
type errors in the console when I try to visit those pages.
If I add in addAllPagesToCollections: true
(thanks to @chriskirknielsen in Discord) it solves the first issue. But I can't get the /preview/posts/...
routes to render with the correct data.
It happens in 1.0.2
& 2.0.0-canary.15
Is this a bug, a couple of bugs? Or am I doing something wrong?
Environment:
- OS and Version: Mac (12.5)
- Eleventy Version:
1.0.2
&2.0.0-canary.15
Additional context Repo to reproduce
I looked at the this code (from the 11ty website) the only differences I can see are :
- The 11ty websites Authors are rendered with on demand builders rather standard serverless.
- It is not simultaneously building and doing serverless routes in the same template.
I don't know if either of these are relevant.
I am seeing the same issue with Eleventy serverless. The data from the first item in the pagination array is returned regardless of the route. Using 2.0.0-canary.15.
Same issue exists on 2.0.0-canaray.16
OK finally worked it out. Your data has to be an Object
not an Array
.
If you use this as your data pagination data structure it will work as expected.
{
"post1": {
"name": "post1",
"content": "Content for Post 1"
},
"post2": {
"name": "post2",
"content": "Content for Post 2"
},
"post3": {
"name": "post3",
"content": "Content for Post 3"
}
}
Closing this as it's a documentation issue, not a Eleventy one. If anyone else comes across it, there is a working prototype here: https://github.com/dwkns/eleventy-serverless-issue