admin管理员组文章数量:1397110
In this example, am I using NextJS's (14.2.24) server-side extension to WebAPI's fetch or the standard WebAPI fetch, specifically relating to the use of the cache property? Am I referring to NextJS's cache or the browser's http cache?
// actions.ts
"use_server"
export const callApi = async (
endpoint: string,
method: Method = Method.GET,
body?: any
) => {
const url = `${process.env.API_BASE_URL}${process.env.API_LIB}${endpoint}`;
let token = cookies().get("session")?.value;
let response = await fetch(url, {
method: method,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: body && JSON.stringify(body),
cache: 'no-store'
})
// ... continues
}
export async function addEmployeesToGroup(ids: string[]) {
const add = await api(`/add-employees`, Method.POST, {
employees: ids,
});
// ... continues
return add;
}
// client component (extract)
export default function AddEmployees() {
const add = async () => {
const employeeIds = ["LQqihAWU6Y", "4mhbHp7ps0"]; // for example
const response = await addEmployeesToGroup(employeeIds);
// ... continues
};
return <button onClick={add}>Add employees</button>;
}
In this example, am I using NextJS's (14.2.24) server-side extension to WebAPI's fetch or the standard WebAPI fetch, specifically relating to the use of the cache property? Am I referring to NextJS's cache or the browser's http cache?
// actions.ts
"use_server"
export const callApi = async (
endpoint: string,
method: Method = Method.GET,
body?: any
) => {
const url = `${process.env.API_BASE_URL}${process.env.API_LIB}${endpoint}`;
let token = cookies().get("session")?.value;
let response = await fetch(url, {
method: method,
headers: {
Accept: "application/json",
"Content-Type": "application/json",
Authorization: `Bearer ${token}`,
},
body: body && JSON.stringify(body),
cache: 'no-store'
})
// ... continues
}
export async function addEmployeesToGroup(ids: string[]) {
const add = await api(`/add-employees`, Method.POST, {
employees: ids,
});
// ... continues
return add;
}
// client component (extract)
export default function AddEmployees() {
const add = async () => {
const employeeIds = ["LQqihAWU6Y", "4mhbHp7ps0"]; // for example
const response = await addEmployeesToGroup(employeeIds);
// ... continues
};
return <button onClick={add}>Add employees</button>;
}
Share
Improve this question
edited Mar 28 at 8:34
Jim
asked Mar 26 at 11:44
JimJim
3015 silver badges12 bronze badges
1 Answer
Reset to default 2In Next.js, the server-side fetch API extends the standard Web API fetch with additional optimizations, including automatic request caching, deduplication, and revalidation. Since your callApi
function is inside "use server", it is running on the server, and it will be using Next.js's enhanced fetch.
When you specify cache: 'no-store'
in this context, you're referring to Next.js's Data Cache, not the browser's HTTP cache. This setting tells Next.js to fetch the resource from the remote server on every request, bypassing the Next.js Data Cache completely, even if Dynamic APIs are not detected on the route.
In a server-side fetch within Next.js, the cache
option indicates how a server-side fetch request will interact with the framework's persistent Data Cache, rather than the browser's HTTP cache.
本文标签:
版权声明:本文标题:javascript - In this example, am I using NextJS's extension to WebAPI's fetch or the standard WebAPI fetch? - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744147620a2592911.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论