我在Next.js应用程序中有一个服务器渲染的页面,我想检查一个条件并重定向到另一个页面。请参阅下面的代码以更好地理解:
// This is a server-rendered page
export default async function Packs() {
const cookiesStore = cookies()
const token = cookiesStore.get('token')?.value
const gameInfo = await getGameInfo()
const user = await getUserContextFromToken(token || "") as UserContext
const theme = getThemeForLevel(user?.level || 1)
if (isEmpty(user)) {
// Instead of sending the home page we should be able to redirect
return <HomePage sessionExpired={true} user={null} theme={theme} totalLevels={gameInfo.totalLevels} />
// return NextResponse.redirect(AppRoutes.home, 302); // (is something like this possible)
}
return (
<PacksPage theme={theme} currentLevel={user.level} packSize={gameInfo.packSize} totalLevels={gameInfo.totalLevels} />
);
}
字符串
1条答案
按热度按时间mftmpeh81#
您可以使用
next/navigation
中的redirect
函数,例如:字符串