Skip to navigation Skip to main content
Eleventy
Eleventy Documentation
Stable
2.0.1
Toggle Menu
Eleventy 1.93s
Gatsby 29.05s

Global Data from the Configuration API

Contents

In addition to Global Data Files global data can be added to the Eleventy config object using the addGlobalData method. This is especially useful for plugins.

The first value of addGlobalData is the key that will be available to your templates and the second value is the value of the value returned to the template.

Literals

module.exports = function (eleventyConfig) {
// Values can be static:
eleventyConfig.addGlobalData("myString", "myValue");
};

More Complex Paths

The first argument can be any lodash-set compatible path:

module.exports = function (eleventyConfig) {
eleventyConfig.addGlobalData("myNestedObject.myString", "myValue");
};

Functions

Importantly, passing a function to addGlobalData will evaluate that function before setting the value to the data cascade (and is async-friendly).

module.exports = function (eleventyConfig) {
eleventyConfig.addGlobalData("myDate", () => new Date());

// myDate’s value will be a Date instance
};

If you want a function returned, make sure you nest it:

module.exports = function (eleventyConfig) {
eleventyConfig.addGlobalData("myFunction", () => {
return () => new Date(),
});

// myFunction’s value will be a function that returns a Date instance
};

The above is important to know when using this API with Computed Data:

module.exports = function (eleventyConfig) {
eleventyConfig.addGlobalData("eleventyComputed.myString", () => {
return (data) => "This is a string!",
});

// myString’s value will be "This is a string!"
};

Async/Promises

module.exports = function (eleventyConfig) {
// or a promise:
eleventyConfig.addGlobalData("myFunctionPromise", () => {
return new Promise((resolve) => {
setTimeout(resolve, 100, "foo");
});
});

// or async:
eleventyConfig.addGlobalData("myAsyncFunction", async () => {
return Promise.resolve("hi");
});
};

Sources of Data

When the data is merged in the Eleventy Data Cascade, the order of priority for sources of data is (from highest priority to lowest):

  1. Computed Data
  2. Front Matter Data in a Template
  3. Template Data Files
  4. Directory Data Files (and ascending Parent Directories)
  5. Front Matter Data in Layouts (this moved in 1.0)
  6. Configuration API Global Data
  7. Global Data Files

Other pages in Data Cascade: