-
Hello, I've been looking for a way to explicitly merge / concatenate the datasets / arrays returned by several Global Data files that are making API calls, without making each file write a file and then merge these files. addGlobalData seems to maybe be the ticket but the Official Documentation is just not enough to make it clear to me and I haven't found any help on google. Am I right to suspect you should be able to do it with addGlobalData? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I haven't tried global data files yet, but note from the docs that it is I imagine you'd be able to merge some stuff from global data files by requiring the files in your .eleventy.js config and then use You could probably create a global data file in the current v0.12 that makes multiple API calls in a single file and return a value without needing to write temp files to disk. Or, since you said you already have several global data files, I have this terrible code: // ./src/_data/one.js
module.exports = async () => Promise.resolve("One"); And // ./src/_data/two.js
module.exports = async () => Promise.resolve("Two"); Then I can create a third data file which loads the first two async data files, waits for them to finish doing their async things (API fetching, or whatever), then concatenate the results and return a value: // ./src/_data/three.js
const one = require("./one");
const two = require("./two");
module.exports = async () => {
// Since each of the other global data files were returning async functions
// (assuming you're using something like `axios` or `fetch` to get
// API results from a remote server, we need to use `async..await`
// here to wait for the results to finish.
// This might need to be tweaked to make them fetch simultaneously vs sequentially.
const $one = await one();
const $two = await two();
// Concatenate the two strings from our previous fake-API calls.
return $one + $two;
}; Now I can use the value of the global <p>{{ three }}</p>
<!-- output: "<p>OneTwo</p> --> |
Beta Was this translation helpful? Give feedback.
I haven't tried global data files yet, but note from the docs that it is
Coming soon in v1.0.0
(and the current stable release from npm is still v0.12.1). So information and details from Google is probably pretty rare currently, and it'd only be available if you install Eleventy directly from GitHub, or a canary release from npm.I imagine you'd be able to merge some stuff from global data files by requiring the files in your .eleventy.js config and then use
.addGlobalData()
, but I'm not sure you'd have access to stuff like collections.You could probably create a global data file in the current v0.12 that makes multiple API calls in a single file and return a value without needing to wri…