是否有人知道如何将Next.js中的任何特定页面定义为根(主页)
我一直在网上寻找,没有找到任何具体的解决办法。这应该是一个非常常见的用例。
还有另一个帖子,Using Next.js next-routes, how to define a home route for the base web page?,但是它建议为它创建一个服务器。
有什么“NextJ”设置主页的方法吗?
有什么想法吗?
提前感谢!
发布于 2021-02-11 23:16:09
下面是nextjs提供的解决方案。
在根目录中创建或编辑文件next.config.js:
module.exports = {
async redirects() {
return [
{
source: '/',
destination: '/about',
permanent: true,
},
]
},
}
来源:https://nextjs.org/docs/api-reference/next.config.js/redirects
发布于 2020-07-25 00:43:55
“当一个文件被添加到页面目录中时,它会自动作为路由使用。
页面目录中的文件可以用来定义最常见的模式。“
路由器将自动将名为索引的文件路由到目录的根目录。
pages/index.js→/
发布于 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 RedirectPagehttps://stackoverflow.com/questions/63082929
复制相似问题