最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Next.js overlaps static route with dynamic route - Stack Overflow

programmeradmin3浏览0评论

I am using Next.JS application routing system.

I have created a dynamic route with structure like pages/[country]/[language]/index.js.

Also there is a static route with structure pages/cz/cz/index.js.

Issue appears then i am on static page and trying to navigate throught Link ponent to access static route content in my case should go to home page & reload same page, instead of that dynamic route content is rendered.

In my case link is just simple navigation to home page <Link to="/"> for both routes. But maybe issue lies on how index.js file is setup for predefined & dynamic routes.

cz/cz/index.js

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

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

export { getStaticProps };

[country]/[language]/index.js

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

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

export { getStaticPaths, getStaticProps };

createRoute

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

Pages structure

So why [country]/[language]/index.js overrides cz/cz/index.js ?

So is there anything available in nextJS route to match a URL absolutely?

Or insure that going from static route should go to static route?

I am using Next.JS application routing system.

I have created a dynamic route with structure like pages/[country]/[language]/index.js.

Also there is a static route with structure pages/cz/cz/index.js.

Issue appears then i am on static page and trying to navigate throught Link ponent to access static route content in my case should go to home page & reload same page, instead of that dynamic route content is rendered.

In my case link is just simple navigation to home page <Link to="/"> for both routes. But maybe issue lies on how index.js file is setup for predefined & dynamic routes.

cz/cz/index.js

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

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

export { getStaticProps };

[country]/[language]/index.js

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

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

export { getStaticPaths, getStaticProps };

createRoute

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

Pages structure

So why [country]/[language]/index.js overrides cz/cz/index.js ?

So is there anything available in nextJS route to match a URL absolutely?

Or insure that going from static route should go to static route?

Share Improve this question edited Nov 29, 2021 at 4:53 User Paulius asked Nov 26, 2021 at 6:46 User PauliusUser Paulius 2572 gold badges4 silver badges11 bronze badges 9
  • can you share folder structure and code with link redirection ? – Fiodorov Andrei Commented Nov 26, 2021 at 13:37
  • @FiodorovAndrei added folder structure & updated description – User Paulius Commented Nov 26, 2021 at 13:43
  • Are you saying that if you try to access localhost:3000/cz/cz in the browser the app renders what's in pages/[country]/[language]/index.js? – juliomalves Commented Nov 27, 2021 at 15:11
  • @juliomalves accesing from browser it's fine, but going thought Link ponent, renders pages/[country]/[language]/index.js content, actually shoul just reload the page with localhost:3000/cz/cz content – User Paulius Commented Nov 28, 2021 at 7:58
  • Could you add the code where you're using the Link ponent? – juliomalves Commented Nov 28, 2021 at 8:01
 |  Show 4 more ments

2 Answers 2

Reset to default 10

Following next.js documentation predefined routes take precedence over dynamic routes, and dynamic routes over catch all routes. Take a look at the following examples:

  • pages/post/create.js - Will match /post/create
  • pages/post/[pid].js - Will match /post/1, /post/abc, etc. But not /post/create
  • pages/post/[...slug].js - Will match /post/1/2, /post/a/b/c, etc. But not /post/create, /post/abc

In your case you have defined predefined routes cz/cz/index.js and this route have priority

If you happen to be using redirects, note that they are processed before next ever goes to the pages.

Redirects are checked before the filesystem which includes pages and /public files.

https://nextjs/docs/api-reference/next.config.js/redirects

发布评论

评论列表(0)

  1. 暂无评论