11ty / eleventy

A simpler site generator. Transforms a directory of templates (of varying types) into HTML.

Home Page:https://www.11ty.dev/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Serverless function error with `getGitLastUpdatedTimeStamp`

mrmartineau opened this issue · comments

Operating system

macOS Ventura 13.1

Eleventy

2.0.0-beta.3

Describe the bug

I have been experiencing issues with my serverless function. I am using it to search my site's content using Algolia. It works locally, but not when published to Netlify. I created a GitHub discussion about the problems I was having and managed to solve some of them, but there is one that I can't resolve which I believe to be a bug of some kind.

Here's an example search page: notez.zander.wtf/search/?query=css. It should return a list of notes that looks like this:
image

My serverless function (searcher) has no problem calling Algolia for data, but when it comes to writing templates based on those results, it has problems. The function's error logs can be seen below:

Feb 6, 01:28:15 PM: 4aa77591 ERROR  [11ty] Problem writing Eleventy templates: (more in DEBUG output)
Feb 6, 01:28:15 PM: 4aa77591 ERROR  [11ty] Cannot read properties of null (reading 'toString') (via TypeError)
Feb 6, 01:28:15 PM: 4aa77591 ERROR  [11ty] 
[11ty] Original error stack trace: TypeError: Cannot read properties of null (reading 'toString')
[11ty]     at getGitLastUpdatedTimeStamp (/var/task/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js:17:16)
[11ty]     at module.exports (/var/task/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js:24:19)
[11ty]     at Template.getMappedDate (/var/task/node_modules/@11ty/eleventy/src/Template.js:941:17)
[11ty]     at Template.addPageDate (/var/task/node_modules/@11ty/eleventy/src/Template.js:403:30)
[11ty]     at Template.getData (/var/task/node_modules/@11ty/eleventy/src/Template.js:390:29)
[11ty]     at async TemplateMap.add (/var/task/node_modules/@11ty/eleventy/src/TemplateMap.js:65:16)
[11ty]     at async TemplateWriter._addToTemplateMap (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:235:7)
[11ty]     at async TemplateWriter._createTemplateMap (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:325:5)
[11ty]     at async TemplateWriter.generateTemplates (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:360:5)
[11ty]     at async TemplateWriter.getJSON (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:418:20)

Feb 6, 01:28:15 PM: 4aa77591 INFO   Serverless Error: TypeError: Cannot read properties of null (reading 'toString')
    at getGitLastUpdatedTimeStamp (/var/task/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js:17:16)
    at module.exports (/var/task/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js:24:19)
    at Template.getMappedDate (/var/task/node_modules/@11ty/eleventy/src/Template.js:941:17)
    at Template.addPageDate (/var/task/node_modules/@11ty/eleventy/src/Template.js:403:30)
    at Template.getData (/var/task/node_modules/@11ty/eleventy/src/Template.js:390:29)
    at async TemplateMap.add (/var/task/node_modules/@11ty/eleventy/src/TemplateMap.js:65:16)
    at async TemplateWriter._addToTemplateMap (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:235:7)
    at async TemplateWriter._createTemplateMap (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:325:5)
    at async TemplateWriter.generateTemplates (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:360:5)
    at async TemplateWriter.getJSON (/var/task/node_modules/@11ty/eleventy/src/TemplateWriter.js:418:20)
Feb 6, 01:28:15 PM: 4aa77591 Duration: 492.26 ms	Memory Usage: 106 MB	Init Duration: 769.38 ms

As you can see there is a problem with the getGitLastUpdatedTimeStamp function. Looking at the source code for the getGitLastUpdatedTimeStamp function, it is spawning git to get the date, but there is no date which causes the error. Why is it doing this on a serverless function?

I use git Last Modified for many dates of the files in this repo, but on this page, as far as I know, it should only be rendering dates from the search results data. I even added date: git Last Modified to the front-matter of my search template, but it did not resolve the issue.

Reproduction steps

My repo is open source and can be built, but I only see this error on Netlify when it is published - not locally. So you'd need to publish it yourself to recreate the issue. You'd also need to setup an Algolia index and then provide some environment variables, but I can provide some env vars if needed.

Expected behavior

The /search page renders and shows a list of search results.

Reproduction URL

https://github.com/mrmartineau/11ty-notes

Screenshots

image

so after a little digging, I now know what the problem but I don't know if it is the intended behaviour. I first patched the DateGitLastUpdated.js file like so:

diff --git a/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js b/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js
index 905af88..038b266 100644
--- a/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js
+++ b/node_modules/@11ty/eleventy/src/Util/DateGitLastUpdated.js
@@ -5,6 +5,7 @@ const spawn = require("cross-spawn");
  * MIT licensed: https://github.com/vuejs/vuepress/blob/89440ce552675859189ed4ab254ce19c4bba5447/LICENSE
  */
 function getGitLastUpdatedTimeStamp(filePath) {
+  console.log(`🚀 ~ getGitLastUpdatedTimeStamp ~ filePath`, filePath)
   return (
     parseInt(
       spawn
@@ -25,4 +26,5 @@ module.exports = function (inputPath) {
   if (timestamp) {
     return new Date(timestamp);
   }
+  return new Date()
 };

I thought that return the date as it is when the serverless function was run would be better than no date, but it didn't work. In fact I now realise that there should be a try...catch block around the body of the function like so:

module.exports = function (inputPath) {
  try {
    let timestamp = getGitLastUpdatedTimeStamp(inputPath);
    if (timestamp) {
      return new Date(timestamp);
    }
  } catch (err) {
    return new Date()
  }
};

I have not tried this, because I figured out that the issue was something else.. but @zachleat does my suggested change make sense? or is it pointless because is not returning a useful date at all..?

When I changed the DateGitLastUpdated.js file, I also logged out the filePath as well. This printed all 41 files that use date: git Last Modified in their front-matter (not all of my notes use that value). After a bit of back and forth, I realised that these files were being "loaded" by the serverless function (is that the correct term?) because I used this (singleTemplateScope: false) in the options for EleventyServerless in my serverless function.

I used singleTemplateScope: false because I wanted my search template to have access to one of the collections (tagList) I had setup in my eleventy.js file. Removing that option is what fixed the issue for me and the search template works!... but the sidebar does not have all the content I want because of the missing tagList data. So now I trying the precompiledCollections (which I tried using before the singleTemplateScope option) but I can't get it to work.

I'm sure I will figure it out soon enough though..

Anyway, let me know what you think I found is a bug or not.

FYI I am still trying to figure out exactly how precompiledCollections work because it is not obvious to me. I created a discussion on the matter to see if anyone can enlighten me..