How can I invoke cookies() in a server action
Unanswered
American black bear posted this in #help-forum
American black bearOP
Version: Next 14.1.3 App router
base code:
export const fetchDataForMyCourses = async ()=> {
const token = cookies().get('token')?.value;
console.log(token);
return serverFetchPostJson<MyCoursesResponseType>(
forInnerFullUrl(Urls.MY_COURSES), {}, "");
}
the built error is below:
n [Error]: Dynamic server usage: Page couldn't be rendered statically because it used
at u (/project/.next/server/chunks/881.js:3:7500)
at Object.c (/project/.next/server/chunks/881.js:3:2355)
at v (/project/.next/server/chunks/434.js:1:7641)
at l (/project/.next/server/app/my/courses/page.js:1:5240)
at em (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:131226)
at /project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142926
at Array.toJSON (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:146504)
at stringify (<anonymous>)
at eR (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:134889)
at eP (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135332) {
description: "Page couldn't be rendered statically because it used
digest: 'DYNAMIC_SERVER_USAGE'
}
I have been read the documents which refered in the error info, however, I can not have a solution, Could anyone help me? Thanks.
base code:
export const fetchDataForMyCourses = async ()=> {
const token = cookies().get('token')?.value;
console.log(token);
return serverFetchPostJson<MyCoursesResponseType>(
forInnerFullUrl(Urls.MY_COURSES), {}, "");
}
the built error is below:
n [Error]: Dynamic server usage: Page couldn't be rendered statically because it used
cookies. See more info here: https://nextjs.org/docs/messages/dynamic-server-errorat u (/project/.next/server/chunks/881.js:3:7500)
at Object.c (/project/.next/server/chunks/881.js:3:2355)
at v (/project/.next/server/chunks/434.js:1:7641)
at l (/project/.next/server/app/my/courses/page.js:1:5240)
at em (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:131226)
at /project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:142926
at Array.toJSON (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:146504)
at stringify (<anonymous>)
at eR (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:134889)
at eP (/project/node_modules/next/dist/compiled/next-server/app-page.runtime.prod.js:12:135332) {
description: "Page couldn't be rendered statically because it used
cookies. See more info here: https://nextjs.org/docs/messages/dynamic-server-error",digest: 'DYNAMIC_SERVER_USAGE'
}
I have been read the documents which refered in the error info, however, I can not have a solution, Could anyone help me? Thanks.