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

Created on 15 May 2020  路  6Comments  路  Source: 11ty/eleventy

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.

education

All 6 comments

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鈥檛 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.

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鈥攅ven if you get it working using the ideas above?

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鈥檛 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鈥攅ven 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.

Whoever does triaging, this is a duplicate of #731

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.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

aaronstezycki picture aaronstezycki  路  3Comments

zac-heisey picture zac-heisey  路  3Comments

zellwk picture zellwk  路  3Comments

veleek picture veleek  路  3Comments

robdodson picture robdodson  路  3Comments