@shopify/hydrogen 2023.7.4 → 2023.7.6

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.
@@ -427,7 +427,7 @@ var warnOnce = (string) => {
427
427
  };
428
428
 
429
429
  // src/version.ts
430
- var LIB_VERSION = "2023.7.4";
430
+ var LIB_VERSION = "2023.7.6";
431
431
 
432
432
  // src/storefront.ts
433
433
  var StorefrontApiError = class extends Error {
@@ -1368,8 +1368,35 @@ function Pagination({
1368
1368
  PreviousLink
1369
1369
  });
1370
1370
  }
1371
+ function getParamsWithoutPagination(paramsString) {
1372
+ const params = new URLSearchParams(paramsString);
1373
+ params.delete("cursor");
1374
+ params.delete("direction");
1375
+ return params.toString();
1376
+ }
1377
+ function makeError(prop) {
1378
+ throw new Error(
1379
+ `The Pagination component requires ${"`" + prop + "`"} to be a part of your query. See the guide on how to setup your query to include ${"`" + prop + "`"}: https://shopify.dev/docs/custom-storefronts/hydrogen/data-fetching/pagination#setup-the-paginated-query`
1380
+ );
1381
+ }
1371
1382
  function usePagination(connection) {
1372
- const { state, search } = react$1.useLocation();
1383
+ if (!connection.pageInfo) {
1384
+ makeError("pageInfo");
1385
+ }
1386
+ if (typeof connection.pageInfo.startCursor === "undefined") {
1387
+ makeError("pageInfo.startCursor");
1388
+ }
1389
+ if (typeof connection.pageInfo.endCursor === "undefined") {
1390
+ makeError("pageInfo.endCursor");
1391
+ }
1392
+ if (typeof connection.pageInfo.hasNextPage === "undefined") {
1393
+ makeError("pageInfo.hasNextPage");
1394
+ }
1395
+ if (typeof connection.pageInfo.hasPreviousPage === "undefined") {
1396
+ makeError("pageInfo.hasPreviousPage");
1397
+ }
1398
+ const navigate = react$1.useNavigate();
1399
+ const { state, search, pathname } = react$1.useLocation();
1373
1400
  const params = new URLSearchParams(search);
1374
1401
  const direction = params.get("direction");
1375
1402
  const isPrevious = direction === "previous";
@@ -1380,18 +1407,34 @@ function usePagination(connection) {
1380
1407
  hasPreviousPage: connection.pageInfo.hasPreviousPage,
1381
1408
  hasNextPage: connection.pageInfo.hasNextPage
1382
1409
  });
1410
+ const urlRef = react.useRef({
1411
+ params: getParamsWithoutPagination(search),
1412
+ pathname
1413
+ });
1383
1414
  react.useEffect(() => {
1384
- if (state?.nodes) {
1415
+ if (
1416
+ // If the URL changes (independent of pagination params)
1417
+ // then reset the pagination params in the URL
1418
+ getParamsWithoutPagination(search) !== urlRef.current.params || pathname !== urlRef.current.pathname
1419
+ ) {
1420
+ urlRef.current = {
1421
+ pathname,
1422
+ params: getParamsWithoutPagination(search)
1423
+ };
1424
+ navigate(`${pathname}?${getParamsWithoutPagination(search)}`, {
1425
+ replace: true,
1426
+ preventScrollReset: true,
1427
+ state: { nodes: void 0, pageInfo: void 0 }
1428
+ });
1429
+ } else if (state?.nodes) {
1385
1430
  setNodes(
1386
1431
  isPrevious ? [...hydrogenReact.flattenConnection(connection), ...state.nodes] : [...state.nodes, ...hydrogenReact.flattenConnection(connection)]
1387
1432
  );
1388
- }
1389
- if (state?.pageInfo) {
1390
- let pageStartCursor = state?.pageInfo?.startCursor === void 0 ? connection.pageInfo.startCursor : state.pageInfo.startCursor;
1391
- let pageEndCursor = state?.pageInfo?.endCursor === void 0 ? connection.pageInfo.endCursor : state.pageInfo.endCursor;
1392
- let previousPageExists = state?.pageInfo?.hasPreviousPage === void 0 ? connection.pageInfo.hasPreviousPage : state.pageInfo.hasPreviousPage;
1393
- let nextPageExists = state?.pageInfo?.hasNextPage === void 0 ? connection.pageInfo.hasNextPage : state.pageInfo.hasNextPage;
1394
- if (state?.nodes) {
1433
+ if (state?.pageInfo) {
1434
+ let pageStartCursor = state?.pageInfo?.startCursor === void 0 ? connection.pageInfo.startCursor : state.pageInfo.startCursor;
1435
+ let pageEndCursor = state?.pageInfo?.endCursor === void 0 ? connection.pageInfo.endCursor : state.pageInfo.endCursor;
1436
+ let previousPageExists = state?.pageInfo?.hasPreviousPage === void 0 ? connection.pageInfo.hasPreviousPage : state.pageInfo.hasPreviousPage;
1437
+ let nextPageExists = state?.pageInfo?.hasNextPage === void 0 ? connection.pageInfo.hasNextPage : state.pageInfo.hasNextPage;
1395
1438
  if (isPrevious) {
1396
1439
  pageStartCursor = connection.pageInfo.startCursor;
1397
1440
  previousPageExists = connection.pageInfo.hasPreviousPage;
@@ -1399,15 +1442,23 @@ function usePagination(connection) {
1399
1442
  pageEndCursor = connection.pageInfo.endCursor;
1400
1443
  nextPageExists = connection.pageInfo.hasNextPage;
1401
1444
  }
1445
+ setCurrentPageInfo({
1446
+ startCursor: pageStartCursor,
1447
+ endCursor: pageEndCursor,
1448
+ hasPreviousPage: previousPageExists,
1449
+ hasNextPage: nextPageExists
1450
+ });
1402
1451
  }
1452
+ } else {
1453
+ setNodes(hydrogenReact.flattenConnection(connection));
1403
1454
  setCurrentPageInfo({
1404
- startCursor: pageStartCursor,
1405
- endCursor: pageEndCursor,
1406
- hasPreviousPage: previousPageExists,
1407
- hasNextPage: nextPageExists
1455
+ startCursor: connection.pageInfo.startCursor,
1456
+ endCursor: connection.pageInfo.endCursor,
1457
+ hasPreviousPage: connection.pageInfo.hasPreviousPage,
1458
+ hasNextPage: connection.pageInfo.hasNextPage
1408
1459
  });
1409
1460
  }
1410
- }, [state, connection]);
1461
+ }, [state, connection, isPrevious, search, navigate, pathname]);
1411
1462
  const previousPageUrl = react.useMemo(() => {
1412
1463
  const params2 = new URLSearchParams(search);
1413
1464
  params2.set("direction", "previous");