Next.js 将静态路由与动态路由重叠

Use*_*ius 11 javascript routes reactjs next.js

我正在使用 Next.JS 应用程序路由系统。

我创建了一条结构如下的动态路线pages/[country]/[language]/index.js

还有一个带有结构的静态路由pages/cz/cz/index.js

然后出现问题,我在静态页面上,并尝试通过Link组件导航来访问静态路由内容,在我的情况下应该转到主页并重新加载同一页面,而不是呈现动态路由内容。

<Link to="/">就我而言,链接只是两个路线主页的简单导航。但问题可能在于如何为预定义和动态路由设置 index.js 文件。

捷克/捷克/index.js

export { default } from '../../../components/HomePage/';

const { getStaticProps } = createRoute({
  path: '/',
  locale: 'cs',
});

export { getStaticProps };
Run Code Online (Sandbox Code Playgroud)

[国家/地区]/[语言]/index.js

export { default } from '../../../components/HomePage/v2';

const { getStaticPaths, getStaticProps } = createRoute({
  path: '/',
  exclude: ['cs'],
  otherLocales: ['cs'],
});

export { getStaticPaths, getStaticProps };
Run Code Online (Sandbox Code Playgroud)

创建路由

export default function createRoute({
  path,
  otherLocales,
  getPathParams,
  locale,
} = {}) {
  const locales = _.without(include, ...exclude);
  return {
    getStaticPaths: createGetStaticPaths({
      locales,
      getPathParams,
    }),
    getStaticProps: createGetStaticProps({
      path,
      locale,
      locales,
      otherLocales,
    }),
  };
}
Run Code Online (Sandbox Code Playgroud)

页面结构

在此输入图像描述

那么为什么要[country]/[language]/index.js覆盖cz/cz/index.js呢?

那么 nextJS 路由中有什么可以用来绝对匹配 URL 的吗?

或者确保从静态路由转到静态路由?

Fio*_*rei 8

遵循 next.js文档,预定义路由优先于动态路由,动态路由优先于捕获所有路由。看看下面的例子:

  • pages/post/create.js- 将匹配/post/create
  • pages/post/[pid].js- 将匹配 /post/1、/post/abc 等,但不匹配 /post/create
  • pages/post/[...slug].js- 将匹配 /post/1/2、/post/a/b/c 等,但不匹配 /post/create、/post/abc

在您的情况下,您已经定义了预定义的路线cz/cz/index.js,并且该路线具有优先级

  • 这就是问题所在,因为我的预定义路由“cz/cz/index.js”不优先,而动态路由会覆盖它,反之亦然 (2认同)