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

Eleventy skips building paginated pages if the collection is empty. How do I build the page anyway? #1185

Closed
MaybeThisIsRu opened this issue May 15, 2020 · 9 comments · Fixed by #2208

Comments

@MaybeThisIsRu
Copy link

I have a notes.liquid page with the following front-matter:

---
layout: default
title: Notes
pagination:
    data: collections.notes
    size: 10
permalink: "/notes/{% if pagination.pageNumber > 0 %}{{ pagination.pageNumber + 1 }}/{% endif %}"
---

The notes collection is empty. In this case, the page does not build. Navigating to /notes/ returns the following error in a browser:

Cannot GET /notes/

Removing pagination as follows:

---
layout: default
title: Notes
content-description: Paginated page for notes
changeFrequency: daily
permalink: "/notes/"
---

...and then visiting the route works.

It seems Eleventy is not building the page is the collection is empty. Is it possible to change this behaviour? I'd like to display a small piece of paragraph instead of a paginated list on such a page, so I'd like for the page to still build.

@MaybeThisIsRu MaybeThisIsRu changed the title Eleventy seems to not build paginated pages if the collection is empty. How do I build the page anyway? Eleventy skips building paginated pages if the collection is empty. How do I build the page anyway? May 15, 2020
@zachleat
Copy link
Member

Huh, fascinating idea.

Some ideas you could try:

Make a separate notes-empty.liquid template that uses a permalink: "{% if collections.notes.length %}/notes/{% else %}false{% endif %} though I wonder if we handle false the string versus false the boolean there correctly, hmm.

If that doesn’t work you could try using eleventyComputed to set the permalink value using a JavaScript function.

Or you could add a Template Data file notes.11tydata.js that searches the file system to look for any notes in your directory and sets the permalink based on that.

@zachleat
Copy link
Member

I think this is probably eligible for a feature request, just thinking more about it. Do you mind if I add this to the enhancement queue—even if you get it working using the ideas above?

@MaybeThisIsRu
Copy link
Author

Make a separate notes-empty.liquid template that uses a permalink: "{% if collections.notes.length %}/notes/{% else %}false{% endif %} though I wonder if we handle false the string versus false the boolean there correctly, hmm.

You're right, this most likely would get parsed as a string.

If that doesn’t work you could try using eleventyComputed to set the permalink value using a JavaScript function.

Hmm... this is a good idea. It's going to be a bit of an outlier because the rest of my front matter is YAML with Directory Data Files supplying the permalinks anyway. I'll just write the front matter in JS directly for this template. This is probably the best course of action for now. Thank you!

I think this is probably eligible for a feature request, just thinking more about it. Do you mind if I add this to the enhancement queue—even if you get it working using the ideas above?

Yes please! I'm not a fan of keeping a bunch of extra templates (notes, photos, etc.) purely for this purpose so if at all this can be released as an enhancement, it would be amazing.

It was surprising that this happened at all given how much control Eleventy gives to the developer (which is nice!), but holds back with a default behaviour here.

@Abogical
Copy link

Whoever does triaging, this is a duplicate of #731

@WebShapedBiz
Copy link

Hi.

Just want to add that, in my 2 cents' worth opinion, there is an everyday example where pagination should render the page even with empty data.

I'm having a global data file, where I'm getting the category and all the products within that category with API. However, sometimes, there are temporarily no products within one of the categories.
Without being able to render the category page without the pagination data, the visitors will get a 404 error, instead of my page saying "Currently we're not having these articles".

I guess this can be handled at the global data file, but I really think that this should be fixed as soon as possible.

Cheers.

@densevoid
Copy link

I would also like to know if it is possible to display a page that receives an empty object in pagination.

@pdehaan
Copy link
Contributor

pdehaan commented Nov 8, 2021

Note: there are some examples/workarounds in #2078 (reply in thread) discussion.

@WebShapedBiz
Copy link

Thank you @pdehaan for the suggested workarounds. I've managed to resolve this problem in _data file where, in case of no API content, I create an object with an "empty" property. This way I can circumvent the pagination issue and check for an empty property to display appropriate wording on the page, something like "No items to show".

@cmcknight
Copy link

I just did something similar to work around the issue.

I posted an idea for an enhancement on the original bug #731 that doesn't look to overwhelming to implement and allows the developer to determine how they want to handle the custom message.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants