Next.js Discord

Discord Forum

rewrites does not seems to show the right page

Unanswered
Devon Rex posted this in #help-forum
Open in Discord
Devon RexOP
Hello,

I am trying to generate that kind of url following this example https://www.reddit.com/r/nextjs/comments/1fjnlbc/comment/lnpe8zv/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1&utm_content=share_button

e.g.:

the url /blog/how-to-do-:A-with:B while :a and :b are dynamic parameters to /blog/:a/:b to ease the management of those generation.

I've tried to implement this within rewrites and middleware way of doing it following https://nextjs.org/docs/app/api-reference/next-config-js/rewrites

However, it does not render the right page even if handled by middleware or rewrites as per the debug mode.

In the following example (easier than the above for testing purpose), I try to redirect /babbabbabbbbab to /learn but still no success even if I can see the /learn into the parsedUrl debug but matchedOutput is the / page.

rewrites
  async rewrites() {
    return {
      beforeFiles: [
        {
          source: '/:lng?/babbabbabbbbab',
          destination: '/learn',
        },
      ]
    };
  },


Debug:
  next:router-server:main requestHandler! /babbabbabbbbab {
  matchedOutput: {
    type: 'appFile',
    fsPath: undefined,
    locale: undefined,
    itemsRoot: undefined,
    itemPath: '/[lng]'
  },
  statusCode: undefined,
  resHeaders: {
    'x-next-i18n-router-locale': 'en',
    'x-middleware-rewrite': '/en/babbabbabbbbab'
  },
  bodyStream: false,
  parsedUrl: {
    pathname: '/learn',
    query: [Object: null prototype] {
      __nextDefaultLocale: undefined,
      lng: 'en'
    }
  },
  finished: true
}



PS: I am using next-i18n-router as well

Any ideas?

5 Replies

does it work without beforeFiles?

async rewrites() {
return [
{
source: "/:lng?/babbabbabbbbab",
destination: "/",
},
];
},
Devon RexOP
I have exactly the same behavior without the beforeFiles 😦
it is always rendering the / instead of /learn
btw, I have as directory structure the following
/app
 /[lng]
   layout.tsx
   /(infos)
     layout.tsx
     /learn
       page.tsx
   (main)
     page.tsx
     layout.tsx