是否有人知道如何将Next.js中的任何特定页面定义为根(主页)
我一直在网上寻找,没有找到任何具体的解决办法。这应该是一个非常常见的用例。
还有另一个帖子,Using Next.js next-routes, how to define a home route for the base web page?,但是它建议为它创建一个服务器。
有什么“NextJ”设置主页的方法吗?
有什么想法吗?
提前感谢!
发布于 2020-08-06 07:04:05
我在另一条帖子中发布了解决我问题的答案:
Next.js Redirect from / to another page
这个例子取自https://dev.to/justincy/client-side-and-server-side-redirection-in-next-js-3ile
我需要立即将访问我的根页面(mywebsite.com/)的用户转发到一个子页面,在本例中是: mywebsite.com/home。
在我的主index.js文件中粘贴以下任何一个,都会达到预期的结果:
有复制粘贴级别的示例。
客户端
import { useRouter } from 'next/router'
function RedirectPage() {
const router = useRouter()
// Make sure we're in the browser
if (typeof window !== 'undefined') {
router.push('/home')
}
}
export default RedirectPage
服务器端
import { useRouter } from 'next/router'
function RedirectPage({ ctx }) {
const router = useRouter()
// Make sure we're in the browser
if (typeof window !== 'undefined') {
router.push('/home');
return;
}
}
RedirectPage.getInitialProps = ctx => {
// We check for ctx.res to make sure we're on the server.
if (ctx.res) {
ctx.res.writeHead(302, { Location: '/home' });
ctx.res.end();
}
return { };
}
export default RedirectPage
https://stackoverflow.com/questions/63082929
复制相似问题