react-instantsearch-nextjs 1.2.8 → 1.2.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -25,6 +25,7 @@ var _utils = require("instantsearch.js/cjs/lib/utils");
25
25
  var _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
26
26
  var _reactinstantsearchcore = require("react-instantsearch-core");
27
27
  var _InitializePromise = require("./InitializePromise");
28
+ var _RefreshOnClientNavigation = require("./RefreshOnClientNavigation");
28
29
  var _TriggerSearch = require("./TriggerSearch");
29
30
  var _useDynamicRouteWarning = require("./useDynamicRouteWarning");
30
31
  var _useInstantSearchRouting = require("./useInstantSearchRouting");
@@ -71,7 +72,7 @@ function InstantSearchNext(_0) {
71
72
  nonce: nonce
72
73
  }), children, isServer && /*#__PURE__*/ _react.default.createElement(_TriggerSearch.TriggerSearch, {
73
74
  nonce: nonce
74
- })));
75
+ }), !isServer && /*#__PURE__*/ _react.default.createElement(_RefreshOnClientNavigation.RefreshOnClientNavigation, null)));
75
76
  }
76
77
  function ServerOrHydrationProvider(param) {
77
78
  var isServer = param.isServer, children = param.children, instance = param.instance, ignoreMultipleHooksWarning = param.ignoreMultipleHooksWarning;
@@ -0,0 +1,69 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ function _export(target, all) {
7
+ for(var name in all)Object.defineProperty(target, name, {
8
+ enumerable: true,
9
+ get: Object.getOwnPropertyDescriptor(all, name).get
10
+ });
11
+ }
12
+ _export(exports, {
13
+ get RefreshOnClientNavigation () {
14
+ return RefreshOnClientNavigation;
15
+ },
16
+ get isClientNavigation () {
17
+ return isClientNavigation;
18
+ }
19
+ });
20
+ var _sliced_to_array = require("@swc/helpers/_/_sliced_to_array");
21
+ var _react = require("react");
22
+ var _reactinstantsearchcore = require("react-instantsearch-core");
23
+ // Fallback store for the first location an `InstantSearchNext` instance
24
+ // rendered with, used only in environments without the Navigation Timing API
25
+ // (e.g. jsdom in tests). See `getInitialLocation` below.
26
+ var InstantSearchDocumentLocation = Symbol.for('InstantSearchDocumentLocation');
27
+ /**
28
+ * The `pathname` + `search` the document was initially loaded with.
29
+ *
30
+ * It comes from the Navigation Timing API, which reflects the hard page load
31
+ * and is *not* affected by client-side (SPA) navigations. The hash is ignored
32
+ * as it isn't part of the routing state. Falls back to the first location any
33
+ * instance rendered with (stored on `window`) when Navigation Timing is
34
+ * unavailable.
35
+ */ function getInitialLocation(currentLocation) {
36
+ try {
37
+ var _performance_getEntriesByType = _sliced_to_array._(performance.getEntriesByType('navigation'), 1), entry = _performance_getEntriesByType[0];
38
+ if (entry && entry.name) {
39
+ var url = new URL(entry.name);
40
+ return url.pathname + url.search;
41
+ }
42
+ } catch (e) {
43
+ // Navigation Timing not available; fall through to the `window` fallback.
44
+ }
45
+ if (window[InstantSearchDocumentLocation] === undefined) {
46
+ window[InstantSearchDocumentLocation] = currentLocation;
47
+ }
48
+ return window[InstantSearchDocumentLocation];
49
+ }
50
+ function isClientNavigation() {
51
+ var currentLocation = window.location.pathname + window.location.search;
52
+ return getInitialLocation(currentLocation) !== currentLocation;
53
+ }
54
+ function RefreshOnClientNavigation() {
55
+ var search = (0, _reactinstantsearchcore.useInstantSearchContext)();
56
+ var hasRefreshed = (0, _react.useRef)(false);
57
+ (0, _react.useEffect)(function() {
58
+ if (hasRefreshed.current) {
59
+ return;
60
+ }
61
+ hasRefreshed.current = true;
62
+ if (search.started && isClientNavigation()) {
63
+ search.refresh();
64
+ }
65
+ }, [
66
+ search
67
+ ]);
68
+ return null;
69
+ }
@@ -5,6 +5,7 @@ import { safelyRunOnBrowser } from 'instantsearch.js/es/lib/utils/index.js';
5
5
  import React, { useRef, useEffect } from 'react';
6
6
  import { InstantSearch, InstantSearchRSCContext, InstantSearchSSRContext } from 'react-instantsearch-core';
7
7
  import { InitializePromise } from './InitializePromise.js';
8
+ import { RefreshOnClientNavigation } from './RefreshOnClientNavigation.js';
8
9
  import { TriggerSearch } from './TriggerSearch.js';
9
10
  import { useDynamicRouteWarning } from './useDynamicRouteWarning.js';
10
11
  import { useInstantSearchRouting } from './useInstantSearchRouting.js';
@@ -52,7 +53,7 @@ function InstantSearchNext(_0) {
52
53
  nonce: nonce
53
54
  }), children, isServer && /*#__PURE__*/ React.createElement(TriggerSearch, {
54
55
  nonce: nonce
55
- })));
56
+ }), !isServer && /*#__PURE__*/ React.createElement(RefreshOnClientNavigation, null)));
56
57
  }
57
58
  function ServerOrHydrationProvider(param) {
58
59
  var isServer = param.isServer, children = param.children, instance = param.instance, ignoreMultipleHooksWarning = param.ignoreMultipleHooksWarning;
@@ -0,0 +1,28 @@
1
+ declare const InstantSearchDocumentLocation: unique symbol;
2
+ declare global {
3
+ interface Window {
4
+ [InstantSearchDocumentLocation]?: string;
5
+ }
6
+ }
7
+ /**
8
+ * Whether the current location differs from the one the document was initially
9
+ * loaded with — i.e. whether we reached this render through a client-side (SPA)
10
+ * navigation rather than the initial hydration.
11
+ */
12
+ export declare function isClientNavigation(): boolean;
13
+ /**
14
+ * Refreshes the search after a client-side navigation.
15
+ *
16
+ * On a client-side navigation the App Router remounts `InstantSearchNext` with
17
+ * the server-streamed `initialResults`, which can be empty (e.g. coming from a
18
+ * page without InstantSearch) or stale, and the instance doesn't search again
19
+ * on its own — leaving the new page with no or outdated hits until a full
20
+ * reload (#7060). When we detect such a navigation we refresh the search so it
21
+ * re-fetches with the (already correct) current UI state.
22
+ *
23
+ * Unlike re-running the router's `onUpdate`, `refresh()` doesn't touch the URL,
24
+ * so it can't wipe the URL of a nested `<Index>` whose children only register
25
+ * on a second render pass (#6980).
26
+ */
27
+ export declare function RefreshOnClientNavigation(): null;
28
+ export {};
@@ -0,0 +1,70 @@
1
+ import { _ } from '@swc/helpers/esm/_sliced_to_array.js';
2
+ import { useRef, useEffect } from 'react';
3
+ import { useInstantSearchContext } from 'react-instantsearch-core';
4
+
5
+ // Fallback store for the first location an `InstantSearchNext` instance
6
+ // rendered with, used only in environments without the Navigation Timing API
7
+ // (e.g. jsdom in tests). See `getInitialLocation` below.
8
+ var InstantSearchDocumentLocation = Symbol.for('InstantSearchDocumentLocation');
9
+ /**
10
+ * The `pathname` + `search` the document was initially loaded with.
11
+ *
12
+ * It comes from the Navigation Timing API, which reflects the hard page load
13
+ * and is *not* affected by client-side (SPA) navigations. The hash is ignored
14
+ * as it isn't part of the routing state. Falls back to the first location any
15
+ * instance rendered with (stored on `window`) when Navigation Timing is
16
+ * unavailable.
17
+ */ function getInitialLocation(currentLocation) {
18
+ try {
19
+ var _performance_getEntriesByType = _(performance.getEntriesByType('navigation'), 1), entry = _performance_getEntriesByType[0];
20
+ if (entry && entry.name) {
21
+ var url = new URL(entry.name);
22
+ return url.pathname + url.search;
23
+ }
24
+ } catch (e) {
25
+ // Navigation Timing not available; fall through to the `window` fallback.
26
+ }
27
+ if (window[InstantSearchDocumentLocation] === undefined) {
28
+ window[InstantSearchDocumentLocation] = currentLocation;
29
+ }
30
+ return window[InstantSearchDocumentLocation];
31
+ }
32
+ /**
33
+ * Whether the current location differs from the one the document was initially
34
+ * loaded with — i.e. whether we reached this render through a client-side (SPA)
35
+ * navigation rather than the initial hydration.
36
+ */ function isClientNavigation() {
37
+ var currentLocation = window.location.pathname + window.location.search;
38
+ return getInitialLocation(currentLocation) !== currentLocation;
39
+ }
40
+ /**
41
+ * Refreshes the search after a client-side navigation.
42
+ *
43
+ * On a client-side navigation the App Router remounts `InstantSearchNext` with
44
+ * the server-streamed `initialResults`, which can be empty (e.g. coming from a
45
+ * page without InstantSearch) or stale, and the instance doesn't search again
46
+ * on its own — leaving the new page with no or outdated hits until a full
47
+ * reload (#7060). When we detect such a navigation we refresh the search so it
48
+ * re-fetches with the (already correct) current UI state.
49
+ *
50
+ * Unlike re-running the router's `onUpdate`, `refresh()` doesn't touch the URL,
51
+ * so it can't wipe the URL of a nested `<Index>` whose children only register
52
+ * on a second render pass (#6980).
53
+ */ function RefreshOnClientNavigation() {
54
+ var search = useInstantSearchContext();
55
+ var hasRefreshed = useRef(false);
56
+ useEffect(function() {
57
+ if (hasRefreshed.current) {
58
+ return;
59
+ }
60
+ hasRefreshed.current = true;
61
+ if (search.started && isClientNavigation()) {
62
+ search.refresh();
63
+ }
64
+ }, [
65
+ search
66
+ ]);
67
+ return null;
68
+ }
69
+
70
+ export { RefreshOnClientNavigation, isClientNavigation };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-instantsearch-nextjs",
3
- "version": "1.2.8",
3
+ "version": "1.2.9",
4
4
  "description": "React InstantSearch SSR utilities for Next.js",
5
5
  "types": "dist/es/index.d.ts",
6
6
  "main": "dist/cjs/index.js",
@@ -52,9 +52,9 @@
52
52
  },
53
53
  "devDependencies": {
54
54
  "@playwright/test": "1.49.1",
55
- "instantsearch.js": "4.101.1",
55
+ "instantsearch.js": "4.102.0",
56
56
  "next": "16.0.7",
57
- "react-instantsearch-core": "7.35.1",
57
+ "react-instantsearch-core": "7.36.0",
58
58
  "start-server-and-test": "1.15.3",
59
59
  "ts-node": "8.4.1"
60
60
  },
@@ -62,5 +62,5 @@
62
62
  "next": ">= 13.4 < 17",
63
63
  "react-instantsearch": ">= 7.1.0 < 8"
64
64
  },
65
- "gitHead": "8f1f4c966eccc81fbd5b7c3003319d040ec64fdb"
65
+ "gitHead": "9f2107a28e81b33218bf22a29a1d327348eb7906"
66
66
  }