getInitialProps does not work when the page reload

Viewed 1389

I'm using getInitialProps in the _app.js component, but when I reload the page, the request does not execute.

For example:

// getData.js
import * as axios from 'axios';
export default async function getData() {
  const response = await axios.get('http://someapi.com/');

  return response.data;
}

And then I'm going to use that data...

// _app.js
import getData from './getData';
import App, { Container } from "next/app";

class MyApp extends App {
  static async getInitialProps() {
    const response = await getData();

    if (response) {
      return { response };
    }
    return {};
  }
  render() {
    const { Component, response } = this.props;
    <Container>
      <Component {...this.props} data={response} />
    </Container>
  }
}

The first time, it works perfectly, but when I reload the page the getInitialProps() function does not executes :(

How can I resolve this?

Thank you.

0 Answers
Related