react-instantsearch-router-nextjs
Version:
React InstantSearch Router for Next.js
84 lines (81 loc) • 3.85 kB
JavaScript
import { _ } from '@swc/helpers/esm/_object_spread.js';
import history from 'instantsearch.js/es/lib/routers/history.js';
import { stripLocaleFromUrl } from './utils/stripLocaleFromUrl.js';
function createInstantSearchRouterNext(options) {
var _ref = options || {}, beforePopState = _ref.beforePopState, singletonRouter = _ref.singletonRouter, serverUrl = _ref.serverUrl, beforeStart = _ref.beforeStart, beforeDispose = _ref.beforeDispose, routerOptions = _ref.routerOptions;
var handler;
var ownBeforePopState = function ownBeforePopState() {
return true;
};
// If we're rendering on the server, we create a simpler router
if (typeof window === 'undefined') {
return history(_({
getLocation: function getLocation() {
return new URL(serverUrl);
}
}, routerOptions));
}
/**
* Marker to skip `routeChangeComplete` event when the push comes from the router itself.
*/ var lastPushFromThis = false;
var router = history(_({
start: function start(onUpdate) {
var _singletonRouter_router;
if (beforeStart) {
beforeStart(onUpdate);
}
var initialPathname = singletonRouter.pathname;
handler = function handler() {
// Without this check, we would trigger an unnecessary search when navigating
// to a page without InstantSearch
if (singletonRouter.pathname === initialPathname && !lastPushFromThis) {
onUpdate();
}
lastPushFromThis = false;
};
singletonRouter.events.on('routeChangeComplete', handler);
if ((_singletonRouter_router = singletonRouter.router) === null || _singletonRouter_router === void 0 ? void 0 : _singletonRouter_router._bps) {
ownBeforePopState = singletonRouter.router._bps;
}
function libraryBeforePopState() {
var previousPathname = singletonRouter.asPath.split('?')[0];
var nextPathname = new URL(window.location.href).pathname;
// We strip the locale from the pathname if it's present
nextPathname = stripLocaleFromUrl(nextPathname, singletonRouter.locale, previousPathname !== '/');
// We only want to trigger a server request when going back/forward to a different page
return previousPathname !== nextPathname;
}
singletonRouter.beforePopState(function(state) {
if (beforePopState) {
return beforePopState({
state: state,
libraryBeforePopState: libraryBeforePopState,
ownBeforePopState: ownBeforePopState
});
}
return libraryBeforePopState();
});
},
dispose: function dispose() {
if (beforeDispose) {
beforeDispose();
}
singletonRouter.events.off('routeChangeComplete', handler);
singletonRouter.beforePopState(ownBeforePopState);
},
push: function push(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
var url = stripLocaleFromUrl(newUrl, singletonRouter.locale);
// No need to provide the second argument, Next.js will know what to do
singletonRouter.push(url, undefined, {
shallow: true
});
lastPushFromThis = true;
}
}, routerOptions));
router._isNextRouter = true;
router.$$type = 'ais.nextjs';
return router;
}
export { createInstantSearchRouterNext };