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

eleventyComputed added via config does not follow deep data merge #3389

Closed
LeaVerou opened this issue Jul 29, 2024 · 6 comments
Closed

eleventyComputed added via config does not follow deep data merge #3389

LeaVerou opened this issue Jul 29, 2024 · 6 comments
Labels
feature: 🧮 computed data Related to Eleventy’s Computed Data feature feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🏔 data cascade Issues related to the Data Cascade feature

Comments

@LeaVerou
Copy link

(Thanks @DmitrySharabin for debugging this issue!)

I was using two plugins, both of which were doing something like this.

Plugin 1:

config.addGlobalData("eleventyComputed", {
	foo (data) {
		// stuff
	}
});

Plugin 2:

config.addGlobalData("eleventyComputed", {
	bar (data) {
		// stuff
	}
});

I expected that deep data merge should take care of merging the objects, but it turned out that each plugin was overriding the previous one.

a) This shouldn't happen and
b) Is there a better way for plugins to add global computed data?

@zachleat zachleat added feature: 🧮 computed data Related to Eleventy’s Computed Data feature feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🏔 data cascade Issues related to the Data Cascade feature labels Jul 30, 2024
@zachleat
Copy link
Member

I’ll update the docs but addGlobalData does take a lodash set selector.

The awkward part of addGlobalData is that when you pass in a function we’ll run it so when you want to return a function as part of eleventyComputed you need to nest it, like so:

config.addGlobalData("eleventyComputed.foo", () => {
	return (data) => { /* stuff */ };
});
config.addGlobalData("eleventyComputed.bar", () => {
	return (data) => { /* stuff */ };
});

I would like to improve that for eleventyComputed specifically but I don’t see a clear path forward yet that won’t break stuff.

The problem is this section right here:

if (typeof returnValue === "function") {
returnValue = await returnValue();
}

@zachleat
Copy link
Member

Oh, I forgot to say to your original point: I agree that we should merge objects passed to addGlobalData so I’ll work on that change too.

zachleat added a commit to 11ty/11ty-website that referenced this issue Jul 30, 2024
@zachleat
Copy link
Member

Merging of objects passed to addGlobalData will ship with 3.0.0-alpha.18

@zachleat zachleat added this to the Eleventy 3.0.0 milestone Jul 30, 2024
@zachleat
Copy link
Member

One more note here is that this will happen independent of whether or not deep data merge is enabled. We’re trying to phase out/discourage use of the dataDeepMerge feature.

@zachleat
Copy link
Member

Docs are updated: https://www.11ty.dev/docs/data-global-custom/

@LeaVerou
Copy link
Author

Wow, that was fast! Color me impressed!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature: 🧮 computed data Related to Eleventy’s Computed Data feature feature: 🛠 configuration Related to Eleventy’s Configuration file feature: 🏔 data cascade Issues related to the Data Cascade feature
Projects
None yet
Development

No branches or pull requests

2 participants