Whenever I compile my Next.js 14
application using the app directory
, I encounter the following error:
[Error]: Dynamic server usage: Page couldn't be rendered statically because it used `headers`. More details can be found here: https://nextjs.org/docs/messages/dynamic-server-error
at l (/Users/joh/Documents/dev/waletoo/.next/server/chunks/185.js:30:30275)
at u (/Users/joh/Documents/dev/waletoo/.next/server/chunks/185.js:30:26794)
at s (/Users/joh/Documents/dev/waletoo/.next/server/chunks/185.js:30:20357)
at p (/Users/joh/Documents/dev/waletoo/.next/server/app/api/user/route.js:1:1271)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async /Users/joh/Documents/dev/waletoo/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:41304 {
digest: 'DYNAMIC_SERVER_USAGE'
}
An error occurred while prerendering page "/api/user". Find more information here: https://nextjs.org/docs/messages/prerender-error
Error: Internal Server Error
at p (/Users/joh/Documents/dev/waletoo/.next/server/app/api/user/route.js:1:1442)
This is how I have defined my api/user/route
route :
import { getServerSession } from "next-auth";
import connectDB from "@/src/mongoDB/connect";
import UserModel from "@/src/mongoDB/userSchema";
import { authOptions } from "@/src/utils/authOptions";
export async function GET() {
await connectDB();
try {
const session = await getServerSession(authOptions);
if (!session?.user?.email) throw new Error("Unauthorized");
const userEmail = session.user.email;
const userInformations = await UserModel.find({ email: userEmail });
return new Response(JSON.stringify(userInformations));
} catch (error) {
console.error(error);
throw new Error("Internal Server Error");
}
}
Can you identify any issues in this setup?