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

Null date throws an error #786

Closed
dceddia opened this issue Nov 22, 2019 · 6 comments · Fixed by #850
Closed

Null date throws an error #786

dceddia opened this issue Nov 22, 2019 · 6 comments · Fixed by #850
Labels

Comments

@dceddia
Copy link
Contributor

dceddia commented Nov 22, 2019

Describe the bug
I'm converting an existing Jekyll blog to Eleventy. I have some pages that have an empty date in the frontmatter, and Eleventy is crashing with the error Cannot read property 'toLowerCase' of null.

To Reproduce
Steps to reproduce the behavior:

  1. Create this file:
---
title: "This blows up"
date:
---
  1. Run eleventy
  2. Boom
TypeError: Cannot read property 'toLowerCase' of null
    at Template.getMappedDate (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/Template.js:606:23)
    at async Template.addPageDate (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/Template.js:256:19)
    at async Template.getData (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/Template.js:237:20)
    at async Template.getTemplateMapEntries (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/Template.js:673:16)
    at async TemplateMap.add (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/TemplateMap.js:32:21)
    at async Promise.all (index 0)
    at async TemplateWriter._createTemplateMap (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/TemplateWriter.js:123:3)
    at async TemplateWriter.write (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/TemplateWriter.js:155:3)
    at async Eleventy.write (/Users/dceddia/.n/lib/node_modules/@11ty/eleventy/src/Eleventy.js:428:13)

Expected behavior
I expected the empty date to be ignored. For some posts or pages the date isn't relevant and I don't want to show it on the page at all. Maybe there's a better way to do this?

Environment:

  • OS and Version: macOS Catalina 10.15.1
  • Eleventy Version 0.9.0

Additional context
Near where it fails, in Template.js, I can fix the crash by changing this line:

if ("date" in data) {

to this:

if ("date" in data && data.date) {

Should I create a PR? Is there a better way to fix it?

@coolsoftwaretyler
Copy link

coolsoftwaretyler commented Nov 23, 2019

Looks like your fix works and it definitely feels clear. There's a check around line 625 for validity:

         if (!date.isValid) {
            throw new Error(
              `date front matter value (${data.date}) is invalid for ${this.inputPath}`
            );
          }

Which makes me feel like checking other types of validity is within the scope of the getMappedDate function.

But let's say we wanted to avoid a change. Does it break convention to have front matter fields that are empty? What happens if you change it to:

date: ""

Instead of just entirely blank? Is it more conventional to explicitly state that you have left this date blank? Or again, is it better to take the date field out if it's unused?

I don't really have any answers or opinions for these questions, but maybe just some prompts to get discussion going about how Eleventy should handle this case in general.

In the short term, would it fit into your workflow to remove the empty frontmatter for now and avoid the errors?

@dceddia
Copy link
Contributor Author

dceddia commented Nov 23, 2019

Yeah those are good questions. The blank file template I'm using came stock with... octopress, I think? And they have a bunch of empty frontmatter fields that Jekyll silently ignores, so I always just left them alone. It does seem a bit odd to have blank fields though, and maybe it'd be clearer (for my own posts) if I left off the date and added a field like timeless: true or something.

I can work around it by removing the empty field for now.

@coolsoftwaretyler
Copy link

I mean, I support adding timeless: true to all your posts, if only for the confidence boost.

@coolsoftwaretyler
Copy link

Did a little thinking and a little reading, and I think you should submit your PR and see what @zachleat thinks.

YAML treats empty nodes as null. Since Eleventy defaults to YAML in its templates, I think it ought to treat YAML the way YAML wants to be treated.

But the date node is given special consideration in Template.js, through the getMappedDate function. This special consideration (attempting to parse front matter with the date node in a specific way) is what's causing the error. I think Eleventy would do well to handle the valid YAML value of null with the check you've proposed.

I have a feeling Jekyll doesn't do anything with date by default, which is part of why this doesn't throw an issue there.

@edwardhorsford
Copy link
Contributor

I had a similar issue with null tags - which I raised in this issue.

Likewise I have a template markdown file with mostly blank keys - date always throws an error first time.

If date isn't required by eleventy, I would support empty keys being ignored (like tags was above).

@zachleat
Copy link
Member

Yep I’m on board with this—take care not to change any existing unit tests in your pull request please! I want to know if anything fails

@zachleat zachleat added bug and removed needs-triage labels Nov 26, 2019
DirtyF added a commit to DirtyF/eleventy that referenced this issue Jan 12, 2020
@zachleat zachleat added this to the Next Minor Version milestone Jan 13, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants