I want to fetch movie data, but when the API has new data, the server does not fetch again. It seems to only fetch once.
async function fetchAllMovies()
{
try
{
const fetchOptions = {
headers: {
'Cache-Control': 'no-store, no-cache, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0',
},
};
const [newMovies, featureMovies, moviesSeries, moviesAnimation, tvShows] = await Promise.all([
fetch('https://phimapi.com/danh-sach/phim-moi-cap-nhat', fetchOptions).then(res => res.json()),
fetch('https://phimapi.com/v1/api/danh-sach/phim-le', fetchOptions).then(res => res.json()),
fetch('https://phimapi.com/v1/api/danh-sach/phim-bo', fetchOptions).then(res => res.json()),
fetch('https://phimapi.com/v1/api/danh-sach/hoat-hinh', fetchOptions).then(res => res.json()),
fetch('https://phimapi.com/v1/api/danh-sach/tv-shows', fetchOptions).then(res => res.json()),
]);
return { newMovies, featureMovies, moviesSeries, moviesAnimation, tvShows };
} catch (error)
{
console.error('Fetch lỗi:', error.message);
return {
newMovies: [],
featureMovies: [],
moviesSeries: [],
moviesAnimation: [],
tvShows: [],
};
}
}
export default async function Home()
{
const { newMovies, featureMovies, moviesSeries, moviesAnimation, tvShows } = await fetchAllMovies();
return (<>
<title>Phim247 | Phim247.vn | Xem phim mới | Phim hay | Phim chiếu rạp</title>
<meta name='description' content='Phim Mới chất lượng cao miễn phí. Xem phim hd VietSub. Phim thuyết minh chất lượng HD. Kho phim247.vn chuẩn nhanh online hay hấp dẫn.'></meta>
<link rel="canonical" href="https://phim247.vn" />
....)
I tried but it didnt work
headers: {
‘Cache-Control’: ‘no-store, no-cache, must-revalidate’,
‘Pragma’: ‘no-cache’,
‘Expires’: ‘0’,
},
Toan Minh is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
You’re fetching data with your own cache control header.
The problem is that when you use next.js, fetch api is extended by the framework by default, so that it is cached on framework level automatically.
(You mostly don’t need to set cache-header manually if you use next.js)
You should fetch like this
...
fetch('https://phimapi.com/danh-sach/phim-moi-cap-nhat',
{ cache: 'no-store' })
.then(res => res.json()),
...
https://nextjs.org/docs/app/api-reference/functions/fetch
1