3

I'm facing an issue while using rewrites and query parameters. I'll describe it further below. if I've lost you at any point during the explanation feel free to ask for a clarification.


Expected result

What I'm trying to achieve is localizing a next/link and passing a query paramter to fill in the dynamic part of the url.


The setup

I have my setup split into a few files, a pathnames.js which holds all the paths and translations. (Won't show the whole file just the neccesary parts)

// The predefined consts for the pathnames
export const pathnames = {
  STOREFINDER: 'stores',
  STOREFINDER_DETAIL: '/stores/[id]',
};

// The localized part of the pathnames
export const localizedPathnames = {
  // This works
  [pathnames.STOREFINDER]: {
    nl: '/winkels',
    fr: '/magasins',
  },
  // This works half (further explanation below)
  [pathnames.STOREFINDER_DETAIL]: {
    nl: '/winkels/[id]',
    fr: '/magasins/[id]',
  },
};

And then the next.config.js where I define the rewrites. (Won't show the whole file just the neccesary parts)

async rewrites() {
  return [{
      source: '/winkels',
      destination: '/stores',
   },
   {
      source: '/magasins',
      destination: '/stores',
   },
   {
      source: '/winkels/:id',
      destination: '/stores/:id',
   },
   {
      source: '/magasins/:id',
      destination: '/stores/:id',
   },  
]}

This is how I make a localized link


<Link
    href={getLocalizedRoute(pathnames.STOREFINDER_DETAIL, locale, {
        query: { id: name },
    })}
    passHref
>
    <Button className="store-info-card-cta" color="secondary">
        {formatMessage({ id: 'store_finder_search_result_details' })}
    </Button>
</Link>

Example return of getLocalizedRoute function

{
  "pathname": "/nl/winkels/[id]",
  "query": {
    "id": "lommel"
  }
}

Current result

Alright, that's the setup.

  1. Now whenever you press the link next will first throw an error that you can see on the screenshot below. Error message shown before routing event starts

  2. Then it will start 'reloading' the browser and continue routing to the correct path without an issue. Reloading image example

  3. On Production this works without the error, but you still see the browser reloading, which is not ideal.

  4. The only thing that works, is routing without the rewrites and localization and just routing to i.e.: /shops/lommel. Which is again, not ideal since the customer relies on localized urls.

<Link
    href={{
        pathname: `/${locale}/stores/[id]`,
        query: { id: name },
    }}
    passHref
>
    <Button className="store-info-card-cta" color="secondary">
        {formatMessage({ id: 'store_finder_search_result_details' })}
    </Button>
</Link>
Wesley Janse
  • 1,244
  • 1
  • 7
  • 14
  • Have you considered an approach as described in [How to setup i18n translated URL routes in Next.js?](https://stackoverflow.com/a/68731057/1870780)? Could be refactored slightly so it would work with your current `pathnames.js` logic. – juliomalves Oct 12 '21 at 18:04
  • I guess I could give that a go, but what would that change regarding the error? the approach looks very similar in my eyes. – Wesley Janse Oct 13 '21 at 12:29
  • I guess the issue in your approach is that you're not factoring in the `locale` in your `rewrites`. – juliomalves Oct 13 '21 at 12:37
  • Alright thanks! I'll test it out and give an update on the progress when I'm done. – Wesley Janse Oct 14 '21 at 06:42
  • This gives exactly the same result, Refactored the next.config.js to include the locales and set locale to false, also rewrote the pathnames file to a locale first approach instead of pathname first (which in the end is exactly the same). It's a bummer, it's nothing 'breaking' but would love to know why it's not working. @juliomalves thanks for the help. – Wesley Janse Oct 20 '21 at 09:43
  • Are you sure your `rewrites` are set up properly? Next.js shouldn't be trying to access `.../page/nl/winkels/%5Bid%5D` as the rewrite would map that to `.../pages/nl/stores/%5Bid%5D` instead. – juliomalves Oct 20 '21 at 09:58
  • Jup, It only does this with dynamic paths. this is an example with 2 different routes. the non dynamic path works the other one doesn't ```module.exports = { async rewrites() { return [ { source: "/nl/winkels", destination: "/nl/stores", locale: false, }, { source: "/nl/winkels/:id", destination: "/nl/stores/:id", locale: false, }, ]; }, };``` Hope you can read this, because the formatting doesn't apply in the comment. – Wesley Janse Oct 20 '21 at 10:28

0 Answers0