react-instantsearch-router-nextjs wrong replace url on push
antonio-spinelli opened this issue Β· comments
π 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
- has domain with locale part as third level (example https://it.mydomain.eu)
- 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
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