Next.js Discord

Discord Forum

Dynamic server usage: Page couldn't be rendered statically because it use

Unanswered
Entlebucher Mountain Dog posted this in #help-forum
Open in Discord
Entlebucher Mountain DogOP
when i'm trying to build my nextjs app i'm getting an error
Error: DynamicServerError: Dynamic server usage: Page couldn't be rendered statically because it used cookies.

import { AuthData } from "$types/auth";
import { RequestCookie } from "next/dist/compiled/@edge-runtime/cookies";
import { cookies } from "next/headers";

type GetTokenResult = {
access: string;
refresh: string;
};

export default async function get_token(): Promise<GetTokenResult | null> {
const requestData: RequestCookie | undefined = cookies().get("authData");
const token: AuthData = requestData
? JSON.parse(requestData?.value as string)
: null;

return {
access: token?.access,
refresh: token?.refresh,
};
}

const fetcher = async <T>(
url: string,
options?: RequestInit
): Promise<T | null | undefined> => {
try {
const token = await get_token();

const headers = {
Authorization: Bearer ${token?.access},
...(options?.headers || {}),
};

const newOptions: RequestInit = {
...options,
headers,
};

if (url.endsWith("/refresh/") && newOptions?.headers) {
const headers = newOptions.headers as Record<string, string>;
delete headers.Authorization;
}
const api_url = config.backend_url + url;
const res = await fetch(api_url, newOptions);

if (res.status === 403) {
hanldeSignOut();
}

if (!res.ok) return null;
const data = await res.json();
return data;
} catch (error) {
console.error("Error:", error);
}
};


what's worng with my code ? can you guys help me out

0 Replies