【发布时间】:2021-04-18 18:23:28
【问题描述】:
如果我没有找到代码 query 参数,我想将用户重定向到错误页面
import { useEffect } from 'react';
import { useRouter } from 'next/router';
export default function Home(props){
const router = useRouter();
useEffect(() => {
if(!props.code){
console.log('No code found');
router.push('/error');
}
});
// call an api passing the code
return (
<>
<div>Something</div>
</>
)
}
export async function getServerSideProps(context) {
context.res.setHeader('Cache-Control', 'No-Cache');
return {
props: { code: context.query.code || null }
}
}
如果用户使用 URL localhost:3000/ 访问此页面,则应将其重定向到 localhost:3000/error,但如果用户带有特定查询参数 localhost:3000/?code=1234,则不得重定向用户。
问题是当我测试这个组件去没有code参数的URL时,我正确地重定向到错误页面(localhost:3000/error),但是在这之后,如果我输入urllocalhost:3000/?code=1234,我也重定向到错误页面。
当我在匿名窗口中尝试此操作时,我在转到 localhost:3000/ 后转到 localhost:3000/?code=1234 时被正确重定向(然后转到错误页面)
我怎样才能阻止这种行为?
【问题讨论】:
标签: next.js