algolia / instantsearch

⚑️ Libraries for building performant and instant search and recommend experiences with Algolia. Compatible with JavaScript, TypeScript, React and Vue.
https://www.algolia.com/doc/guides/building-search-ui/what-is-instantsearch/js/
MIT License
3.72k stars 522 forks source link

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

Open antonio-spinelli opened 11 months ago

antonio-spinelli commented 11 months ago

πŸ› 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

Haroenv commented 11 months ago

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