eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Serverless templates not rendering correct data and associated build issues

Open dwkns opened this issue 2 years ago • 1 comments

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:

  1. Only post1 gets built post2 and post3 do not, no error is given, which was unexpected. Make sure you do rm -rf _site netlify netlify.toml .netlify && netlify dev otherwise you can get caught out testing this.
  2. None of the serverless /preview/posts/ routes render. I get an "error": "json.filter is not a function" error in the browser and Could 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.

dwkns avatar Sep 01 '22 11:09 dwkns

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.

treb0r avatar Sep 01 '22 20:09 treb0r

Same issue exists on 2.0.0-canaray.16

dwkns avatar Oct 28 '22 10:10 dwkns

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"
  }
}

dwkns avatar Oct 28 '22 15:10 dwkns

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

dwkns avatar Nov 02 '22 17:11 dwkns