Confused handling function with headers in canary using better-auth library
Answered
Transvaal lion posted this in #help-forum
Transvaal lionOP
Hello everyone, i have some issue in using better-auth in canary version. currently, i am being told to use canary version for a project and use better-auth library for auth stuff. i am a beginner dev, i have tried to solve this issue for almost 2 days now, i think i hit a wall here.
the problem:
in better-auth library, we can check for authentication in client, server component or server action. for most of the case, i use server action to do this. here is the code:
this function is called from the dashboard page for example
it produce this error: "Route "/dashboard": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. "
based on my understanding: because it use headers, it turn it to dynamic rendering, which need suspense boundary or "use cache". i dont know how to implement this. i dont have any ideas how i suppose to give suspense to this function (because it is called inside the page, not a individual component). i cant use "use cache" because there is headers used in the function. i have been stuck thinking how to solve this, and i cant just change nextjs version nor the library becuase i am being told to use canary with use cache features and that library.
is there a miss in my understanding? and how to resolve this? i have been searching, but no luck yet, maybe there is some part that i missunderstood that make it harder for me to think for a solution
thank you in advance for your attention and help 😄
the problem:
in better-auth library, we can check for authentication in client, server component or server action. for most of the case, i use server action to do this. here is the code:
async function getIsLoggedIn(headersData){
const session = await auth.api.getSession({
headers: await headers(),
})
if(!session) {
redirect(LOGIN_PAGE)
}
return {
return session
};
}
this function is called from the dashboard page for example
it produce this error: "Route "/dashboard": A component accessed data, headers, params, searchParams, or a short-lived cache without a Suspense boundary nor a "use cache" above it. "
based on my understanding: because it use headers, it turn it to dynamic rendering, which need suspense boundary or "use cache". i dont know how to implement this. i dont have any ideas how i suppose to give suspense to this function (because it is called inside the page, not a individual component). i cant use "use cache" because there is headers used in the function. i have been stuck thinking how to solve this, and i cant just change nextjs version nor the library becuase i am being told to use canary with use cache features and that library.
is there a miss in my understanding? and how to resolve this? i have been searching, but no luck yet, maybe there is some part that i missunderstood that make it harder for me to think for a solution
thank you in advance for your attention and help 😄
Answered by Morelet’s Crocodile
If you page is checking headers use loading.tsx it wraps the page in suspense and shows a fallback page while the headers are evaluated.
7 Replies
Morelet’s Crocodile
If you page is checking headers use loading.tsx it wraps the page in suspense and shows a fallback page while the headers are evaluated.
Answer
Morelet’s Crocodile
If you are checking the headers in a server component not a page wrap it in a suspense element
Or .jsx if you are using js
@Morelet’s Crocodile If you page is checking headers use loading.tsx it wraps the page in suspense and shows a fallback page while the headers are evaluated.
Transvaal lionOP
ok, thankyou, i will try your suggestions now. will update later. but i dont quite understand what you mean by this "Or .jsx if you are using js"
i mean there is jsx and tsx if you are using typescript in your code then it would be ts and tsx
Transvaal lionOP
ah ok, thank you for the explanation.
update: it works by using loading.tsx, thank you for the help guys, i will mark solved for now.