Preserving the Order of Content Requests

Last updated: May 9, 2024

When requesting multiple content entries via the Storefront-SDK, you might assume that the order of the content will match the order in which it was requested. However, this is not always the case, as The Storefront-SDK does not preserve the order. 

This means if you request handlesA, B, andC, you're expecting to get an array withA first, thenB, and finallyC. While some responses may preserve the order, there is no guarantee that all responses will follow the same order. In these instances, we've created the following code-snippet that will preserve the order of your requests: 

const pdpHandles = ["header-settings", "footer-settings"];

const content = await client
  .content({ handles: pdpHandles })
  .then((pdps) =>
    pdps.sort(
      (a, b) => pdpHandles.indexOf(a.handle) - pdpHandles.indexOf(b.handle)
    )
  );