react-instantsearch-nextjs
Version:
React InstantSearch SSR utilities for Next.js
81 lines • 4.4 kB
JavaScript
var _excluded = ["children", "routing", "instance", "ignoreMultipleHooksWarning"];
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
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));
}