I'm currently working on Next14project.
And made FetchData method like this.
export async function fetchData({ method, url, data, cache }: FetchDataProps) {
const fullURL = process.env.BASE_API_URL + url;
let options: {
method: string;
mode: RequestMode;
credentials: RequestCredentials;
headers: {
"Content-Type": string;
Authorization?: string;
};
body?: any;
} = {
method,
mode: "cors",
credentials: "same-origin",
headers: {
"Content-Type": "application/json",
Authorization: Bearer ${localStorage.getItem("token")},
},
};
if (method === FetchMethods.POST) {
options = {
...options,
body: JSON.stringify(data),
};
}
try {
const response = await fetch(fullURL, options);
console.log(response);
} catch (err) {
console.log(err);
}
}
But I'm not sure in where I have to call this method.
I guess next 14 is a little different from Next 13.
But the GPT says I have to do it on getStaticProps or getServersideProps.
GPT is right or not? If not, please let me know the right way.