1

I am trying to pass data from a php array to my created Gutenberg block. I need this data to populate a select inspector control. Right now im using the wp rest api and fetching the data inside the array. However this method is very slow and I am wondering if there is a faster approach to getting this data. I only need the data when i create the inspector controls for the block.

let options = [];
    options.push({ value: -1, label: 'Select a template' });

    wp.apiFetch({
        path: 'adf/v1/blocks/hero-block',
    }).then(data => {
        var templates = JSON.parse(data);

        templates.forEach((template) => {
            options.push({ value: template, label: template });
        });
    });
4
  • 1
    There isn't really a faster way to do this. Since it's regarding an HTTP request, it all depends on your CPU speed, Internet Connection, and server response time. Either of those could affect the performance and can be different for every user. If it's slow for everyone, then it's probably a server issue and you should investigate what's taking the server so long. Commented Jul 10 at 10:43
  • Have you checked carefully what's actually causing the bottleneck? Is it this bit of code alone which is slow? Or is it the API call which takes the time? Have you looked at timing each bit separately? It's not very clear from your question what analysis you've done.
    – ADyson
    Commented Jul 10 at 11:28
  • Ok thanks. I wasn't 100% sure if this is the common way to do this. Commented Jul 10 at 14:08
  • It doesn't matter if it's "common" (however you are defining that). The important point is that it works properly and efficiently to meet your requirements. If it doesn't, you need to analyse carefully where the problem is originating from
    – ADyson
    Commented Jul 10 at 14:19

0

Browse other questions tagged or ask your own question.