Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

react-instantsearch-router-nextjs wrong replace url on push #5941

Open
1 task done
antonio-spinelli opened this issue Nov 22, 2023 · 1 comment
Open
1 task done
Labels
triage Issues to be categorized by the team

Comments

@antonio-spinelli
Copy link

🐛 Current behavior

the fix on push method work on all the url, even on the domain part and not only on the path

    push(newUrl) {
      let url = newUrl
      // We need to do this because there's an error when using i18n on the root path
      // it says for example `pages/fr.js` doesn't exist
      if (singletonRouter.locale) {
        url = url.replace(`/${singletonRouter.locale}`, '');
      }

      // No need to provide the second argument, Next.js will know what to do
      singletonRouter.push(url, undefined, {
        shallow: true,
      })
    },

if the url is something like https://it.mydomain.eu/product with the locale part in third level domain, it will replace the url to https://.mydomain.eu/product

🔍 Steps to reproduce

  1. has domain with locale part as third level (example https://it.mydomain.eu)
  2. use createInstantSearchRouterNext of react-instantsearch-router-nextjs

Live reproduction

unable to reproduce on codesandbox

💭 Expected behavior

should only replace the path part

Possibile Solution

you can try with a regex like this: https://regex101.com/r/U8mzbj/1

    push(newUrl) {
      let url = newUrl
      // We need to do this because there's an error when using i18n on the root path
      // it says for example `pages/fr.js` doesn't exist
      if (singletonRouter.locale) {
        url = url.replace(new RegExp(`(?<=^https?:\/\/[^\/]+)\/${singletonRouter.locale}(?=\/|$)`, 'i'), '');
      }

      // No need to provide the second argument, Next.js will know what to do
      singletonRouter.push(url, undefined, {
        shallow: true,
      })
    },

Package version

7.3.0

Operating system

No response

Browser

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@antonio-spinelli antonio-spinelli added the triage Issues to be categorized by the team label Nov 22, 2023
antonio-spinelli added a commit to antonio-spinelli/instantsearch that referenced this issue Nov 22, 2023
@Haroenv
Copy link
Contributor

Haroenv commented Nov 22, 2023

Instead of the hardcoded fix, is there a way to get the i18n configuration of Next so we can use that as the source of truth? it's indeed also possible that you're using the locale somewhere else in the url and don't want that replaced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
triage Issues to be categorized by the team
Projects
None yet
Development

No branches or pull requests

2 participants