axios.spread() cache my API whereas axios.get() does not


I’m facing a weird situation where I need to call a bunch of my CMS API routes from my server in order to use their into an object that will then be passed to my client side.

This is the code that caches my data: meaning that when I change a field on my CMS, that data that is being pulled is not updated.

The code is:

let baseUrl = ""
let locales = ["?locale=en", "?locale=fr"]

let links = [
  ].map((ele, index) => {
  return {
    en: `${baseUrl + ele + locales[0]}`,
    fr: `${baseUrl + ele + locales[1]}`,

let objectKeys = [

let getAxiosRequests = (locale) => {
  return links
  .map((ele, index) =>  {
    return axios.get(ele[locale])

.then(axios.spread((...responses) => {

  let cmsObjFr = mapToObject(objectKeys, responses);
    (...responses) => {
    let cmsObjEn = mapToObject(objectKeys, responses);
    app.get('/cms-routes', (req, res) => {
      res.json({fr: cmsObjFr, en: cmsObjEn})
  })).catch(errors => {
})).catch(errors => {

 const mapToObject = (objectKeys, responses) => { 
    return objectKeys.reduce( 
        (sum, key, index) => Object.assign(
        sum, { [key]: responses[index]}),{} 

When I access the json object, I see that the field I just changed did not update.

When I individually call that same field’s CMS route, however, the response contains the updated version of the data:

app.get("/credits", (req, res ) => {
  console.log("/credits' call");
    .then(data => res.json(

For, let’s say, the credit field, this method will give me the updated version I don’t have access when I’m using the axios.spread method.


The problem is that because you create your route handler (app.get("/cms-routes")) after retrieving data, the data it responds with is fixed and will never change.

You need to move the data retrieval logic into the route handler.

Also, as mentioned above axios.all() and axios.spread() are deprecated and should not be used.

const links = {
  info: "contact-page",
  keywords: "keywords",
  podcasts: "podcasts",
  randomQuestions: "random-question-arrays",
  featuredEntries: "featured-entries-headlines-anims",
  balladosSubtitle: "main-text",
  balladosTitles: "headline",
  cookiesSetting: "cookie-setting",
  headerInfoArray: "header-info-array",
  randomQuestionPrompt: "random-question-prompt",
  conctactPage: "contact-page",
  mapEntryRightText: "map-entry-right-text",
  mapEntryLeftText: "map-entry-left-text",
  sponsorLogos: "sponsor-logos",
  credit: "credit",
  ProjectsAboutText: "projects-about-texts",

const baseURL = "";

 * Resolves with an array of single property objects, eg
 * [
 *   {
 *     info: {...}
 *   },
 *   {
 *     keywords: {...}
 *   },
 *   ...
 * ]
const getAll = (locale) =>
    Object.entries(links).map(async ([key, link]) => ({
      [key]: (await axios.get(link, { baseURL, params: { locale } })),

app.get("/cms-routes", async (req, res) => {
  const [fr, en] = await Promise.all([getAll("fr"), getAll("en")]);

  res.json({ fr: Object.assign(, en: Object.assign(...en) });

I’ve taken the liberty of simplifying your data structures so your links and object keys are tightly coupled.

Answered By – Phil

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published