Custom instantsearch.js router for Turbo-compatibility

Hi there! I’m using instantsearch.js v4 together with Turbo. I’m running into a common issue where the back button breaks when using these two libraries together.

The solution seems to call a Turbo function directly after calling window.history.pushState, like this:

 history.pushState({}, null, newUrl);

I dug into the instantsearch code and found that instantsearch.routers.history calls history.pushState right here (line 132):

Unfortunately there’s no way to pass in your own write function, so it seems like we need to create a custom router.

What’s the simplest way to create such a router?

Basically what I want is to take the existing instantsearch.routers.history and just override the write function to include the call to Turbo.navigator.history.push.

I also considered using setWindowTitle for this, because it happens to be called in just the right place. But it seems to also get called on initialisation in which case we don’t want to call the Turbo function.