UNPKG

react-instantsearch-nextjs

Version:
86 lines (84 loc) 3.83 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "useInstantSearchRouting", { enumerable: true, get: function() { return useInstantSearchRouting; } }); var _interop_require_default = require("@swc/helpers/_/_interop_require_default"); var _object_spread = require("@swc/helpers/_/_object_spread"); var _type_of = require("@swc/helpers/_/_type_of"); var _history = /*#__PURE__*/ _interop_require_default._(require("instantsearch.js/cjs/lib/routers/history")); var _navigation = require("next/navigation"); var _react = require("react"); var _useNextHeaders = require("./useNextHeaders"); function useInstantSearchRouting(passedRouting, isMounting) { var isServer = typeof window === 'undefined'; var pathname = (0, _navigation.usePathname)(); var searchParams = (0, _navigation.useSearchParams)(); var routingRef = (0, _react.useRef)(null); var onUpdateRef = (0, _react.useRef)(null); var isUnmounting = (0, _react.useRef)(false); // Skip the on-mount fire of the effect below: `subscribe()` already merges // the URL into `_initialUiState`, and a redundant `setUiState` can wipe the // URL with a nested `<Index>` (see #6980). var previousRouteRef = (0, _react.useRef)(null); (0, _react.useEffect)(function() { var _ref; var currentRoute = "".concat(pathname !== null && pathname !== void 0 ? pathname : '', "?").concat((_ref = searchParams === null || searchParams === void 0 ? void 0 : searchParams.toString()) !== null && _ref !== void 0 ? _ref : ''); if (previousRouteRef.current === currentRoute) { return; } var isFirstRun = previousRouteRef.current === null; previousRouteRef.current = currentRoute; if (isFirstRun) { return; } if (onUpdateRef.current) { onUpdateRef.current(); } }, [ pathname, searchParams ]); (0, _react.useEffect)(function() { isUnmounting.current = false; return function() { isUnmounting.current = true; }; }); var headers = (0, _useNextHeaders.useNextHeaders)(); if (passedRouting && !routingRef.current) { var browserHistoryOptions = {}; browserHistoryOptions.getLocation = function() { if (isServer) { var url = "".concat((headers === null || headers === void 0 ? void 0 : headers.get('x-forwarded-proto')) || 'http', "://").concat(headers === null || headers === void 0 ? void 0 : headers.get('host')).concat(pathname, "?").concat(searchParams); return new URL(url); } if (isMounting.current) { return new URL("".concat(window.location.protocol, "//").concat(window.location.host).concat(pathname, "?").concat(searchParams)); } return window.location; }; browserHistoryOptions.push = function push(url) { // This is to skip the push with empty routeState on dispose as it would clear params set on a <Link> if (this.isDisposed && isUnmounting.current) { return; } history.pushState({}, '', url); }; browserHistoryOptions.start = function start(onUpdate) { onUpdateRef.current = onUpdate; }; routingRef.current = {}; if ((typeof passedRouting === "undefined" ? "undefined" : _type_of._(passedRouting)) === 'object') { browserHistoryOptions = _object_spread._({}, browserHistoryOptions, passedRouting.router); routingRef.current.stateMapping = passedRouting.stateMapping; } routingRef.current.router = (0, _history.default)(browserHistoryOptions); } return routingRef.current; }