【发布时间】:2021-03-17 09:59:56
【问题描述】:
我对 Next.js 和所有这些 SSR 东西还是很陌生,所以我需要一个建议。 这是我的情况:
我在 auth 后面有一个页面,我正在尝试做一些预取:
export const getServerSideProps: GetServerSideProps<SomeProps> = async ({ req, res }) => {
try {
const data = await fetchOnServerSide<Data[]>(someAPI, req, {
method: 'GET',
});
return {
props: data, // will be passed to the page component as props
};
} catch (error) {
// Oops, you aren't authed for doing it or your token expired -> go to the "login page"
if (res) {
res.writeHead(302, { Location: '/login' });
res.end();
}
return { props: {} };
}
};
它有效,但我想在成功验证后将我的用户从登录页面重定向回来。 在这种情况下如何获取上一页 URL?
【问题讨论】:
-
到目前为止您尝试过什么?您的登录页面是什么样的?
-
我试过 Router.back() 但它只适用于客户端重定向。现在我只需在主页上使用 router.push('/'); 重定向用户;
标签: javascript redirect next.js