Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serverless templates not rendering correct data and associated build issues #2544

Closed
dwkns opened this issue Sep 1, 2022 · 4 comments
Closed
Labels
feature: 🏙 serverless Eleventy Serverless

Comments

@dwkns
Copy link

dwkns commented Sep 1, 2022

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.

@treb0r
Copy link

treb0r commented Sep 1, 2022

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.

@dwkns
Copy link
Author

dwkns commented Oct 28, 2022

Same issue exists on 2.0.0-canaray.16

@dwkns
Copy link
Author

dwkns commented Oct 28, 2022

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 added a commit to dwkns/11ty-website that referenced this issue Oct 28, 2022
Improve description of how Dynamic Slugs to Subset Your Pagination works.
See: 11ty/eleventy#2544

It wasn't clear  (to me) that you have to use an `Object` for your pagination data. Typically a CMS will return an array rather than an object and that produces weird results.
@dwkns
Copy link
Author

dwkns commented Nov 2, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: 🏙 serverless Eleventy Serverless
Projects
None yet
Development

No branches or pull requests

3 participants