Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
Variable data is not persisted on client-side when using async fetch in Nuxt and data doesn't persist

I'm using the async fetch hook to render the component in SSR and making the API call inside the same. But on the live server, it is actually loading one more time on the client-side and making one more call to API and as API isn't exposed during client-side so data just washes away and the array object gets again set to empty.

 data() {
    return {
      errored: false,
      randomData: [],
    };
  },
 async fetch() {
    await this.$axios
      .get(this.routeUrl)
      .then((res) => {
        if (res.data.length == 0 || res.status != 200) this.errored = true;
        this.randomData = res.data;
      })
      .catch((err) => {
        console.log(err);
      });
  },
  fetchOnServer: true,

I want to persist this randomData variable, so there shouldn't be another call on the client-side to populate the same.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

If you want to pass some data from the server-side into the client-side, you could use the nuxtServerInit Vuex action

/store/index.js

actions: {
  nuxtServerInit ({ commit }, { req }) {
    if (req.session.user) {
      commit('user', req.session.user)
    }
  }
}
about 4 years ago · Juan Pablo Isaza Relatório

0

So, the issue was that, as I was Docker and the API was only exposed to the server-side and not the client-side. For a blink, the content was available and then just all gone. Because the async fetch and asyncData hooks are called on the server and as well as on the client-side. I solved it using the Vuex store. Now I started storing the data fetched on the server in store and using the same on the client-side. Here is how I implemented that:

// store/index.js

export const state = () => ({
  randomData: [],
});
export const mutations = {
  SET_RANDOMDATA: (state, payload) => {
    state.randomData = payload;
  },
};
// Inside the component

// Fetch the data from API on server-side
async fetch() {
    if (process.server) {
      await this.$axios
        .get(this.routeUrl)
        .then((res) => {
          this.$store.commit("SET_RANDOMDATA", res.data);
        })
        .catch((err) => {
          console.log(err);
        });
    }
  },
computed: {
    ...mapState(["randomData"]),
},
fetchOnServer: true,
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda