我的 Next.js 应用程序在“/portal/”文件夹中运行,basePath 设置如下:
module.exports = {
basePath: '/portal'
}
现在我需要该应用程序的索引页面在站点根目录中也可用(没有 basePath)。这样 '/' 和/portal/*' 都将由 Next.js 应用程序处理,而其他路由则由另一个网络应用程序处理。在 Nginx 端,根路由被传递给 Next.js 应用程序,但不清楚如何在 basePath 之外提供页面。我试过重写:
async rewrites() {
return [
{
source: '/',
destination: '/portal/',
basePath: false
},
];
},
但出现错误:路由/重写 basePath 之外的 url。请使用以“http://”或“https://”开头的目的地 https://nextjs.org/docs/messages/invalid-external-rewrite
是否可以在 basePath 之外提供一个页面,或者唯一的方法是删除 basePath 并手动更改 Next.js 应用程序中的所有路径和链接?
最佳答案
可能使用 Redirects可能对您的情况有所帮助。
module.exports = {
basePath: '/portal',
async rewrites() {
return [
{
source: '/',
destination: '/portal/',
basePath: false
},
];
},
async redirects() {
return [
{
source: '/with-basePath', // automatically becomes /portal/with-basePath
destination: '/another', // automatically becomes /portal/another
permanent: false,
},
{
// does not add /docs since basePath: false is set
source: '/without-basePath',
destination: '/another',
basePath: false,
permanent: false,
},
]
},
}
关于javascript - Next.js 应用程序在带有 basePath 的子文件夹中,如何使主页在 basePath 之外可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71146752/