UNPKG

react-instantsearch-nextjs

Version:
81 lines 4.06 kB
var _excluded = ["children", "routing", "instance", "ignoreMultipleHooksWarning"]; function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; } function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; } import { safelyRunOnBrowser } from "instantsearch.js/es/lib/utils/index.js"; import React, { useEffect, useRef } from 'react'; import { InstantSearch, InstantSearchRSCContext, InstantSearchSSRContext } from 'react-instantsearch-core'; import { InitializePromise } from "./InitializePromise.js"; import { TriggerSearch } from "./TriggerSearch.js"; import { useDynamicRouteWarning } from "./useDynamicRouteWarning.js"; import { useInstantSearchRouting } from "./useInstantSearchRouting.js"; import { useNextHeaders } from "./useNextHeaders.js"; import { warn } from "./warn.js"; var InstantSearchInitialResults = Symbol.for('InstantSearchInitialResults'); export function createInstantSearchNextInstance() { return { current: null }; } export function InstantSearchNext(_ref) { var children = _ref.children, passedRouting = _ref.routing, instance = _ref.instance, _ref$ignoreMultipleHo = _ref.ignoreMultipleHooksWarning, ignoreMultipleHooksWarning = _ref$ignoreMultipleHo === void 0 ? false : _ref$ignoreMultipleHo, instantSearchProps = _objectWithoutProperties(_ref, _excluded); var isMounting = useRef(true); var isServer = typeof window === 'undefined'; useEffect(function () { isMounting.current = false; }, []); var headers = useNextHeaders(); var nonce = safelyRunOnBrowser(function () { return undefined; }, { fallback: function fallback() { return (headers === null || headers === void 0 ? void 0 : headers.get('x-nonce')) || undefined; } }); useDynamicRouteWarning({ isServer: isServer, isMounting: isMounting, instance: instance }); var routing = useInstantSearchRouting(passedRouting, isMounting); process.env.NODE_ENV === 'development' ? warn(false, "InstantSearchNext relies on experimental APIs and may break in the future.\nThis message will only be displayed in development mode.") : void 0; return /*#__PURE__*/React.createElement(ServerOrHydrationProvider, { isServer: isServer, instance: instance, ignoreMultipleHooksWarning: ignoreMultipleHooksWarning }, /*#__PURE__*/React.createElement(InstantSearch, _extends({}, instantSearchProps, { routing: routing }), isServer && /*#__PURE__*/React.createElement(InitializePromise, { nonce: nonce }), children, isServer && /*#__PURE__*/React.createElement(TriggerSearch, { nonce: nonce }))); } function ServerOrHydrationProvider(_ref2) { var isServer = _ref2.isServer, children = _ref2.children, instance = _ref2.instance, ignoreMultipleHooksWarning = _ref2.ignoreMultipleHooksWarning; var promiseRef = useRef(null); var countRef = useRef(0); var initialResults = safelyRunOnBrowser(function () { return window[InstantSearchInitialResults]; }); return /*#__PURE__*/React.createElement(InstantSearchRSCContext.Provider, { value: { waitForResultsRef: promiseRef, countRef: countRef, ignoreMultipleHooksWarning: ignoreMultipleHooksWarning } }, /*#__PURE__*/React.createElement(InstantSearchSSRContext.Provider, { value: { initialResults: initialResults, ssrSearchRef: isServer ? undefined : instance } }, children)); }