Fetch multiple taxonomy with containing posts at once

I’m trying to write a new handler.
I have a Custom Taxonomy for Posts called Folder.
I want to fetch all the posts with one fetch in multiple folders.
I have the ID’s of the folders, like: const folderIds = '7,10,66,123'. So I want to be able to make a fetch like:

actions.source.fetch("/folders/" + folderIds);

And then get info about the Folders (an ACF field), and get all the posts in a Folder.
I created a handler, and I could get all the posts in all the folders, and get the ACF field of the folders, but I could not get the posts in a specific folder.

My handler looks like this, at the moment:

const FoldersHandler = {
  pattern: "/folders/:ids",
  priority: 1,
  func: async ({ route, params, state, libraries }) => {
    const { ids } = params;
   
    //this will create a request like: https://wp.szinhaz.online/wp-json/wp/v2/posts?_embed=true&folder=969,23,22,1035,25,26,27,24,371&per_page=100
    const postResponse = await libraries.source.api.get({
      endpoint: "posts",
      params: {
        folder: ids,
        per_page: 100,
        _embed: true
      }
    });
    const posts = await libraries.source.populate({state, response: postResponse});
    //if I console.log(posts), I see that a post contains only id, link, and type
    posts.map(post =>{
      Object.assign(state.source.data[post.link],{
        id: post.id,
        type: post.type
      });
    });

    //this will create a request like: https://wp.szinhaz.online/wp-json/wp/v2/folder?head_tags=false&include=969,23,22,1035,25,26,27,24,371&per_page=100
    const folderResponse = await libraries.source.api.get({
      endpoint: "folder",
      params: {
        include: ids,
        per_page: 100,
        head_tags: false
      }
    });
    const folders = await libraries.source.populate({ state, response: folderResponse });
    //if I console.log(folders), I see that a folder has an id, link and type: undefined
    folders.map(folder => {
      Object.assign(state.source.data[folder.link],{
        id: folder.id,
        taxonomy: "folder",
        isArchive: true,
        isTaxonomy: true,
        isFolder: true
      });
    });
  }
};

After this, in frontity.state.source.folder I see all the folders I need, but they don’t have an items field, so I don’t know, what posts are in this folder.
In frontity.state.source.post I see all the posts I should, and they have a folder property, with an array of id’s of the folders.
In frontity.state.source.data I see the posts and the folders, and by both of them isFetching and isReady are false.

How should I assign the posts to a folder?

Hi @koli14

I’m struggling to understand what you’re trying to do. Can you show us some samples of the data you have in the state that will help us understand the issue you’re having.

Thanks.

Hey @mburridge,
The hole started here: Npx frontity dev - Aborted (core dumped) - JavaScript heap out of memory. As @SantosGuillamot suggested, I want to reduce the number of fetches my website makes. Here is my live site: https://szinhaz.online/ I circled with red in this picture the folders (below each folder you can see the posts in them) :


At the moment my code looks like:

....
{folders.map(({ term_id, slug, taxonomy, name }) => {
   React.useEffect(() => {
      actions.source.fetch("/" + taxonomy + "/" + slug);
    }, []);

    const data = state.source.get("/" + taxonomy + "/" + slug);
    ....

But like this, my code make 2*8 fetch to my WordPress site (I have 8 folder, and frontity get the folder details with one fetch, and the posts in it with an other fetch).
I created a fresh clone from mars theme, and try to achieve my goal there.
Here is the git repo: mars-tmp
And here is the live site at Zeit: tmp.koli14.now.sh
There you can check all the data in state from the dev-tools console, with frontinty.state .

Hi @koli14

Can a post be in more than one folder, or is each post in only one folder? Can you show me what comes back in the JSON for a single post so I can see what is in the folder property. Also it might be useful to see what you get in the JSON for a folder too.

Hi @koli14

You could loop through the array of folders in frontity.state.source.folder and for each folder get the posts in frontity.state.source.post where post.folder[0] == folder.id.

That said, I’ve checked with one of the developers on the team and they seem to think that what you’re already doing, i.e. making a separate call for each folder, is the best approach as that way WordPress is doing all the hard work for you already.