Skip to content

Regression: functions inside eleventyComputed arrays are not evaluated (array → object → function) #3953

@asentris

Description

@asentris

Operating system

Windows 10

Eleventy

Broken: 3.1.0-alpha.1 - latest version, Works: 3.0.1-alpha.5, 3.0.0, etc.

Describe the bug

When a computed value is a function nested inside an array, Eleventy 3.1.0-alpha.1+ no longer evaluates it. The key silently disappears or the function value is left unresolved. The same code works in 3.0.x.

Here's a minimal data structure in _data/eleventyComputed.mjs :

export default {
  schemaorg: {
    "@graph": [
      {
        // This is not evaluated in 3.1.0-alpha.1 (key disappears)
        name: (data) => data?.client?.name ?? "NOPE",
      }
    ]
  }
};

In 3.0.x, name resolves to the string; in 3.1.0-alpha.1, name is missing in the final object.

I'm not certain, but I believe the following code in the file src/Template.js may be the cause of the change:

Prior code:

} else if (Array.isArray(obj) || isPlainObject(obj)) {

Changed code in 3.1.0-alpha.1:

} else if(Array.isArray(obj)) {
	// Arrays are treated as one entry in the dependency graph now
	computedData.addTemplateString(parentKey,
		async function(innerData) {
			return Promise.all(obj.map(entry => {
				if(typeof entry === "string") {
					return this.tmpl.renderComputedData(entry, innerData);
				}
				return entry;
			}));
		},
		declaredDependencies,
		this.getParseForSymbolsFunction(obj),
		this);
} else if(isPlainObject(obj)) {

Reproduction steps

Open the sample repo linked below. The broken-version branch won't work. The working-version branch will work.

OR follow the steps below.

Create _data/client.mjs:

export default {
  name: "Acme Co."
};

Create _data/eleventyComputed.mjs:

export default {
  schemaorg: {
    "@context": "https://schema.org",
    "@graph": [
      {
        "@type": "WebPage",
        // Function nested under array element:
        name: (data) => data?.client?.name ?? "NOPE"
      }
    ]
  }
};

In a template (e.g. base.njk or base.html) render:

<script eleventy:ignore type="application/ld+json">
{{ schemaorg | dump | safe }}
</script>

Run with Eleventy 3.1.0-alpha.1 → the name field under @graph[0] is missing (function not evaluated).
Run with 3.0.1-alpha.5 or 3.0.0 → name appears as "Acme Co.".

Expected behavior

Functions inside arrays in eleventyComputed should be evaluated the same as functions inside plain objects, producing concrete values in the final data cascade (behavior in 3.0.x).

Reproduction URL

https://github.com/asentris/eleventy-sample-repo/tree/broken-version

Screenshots

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions