NextJS重写不断令人耳目一新

发布于 2025-01-29 21:12:22 字数 2401 浏览 3 评论 0 原文

我有两个项目,

  • 项目1 - react + electrode- www.example 。
  • ​“ rel =“ nofollow noreferrer”> www.example.com/au/sell-your-car

项目2 有一个中间件来检测请求< /strong>来自桌面/移动

项目2 页面文件夹看起来如下:

  • 页面
    • _viewport
      • 桌面
        • au
          • 卖给您的汽车
      • 移动
        • au
          • 卖给您的汽车

_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 页面正在不断刷新。

I've two projects,

Project 2 has a middleware to detect whether the request is coming from desktop/mobile.

The Project 2 page folder looks like the following:

  • pages
    • _viewport
      • desktop
        • au
          • sell-your-car
      • mobile
        • au
          • sell-your-car

_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);
}

I want to set up rewrites in such a way that they should support all the routes after the /sell-your-car route.

e.g.

Other than that all the routes should redirect to the Project 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
      }
    ];
  }

With the current config, the /sell-your-car page is getting refreshed continuously.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不必你懂 2025-02-05 21:12:22

在此线程上也解决了此问题:

简而言之,您必须进行一些更新,只需在根文件夹中运行:
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~

  • All the best!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文