algolia / algolia/instantsearch

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

Open
#5,941 1 comment 0 reactions 0 assignees View on GitHub
triage
Dominant language
TypeScript
Stars
4.1k
Forks
553
Avg merge
1d 11h
Merged PRs (30d)
51

Description

### 🐛 Current behavior

the fix on push method work on all the url, even on the domain part and not only on the path
```ts
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

```ts
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

- [X] I agree to follow this project's Code of Conduct

Contributor guide

Open the contributing guide

Research direction

Start from the push method in createInstantSearchRouterNext for react-instantsearch-router-nextjs and inspect how singletonRouter.locale is removed from newUrl. Reproduce with a locale in the hostname, such as https://it.mydomain.eu/product; done means only the path locale is removed and the domain remains unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
next.js, react, typescript
Domain
frontend
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.