I am using Nextjs app routing.
[problem]
I have implemented an api called player-wallets using app router.
The problem is that this api is treated as a static route, which means that the result is generated at build time and returns cached data on request.
So it doesn’t reflect the user’s wallet information in real time.
// src/app/api/player-wallets/route.ts
import { NextResponse, NextRequest } from 'next/server'
export const GET = async (request: NextRequest) => {
try {
const playerWallets = await prisma.playerWallet.findMany({
where: {
walletType: WalletType.CUSTOM,
},
})
return NextResponse.json(playerWallets, {
status: 200,
})
} catch (error) {
console.error(error)
return NextResponse.json(
{ error: 'Internal Server Error' },
{ status: 500 },
)
}
}
[solution]
I changed it to a dynamic router by adding export const dynamic = 'force-dynamic'
to solve the urgent problem.
[question]
But my question is, which routes are handled as dynamic and which routes are handled as static? Is there any specific way to determin which route is static?
When I write this route, it’s hard to imagine that it will be static (not querying the db every time).
This seems like a very error-prone behavior, and I’d love to hear your thoughts on it.
[more]
I contacted the vercel team about this and received the following response.
However, this is still not resolved, so I am asking here.
“”
The determination of whether a route is dynamic or static depends on several factors:
Routes using dynamic functions like cookies(), headers(), or accessing request-specific properties are automatically made dynamic.
Routes with dynamic segments in the URL path (like [id]) may trigger dynamic behavior
The data patterns and imports in your code can influence caching decisions
Your other API routes might be using patterns that Next.js automatically detects as requiring dynamic behavior, while this particular route didn’t trigger those same conditions.
For more detailed information on this behavior, you can refer to the Next.js documentation on caching.
“”