I have to make some changes in my NextJS project because my endpoint API doesn't support many calls and I would like to make a refresh from the original data every 3 min.
I implemented API from NextJS: I created a pages/api/data and inside, I make the call to my endpoint, and in my getInitialProps inside index call to data file.
The get works okay, but I have 2 problems:
1: I have an alert message that says:
API resolved without sending a response for /api/data, this may result in stalled requests.
2: It doesn't reload data after 3 minutes... I suppose it is because of the Cache-Control value.
This is my code:
pages/api/data
import { getData } from "../../helper";
export default async function(req, res) {
getData()
.then(response => {
res.statusCode = 200
res.setHeader('Content-Type', 'application/json');
res.setHeader('Cache-Control', 'max-age=180000');
res.end(JSON.stringify(response))
})
.catch(error => {
res.json(error);
next();
});
};
pages/index
import React, { useState, useEffect } from "react";
import fetch from 'isomorphic-unfetch'
const Index = props => {
return (
<>Hello World</>
);
};
Index.getInitialProps = async ({ res }) => {
const response = await fetch('http://localhost:3000/api/data')
const users = await response.json()
return { users }
};
export default Index;
returnstatement beforegetData(), e.g.return getData().then(...).catch(...)