Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Next.js中处理带有可选slug的动态页面的大量重定向?_Next.js_Vercel - Fatal编程技术网

如何在Next.js中处理带有可选slug的动态页面的大量重定向?

如何在Next.js中处理带有可选slug的动态页面的大量重定向?,next.js,vercel,Next.js,Vercel,我的网站有一些动态路线,可以产生数千个水合页面。这些页面遵循如下url结构:/artists/[id]/[slug]或/concerts/[year]/[month]/[day]/[slug]。目前,我的页面结构模仿了这些路线:/pages/artists/[id]/[slug].tsx和/pages/concerts/[year]/[month]/[day]/[slug].tsx 首选带有slug的路由,但是当slug不可用时,站点应该优雅地重定向。例如,我希望将前往/artist/id的任何

我的网站有一些动态路线,可以产生数千个水合页面。这些页面遵循如下url结构:
/artists/[id]/[slug]
/concerts/[year]/[month]/[day]/[slug]
。目前,我的页面结构模仿了这些路线:
/pages/artists/[id]/[slug].tsx
/pages/concerts/[year]/[month]/[day]/[slug].tsx

首选带有slug的路由,但是当slug不可用时,站点应该优雅地重定向。例如,我希望将前往
/artist/id
的任何请求重定向到
/artist/id/slug
。同样,我想为音乐会的各个部分做一些重定向

我最初尝试在next.config.js中指定重定向,如:

module.exports={
...
异步重定向(){
const[allArtists,allConcerts]=等待承诺;
返回[
…allArtists.map((艺术家)=>{
返回{
来源:`/artists/${artist.id}`,
目标:`/artists/${artist.id}/${slugify(artist.name)}`,
永久性:假,
};
}),
…所有音乐会.map((音乐会)=>{
返回{
资料来源:`/concerts/${concert.year}`,
目的地:`/tours/${concert.year}`,
永久性:假,
};
}),
…所有音乐会.map((音乐会)=>{
返回{
资料来源:`/concerts/${concert.year}/${concert.month}`,
目的地:`/tours/${concert.year}`,
永久性:假,
};
}),
…所有音乐会.map((音乐会)=>{
返回{
资料来源:`/concerts/${concert.year}/${concert.month}/${concert.day}`,
目的地:`/concerts/${concert.year}/${concert.month}/${concert.day}/${slugify(concert.name)}`,
永久性:假,
};
}),
],
},
};
但是,截至今天,vercel/next.js不支持该文件中指定的1000多个重定向

我试图从
getStaticProps
返回
redirect:pathWithSplug
,但出现以下错误:

Error: `redirect` can not be returned from getStaticProps during prerendering
关于我如何完成这种行为,有什么想法或方法吗

vercel/next.js不支持该文件中指定的1000多个重定向

你没有,这是在进入站台之前要考虑的重要事情。一些想法和妥协:

  • 对于
    页面/artists/[id]

  • 页面/artists/[id]
    使用与
    页面/artists/[id]/[slug]
    相同的内容,但设置规范url以避免搜索引擎优化惩罚


首先,您可以通过使用路径匹配,将
/concerts/*
重定向的条目数量显著减少到
/tours/*
重定向

async重定向(){
返回[
// ...
{
资料来源:“/concerts/:year/:month”,
目的地:'/tours/:year',
永久性:假
},
{
资料来源:“/concerts/:year”,
目的地:'/tours/:year',
永久性:假
},
// ...
]
}
不幸的是,对于其他路径也不能这样做。仅此一项就有可能将参赛人数减少到1000人以下——这取决于你拥有的艺术家/音乐会的数量——但迟早会再次成为一个问题

其次,为了解决剩余路径,我将把您的
/artists/[id]/[slug]
/concerts/[year]/[month]/[day]/[slug]
路线转换为动态:

  • /artists/[…slug]
  • /concerts/[…slug]
这将允许
/concerts/[…slug].tsx
页面同时匹配
/concerts/2020/08/20
/concerts/2020/08/20/concert name
URL,这意味着您根本不需要重定向这些页面。这同样适用于
artists/*
路线


正如@DerekNguyen在中提到的,拥有一个规范的URL,将用于两种搜索引擎优化目的将是理想的。

谢谢-我希望找到一种方法,而不是一种包罗万象的方法。“一网打尽”的页面有点违背了路由基础设施的目的,我不确定它是否提供了比只返回元刷新和规范链接标签的
pages/artists/[id]/index.tsx更多的内容。