NextJS重写不断令人耳目一新
我有两个项目,
- 项目1 - react + electrode- www.example 。
- “ rel =“ nofollow noreferrer”> www.example.com/au/sell-your-car
项目2 有一个中间件来检测请求< /strong>来自桌面/移动。
项目2 页面文件夹看起来如下:
- 页面
- _viewport
- 桌面
- au
- 卖给您的汽车
- au
- 移动
- au
- 卖给您的汽车
- au
- 桌面
- _viewport
_middleware.tsx
function getViewport(userAgent: UserAgent | null | undefined) {
if (!userAgent?.ua) return;
const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent.ua);
return isMobile ? "mobile" : "desktop";
}
/** @type {import("next/server").NextMiddleware} */
export function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
// Prevent internals from being accessed canonically
// (i.e. if you try to go to example.com/_viewport it should give a 404)
if (url.pathname.startsWith(`/_viewport`)) {
return new Response(null, { status: 404 });
}
const viewport = getViewport(req.ua);
url.pathname = `_viewport/${viewport}${url.pathname}`;
return NextResponse.rewrite(url);
}
我想以这样的方式设置重写,以便它们应在/sell-your-car 路线之后支持所有路线。
例如
除了所有路线都应重定向到项目1(react)
next.config.js
async rewrites() {
return [
{
source: "/:path*",
destination: "/:path*"
},
{
source: "/:path*",
destination: "http://localhost:3001/:path*" // Rewrite non-migrated routes to Create React App
}
];
}
带有当前配置,/sell-your-car 页面正在不断刷新。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在此线程上也解决了此问题:
简而言之,您必须进行一些更新,只需在根文件夹中运行:
npm i next@最新react@最新react-dom@最新eslint-config-next@最新〜
Also tackled this issues on this thread:
Next.JS 13 using appdir -> Page keeps refreshing
In short you have to make some updates, just run in your root folder:
npm i next@latest react@latest react-dom@latest eslint-config-next@latest~