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

setDataFileBaseName() omits extensions other than .11tydata.* for directory data files

rustycoat opened this issue · comments

Operating system

Debian Testing

Eleventy

2.0.0

Describe the bug

eleventyConfig.setDataFileBaseName(newBaseName) changes the base name but in doing so it drops support for .json and any other added extensions and will only search for newBaseName.11tydata.json.

If I enable debug it's clear what's happening (in my case I'm using yml/yaml extensions as well).

Here's a log entry without setDataFileBaseName():
2023-03-01T00:36:28.707Z Eleventy:TemplateData getLocalDataPaths('./content/404.md'): [ './content/404.11tydata.js', './content/404.11tydata.cjs', './content/404.11tydata.json', './content/404.11tydata.yaml', './content/404.11tydata.yml', './content/404.json', './content/404.yaml', './content/404.yml', './content/content.11tydata.js', './content/content.11tydata.cjs', './content/content.11tydata.json', './content/content.11tydata.yaml', './content/content.11tydata.yml', './content/content.json', './content/content.yaml', './content/content.yml' ]

Here's the same log entry after calling setDataFileBaseName('_defaults')
2023-03-01T00:36:36.425Z Eleventy:TemplateData getLocalDataPaths('./content/404.md'): [ './content/404.11tydata.js', './content/404.11tydata.cjs', './content/404.11tydata.json', './content/404.11tydata.yaml', './content/404.11tydata.yml', './content/404.json', './content/404.yaml', './content/404.yml', './content/_defaults.11tydata.js', './content/_defaults.11tydata.cjs', './content/_defaults.11tydata.json', './content/_defaults.11tydata.yaml', './content/_defaults.11tydata.yml' ]

You'll notice the last 3 entries (basename.json, basename.yaml, basename.yml) are missing.

Reproduction steps

  1. Call eleventyConfig.setDataFileBaseName('basename') in eleventy config.
  2. Enable debug and check Eleventy:TemplateData getLocalDataPaths lines for the list of searched paths on any template file and note that "basename.json" is no longer listed in the cascade.

Expected behavior

"basename.json" or any other added data extension is parsed and loaded as a directory data file after calling setDataFileBaseName("basename")

Reproduction URL

No response

Screenshots

No response

The problem is coming from line 606 in TemplateData.js:

if (this.config.dataFileDirBaseNameOverride) {
  let indexDataFile = dir + "/" + this.config.dataFileDirBaseNameOverride;
  this._addBaseToPaths(paths, indexDataFile, userExtensions, true);
} else {
  this._addBaseToPaths(paths, dirPathNoExt, userExtensions);
}

Looking at _addBaseToPaths() the behavior appears intentional:

_addBaseToPaths(paths, base, extensions, nonEmptySuffixesOnly = false)

If set to true it only adds the base.11tydata.ext variants and completely omits the base.ext variants. I'm guessing there's some historical reason or another use case that makes this necessary? Unfortunately, it completely conflicts with the docs and there's no explanation for why this behavior would be in place.

Looks like it's part of this commit (sorry for a regular link, I'm not that familiar with the github platform).

No explanation as to why nonEmptySuffixesOnly was added as a parameter or what the purpose was.

Sorry I saw this one out of order—I did add a comment over at #2872—please subscribe over there!