Pagination error "Cannot read property 'getTime' of undefined" in Eleventy

6 replies
Last updated: Jan 28, 2026
Hi again, I'm trying to create a content type called pages (from sanity+eleventy base template), I've got it to work on the sanity side and they show up in the studio but when I make adjustments in the eleventy-associated files I run into trouble. Basically I tried to just copy all the post.njk-files and then replace the names in them with 'page' or 'pages' depending on context. When I reached the top level page.njk I get
 Cannot read property 'getTime' of undefined
sanity-eleventy-blog-web: `TypeError` was thrown:
sanity-eleventy-blog-web:     TypeError: Cannot read property 'getTime' of undefined
sanity-eleventy-blog-web:         at sortFunctionDateInputPath
this seems to be dependent on the pagination, I think? After some googling. Any advice appreciated
🙂
AI Update

This error is happening because your new "pages" content type likely doesn't have a publishedAt date field (or whatever date field the sortFunctionDateInputPath is trying to access), but the Eleventy template code you copied is still trying to sort by that date.

When you copied the post.njk files and renamed them for pages, the template is still expecting date properties that exist on blog posts but might not exist on your pages. The getTime() method is being called on undefined because the date field doesn't exist.

Here's how to fix it:

Option 1: Add a date field to your pages schema

In your Sanity Studio schema for pages, make sure you have a publishedAt field (or whatever field name the sort function expects):

{
  name: 'page',
  type: 'document',
  fields: [
    // ... other fields
    {
      name: 'publishedAt',
      type: 'datetime',
      title: 'Published at',
    }
  ]
}

Option 2: Modify the sort function in your Eleventy config

If pages don't need dates (which makes sense for static pages), you'll need to modify the pagination configuration. Look in your .eleventy.js or eleventy.config.js file for where sortFunctionDateInputPath is defined, and create a different sort function for pages that doesn't rely on dates:

// For pages, sort by title or slug instead of date
eleventyConfig.addCollection("pages", function(collectionApi) {
  return collectionApi.getAll()
    .filter(item => item.data.contentType === 'page')
    .sort((a, b) => {
      // Sort alphabetically by title or slug
      return a.data.title.localeCompare(b.data.title);
    });
});

Option 3: Make the sort function handle missing dates

Find the sortFunctionDateInputPath function and add a null check:

function sortFunctionDateInputPath(a, b) {
  const dateA = a.publishedAt || a.data.publishedAt;
  const dateB = b.publishedAt || b.data.publishedAt;
  
  if (!dateA) return 1;  // Push items without dates to the end
  if (!dateB) return -1;
  
  return dateB.getTime() - dateA.getTime();
}

The most likely culprit is in your pagination configuration in the frontmatter of your page.njk file. Look for something like before: sortFunctionDateInputPath and either remove it or change it to a sort function that works for pages without dates.

Show original thread
6 replies

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?