@inertiajs/core 2.2.6 → 2.2.8

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.
package/dist/index.js CHANGED
@@ -45,13 +45,15 @@ __export(index_exports, {
45
45
  setupProgress: () => setupProgress,
46
46
  shouldIntercept: () => shouldIntercept,
47
47
  shouldNavigate: () => shouldNavigate,
48
+ urlHasProtocol: () => urlHasProtocol,
49
+ urlToString: () => urlToString,
48
50
  urlWithoutHash: () => urlWithoutHash,
49
51
  useInfiniteScroll: () => useInfiniteScroll
50
52
  });
51
53
  module.exports = __toCommonJS(index_exports);
52
54
 
53
55
  // src/router.ts
54
- var import_lodash_es3 = require("lodash-es");
56
+ var import_lodash_es4 = require("lodash-es");
55
57
 
56
58
  // src/debounce.ts
57
59
  function debounce(fn, delay) {
@@ -103,6 +105,9 @@ var firePrefetchingEvent = (visit) => {
103
105
  return fireEvent("prefetching", { detail: { visit } });
104
106
  };
105
107
 
108
+ // src/history.ts
109
+ var import_lodash_es = require("lodash-es");
110
+
106
111
  // src/sessionStorage.ts
107
112
  var SessionStorage = class {
108
113
  static set(key, value) {
@@ -312,18 +317,24 @@ var Scroll = class {
312
317
  }
313
318
  window.requestAnimationFrame(() => {
314
319
  this.restoreDocument();
315
- this.regions().forEach((region, index) => {
316
- const scrollPosition = scrollRegions[index];
317
- if (!scrollPosition) {
318
- return;
319
- }
320
- if (typeof region.scrollTo === "function") {
321
- region.scrollTo(scrollPosition.left, scrollPosition.top);
322
- } else {
323
- region.scrollTop = scrollPosition.top;
324
- region.scrollLeft = scrollPosition.left;
325
- }
326
- });
320
+ this.restoreScrollRegions(scrollRegions);
321
+ });
322
+ }
323
+ static restoreScrollRegions(scrollRegions) {
324
+ if (typeof window === "undefined") {
325
+ return;
326
+ }
327
+ this.regions().forEach((region, index) => {
328
+ const scrollPosition = scrollRegions[index];
329
+ if (!scrollPosition) {
330
+ return;
331
+ }
332
+ if (typeof region.scrollTo === "function") {
333
+ region.scrollTo(scrollPosition.left, scrollPosition.top);
334
+ } else {
335
+ region.scrollTop = scrollPosition.top;
336
+ region.scrollLeft = scrollPosition.left;
337
+ }
327
338
  });
328
339
  }
329
340
  static restoreDocument() {
@@ -404,7 +415,7 @@ var transformUrlAndData = (href, data, method, forceFormData, queryStringArrayFo
404
415
  };
405
416
  function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets") {
406
417
  const hasDataForQueryString = method === "get" && !isFormData(data) && Object.keys(data).length > 0;
407
- const hasHost = /^[a-z][a-z0-9+.-]*:\/\//i.test(href.toString());
418
+ const hasHost = urlHasProtocol(href.toString());
408
419
  const hasAbsolutePath = hasHost || href.toString().startsWith("/") || href.toString() === "";
409
420
  const hasRelativePath = !hasAbsolutePath && !href.toString().startsWith("#") && !href.toString().startsWith("?");
410
421
  const hasRelativePathWithDotPrefix = /^[.]{1,2}([/]|$)/.test(href.toString());
@@ -448,6 +459,13 @@ var isSameUrlWithoutHash = (url1, url2) => {
448
459
  function isUrlMethodPair(href) {
449
460
  return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
450
461
  }
462
+ function urlHasProtocol(url) {
463
+ return /^[a-z][a-z0-9+.-]*:\/\//i.test(url);
464
+ }
465
+ function urlToString(url, absolute) {
466
+ const urlObj = typeof url === "string" ? hrefToUrl(url) : url;
467
+ return absolute ? `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}${urlObj.search}${urlObj.hash}` : `${urlObj.pathname}${urlObj.search}${urlObj.hash}`;
468
+ }
451
469
 
452
470
  // src/page.ts
453
471
  var CurrentPage = class {
@@ -486,7 +504,9 @@ var CurrentPage = class {
486
504
  return;
487
505
  }
488
506
  page2.rememberedState ?? (page2.rememberedState = {});
489
- const location = typeof window !== "undefined" ? window.location : new URL(page2.url);
507
+ const isServer2 = typeof window === "undefined";
508
+ const location = !isServer2 ? window.location : new URL(page2.url);
509
+ const scrollRegions = !isServer2 && preserveScroll ? history.getScrollRegions() : [];
490
510
  replace = replace || isSameUrlWithoutHash(hrefToUrl(page2.url), location);
491
511
  return new Promise((resolve) => {
492
512
  replace ? history.replaceState(page2, () => resolve(null)) : history.pushState(page2, () => resolve(null));
@@ -502,7 +522,9 @@ var CurrentPage = class {
502
522
  }
503
523
  this.isFirstPageLoad = false;
504
524
  return this.swap({ component, page: page2, preserveState }).then(() => {
505
- if (!preserveScroll) {
525
+ if (preserveScroll) {
526
+ window.requestAnimationFrame(() => Scroll.restoreScrollRegions(scrollRegions));
527
+ } else {
506
528
  Scroll.reset();
507
529
  }
508
530
  if (this.pendingDeferredProps && this.pendingDeferredProps.component === page2.component && this.pendingDeferredProps.url === page2.url) {
@@ -634,15 +656,13 @@ var History = class {
634
656
  this.current = page2;
635
657
  queue.add(() => {
636
658
  return this.getPageData(page2).then((data) => {
637
- const doPush = () => {
638
- this.doPushState({ page: data }, page2.url);
639
- cb && cb();
640
- };
659
+ const doPush = () => this.doPushState({ page: data }, page2.url).then(() => cb?.());
641
660
  if (isChromeIOS) {
642
- setTimeout(doPush);
643
- } else {
644
- doPush();
661
+ return new Promise((resolve) => {
662
+ setTimeout(() => doPush().then(resolve));
663
+ });
645
664
  }
665
+ return doPush();
646
666
  });
647
667
  });
648
668
  }
@@ -680,7 +700,10 @@ var History = class {
680
700
  if (!window.history.state?.page) {
681
701
  return;
682
702
  }
683
- this.doReplaceState({
703
+ if ((0, import_lodash_es.isEqual)(this.getScrollRegions(), scrollRegions)) {
704
+ return;
705
+ }
706
+ return this.doReplaceState({
684
707
  page: window.history.state.page,
685
708
  scrollRegions
686
709
  });
@@ -693,7 +716,10 @@ var History = class {
693
716
  if (!window.history.state?.page) {
694
717
  return;
695
718
  }
696
- this.doReplaceState({
719
+ if ((0, import_lodash_es.isEqual)(this.getDocumentScrollPosition(), scrollRegion)) {
720
+ return;
721
+ }
722
+ return this.doReplaceState({
697
723
  page: window.history.state.page,
698
724
  documentScrollPosition: scrollRegion
699
725
  });
@@ -718,31 +744,31 @@ var History = class {
718
744
  this.current = page2;
719
745
  queue.add(() => {
720
746
  return this.getPageData(page2).then((data) => {
721
- const doReplace = () => {
722
- this.doReplaceState({ page: data }, page2.url);
723
- cb && cb();
724
- };
747
+ const doReplace = () => this.doReplaceState({ page: data }, page2.url).then(() => cb?.());
725
748
  if (isChromeIOS) {
726
- setTimeout(doReplace);
727
- } else {
728
- doReplace();
749
+ return new Promise((resolve) => {
750
+ setTimeout(() => doReplace().then(resolve));
751
+ });
729
752
  }
753
+ return doReplace();
730
754
  });
731
755
  });
732
756
  }
733
757
  doReplaceState(data, url) {
734
- window.history.replaceState(
735
- {
736
- ...data,
737
- scrollRegions: data.scrollRegions ?? window.history.state?.scrollRegions,
738
- documentScrollPosition: data.documentScrollPosition ?? window.history.state?.documentScrollPosition
739
- },
740
- "",
741
- url
758
+ return Promise.resolve().then(
759
+ () => window.history.replaceState(
760
+ {
761
+ ...data,
762
+ scrollRegions: data.scrollRegions ?? window.history.state?.scrollRegions,
763
+ documentScrollPosition: data.documentScrollPosition ?? window.history.state?.documentScrollPosition
764
+ },
765
+ "",
766
+ url
767
+ )
742
768
  );
743
769
  }
744
770
  doPushState(data, url) {
745
- window.history.pushState(data, "", url);
771
+ return Promise.resolve().then(() => window.history.pushState(data, "", url));
746
772
  }
747
773
  getState(key, defaultValue) {
748
774
  return this.current?.[key] ?? defaultValue;
@@ -1021,7 +1047,7 @@ var Polls = class {
1021
1047
  var polls = new Polls();
1022
1048
 
1023
1049
  // src/prefetched.ts
1024
- var import_lodash_es = require("lodash-es");
1050
+ var import_lodash_es2 = require("lodash-es");
1025
1051
 
1026
1052
  // src/objectUtils.ts
1027
1053
  var objectsAreEqual = (obj1, obj2, excludeKeys) => {
@@ -1238,7 +1264,7 @@ var PrefetchedRequests = class {
1238
1264
  }) || null;
1239
1265
  }
1240
1266
  withoutPurposePrefetchHeader(params) {
1241
- const newParams = (0, import_lodash_es.cloneDeep)(params);
1267
+ const newParams = (0, import_lodash_es2.cloneDeep)(params);
1242
1268
  if (newParams.headers["Purpose"] === "prefetch") {
1243
1269
  delete newParams.headers["Purpose"];
1244
1270
  }
@@ -1414,7 +1440,7 @@ var RequestParams = class _RequestParams {
1414
1440
  };
1415
1441
 
1416
1442
  // src/response.ts
1417
- var import_lodash_es2 = require("lodash-es");
1443
+ var import_lodash_es3 = require("lodash-es");
1418
1444
 
1419
1445
  // src/modal.ts
1420
1446
  var modal_default = {
@@ -1624,8 +1650,8 @@ var Response = class _Response {
1624
1650
  const propsToDeepMerge = pageResponse.deepMergeProps || [];
1625
1651
  const matchPropsOn = pageResponse.matchPropsOn || [];
1626
1652
  const mergeProp = (prop, shouldAppend) => {
1627
- const currentProp = (0, import_lodash_es2.get)(page.get().props, prop);
1628
- const incomingProp = (0, import_lodash_es2.get)(pageResponse.props, prop);
1653
+ const currentProp = (0, import_lodash_es3.get)(page.get().props, prop);
1654
+ const incomingProp = (0, import_lodash_es3.get)(pageResponse.props, prop);
1629
1655
  if (Array.isArray(incomingProp)) {
1630
1656
  const newArray = this.mergeOrMatchItems(
1631
1657
  currentProp || [],
@@ -1634,13 +1660,13 @@ var Response = class _Response {
1634
1660
  matchPropsOn,
1635
1661
  shouldAppend
1636
1662
  );
1637
- (0, import_lodash_es2.set)(pageResponse.props, prop, newArray);
1663
+ (0, import_lodash_es3.set)(pageResponse.props, prop, newArray);
1638
1664
  } else if (typeof incomingProp === "object" && incomingProp !== null) {
1639
1665
  const newObject = {
1640
1666
  ...currentProp || {},
1641
1667
  ...incomingProp
1642
1668
  };
1643
- (0, import_lodash_es2.set)(pageResponse.props, prop, newObject);
1669
+ (0, import_lodash_es3.set)(pageResponse.props, prop, newObject);
1644
1670
  }
1645
1671
  };
1646
1672
  propsToAppend.forEach((prop) => mergeProp(prop, true));
@@ -2065,8 +2091,8 @@ var Router = class {
2065
2091
  preserveScroll: true,
2066
2092
  preserveState: true,
2067
2093
  props(currentProps) {
2068
- const newValue = typeof value === "function" ? value((0, import_lodash_es3.get)(currentProps, name), currentProps) : value;
2069
- return (0, import_lodash_es3.set)((0, import_lodash_es3.cloneDeep)(currentProps), name, newValue);
2094
+ const newValue = typeof value === "function" ? value((0, import_lodash_es4.get)(currentProps, name), currentProps) : value;
2095
+ return (0, import_lodash_es4.set)((0, import_lodash_es4.cloneDeep)(currentProps), name, newValue);
2070
2096
  },
2071
2097
  ...options || {}
2072
2098
  });
@@ -2226,10 +2252,45 @@ var elementInViewport = (el) => {
2226
2252
  return verticallyVisible && horizontallyVisible;
2227
2253
  };
2228
2254
  var getScrollableParent = (element) => {
2255
+ const allowsVerticalScroll = (el) => {
2256
+ const computedStyle = window.getComputedStyle(el);
2257
+ if (["scroll", "overlay"].includes(computedStyle.overflowY)) {
2258
+ return true;
2259
+ }
2260
+ if (computedStyle.overflowY !== "auto") {
2261
+ return false;
2262
+ }
2263
+ if (["visible", "clip"].includes(computedStyle.overflowX)) {
2264
+ return true;
2265
+ }
2266
+ return hasDimensionConstraint(computedStyle.maxHeight, el.style.height);
2267
+ };
2268
+ const allowsHorizontalScroll = (el) => {
2269
+ const computedStyle = window.getComputedStyle(el);
2270
+ if (["scroll", "overlay"].includes(computedStyle.overflowX)) {
2271
+ return true;
2272
+ }
2273
+ if (computedStyle.overflowX !== "auto") {
2274
+ return false;
2275
+ }
2276
+ if (["visible", "clip"].includes(computedStyle.overflowY)) {
2277
+ return true;
2278
+ }
2279
+ return hasDimensionConstraint(computedStyle.maxWidth, el.style.width);
2280
+ };
2281
+ const hasDimensionConstraint = (computedMaxDimension, inlineStyleDimension) => {
2282
+ if (computedMaxDimension && computedMaxDimension !== "none" && computedMaxDimension !== "0px") {
2283
+ return true;
2284
+ }
2285
+ if (inlineStyleDimension && inlineStyleDimension !== "auto" && inlineStyleDimension !== "0") {
2286
+ return true;
2287
+ }
2288
+ return false;
2289
+ };
2229
2290
  let parent = element?.parentElement;
2230
2291
  while (parent) {
2231
- const overflowY = window.getComputedStyle(parent).overflowY;
2232
- if (overflowY === "auto" || overflowY === "scroll") {
2292
+ const allowsScroll = allowsVerticalScroll(parent) || allowsHorizontalScroll(parent);
2293
+ if (window.getComputedStyle(parent).display !== "contents" && allowsScroll) {
2233
2294
  return parent;
2234
2295
  }
2235
2296
  parent = parent.parentElement;
@@ -2260,7 +2321,7 @@ var getElementsInViewportFromCollection = (referenceElement, elements) => {
2260
2321
  };
2261
2322
 
2262
2323
  // src/formObject.ts
2263
- var import_lodash_es4 = require("lodash-es");
2324
+ var import_lodash_es5 = require("lodash-es");
2264
2325
  function undotKey(key) {
2265
2326
  if (!key.includes(".")) {
2266
2327
  return key;
@@ -2295,15 +2356,15 @@ function formDataToObject(source) {
2295
2356
  const path = parseKey(undotKey(key));
2296
2357
  if (path[path.length - 1] === "") {
2297
2358
  const arrayPath = path.slice(0, -1);
2298
- const existing = (0, import_lodash_es4.get)(form, arrayPath);
2359
+ const existing = (0, import_lodash_es5.get)(form, arrayPath);
2299
2360
  if (Array.isArray(existing)) {
2300
2361
  existing.push(value);
2301
2362
  } else {
2302
- (0, import_lodash_es4.set)(form, arrayPath, [value]);
2363
+ (0, import_lodash_es5.set)(form, arrayPath, [value]);
2303
2364
  }
2304
2365
  continue;
2305
2366
  }
2306
- (0, import_lodash_es4.set)(form, path, value);
2367
+ (0, import_lodash_es5.set)(form, path, value);
2307
2368
  }
2308
2369
  return form;
2309
2370
  }
@@ -2731,6 +2792,7 @@ var useInfiniteScrollElementManager = (options) => {
2731
2792
  var queue3 = new Queue();
2732
2793
  var initialUrl;
2733
2794
  var payloadUrl;
2795
+ var initialUrlWasAbsolute = null;
2734
2796
  var useInfiniteScrollQueryString = (options) => {
2735
2797
  let enabled = true;
2736
2798
  const queuePageUpdate = (page2) => {
@@ -2741,8 +2803,10 @@ var useInfiniteScrollQueryString = (options) => {
2741
2803
  return resolve();
2742
2804
  }
2743
2805
  if (!initialUrl || !payloadUrl) {
2744
- initialUrl = new URL(window.location.href);
2745
- payloadUrl = new URL(window.location.href);
2806
+ const currentPageUrl = page.get().url;
2807
+ initialUrl = hrefToUrl(currentPageUrl);
2808
+ payloadUrl = hrefToUrl(currentPageUrl);
2809
+ initialUrlWasAbsolute = urlHasProtocol(currentPageUrl);
2746
2810
  }
2747
2811
  const pageName = options.getPageName();
2748
2812
  const searchParams = payloadUrl.searchParams;
@@ -2754,14 +2818,14 @@ var useInfiniteScrollQueryString = (options) => {
2754
2818
  setTimeout(() => resolve());
2755
2819
  });
2756
2820
  }).finally(() => {
2757
- if (enabled && initialUrl && payloadUrl && initialUrl.href !== payloadUrl.href) {
2821
+ if (enabled && initialUrl && payloadUrl && initialUrl.href !== payloadUrl.href && initialUrlWasAbsolute !== null) {
2758
2822
  router.replace({
2759
- url: payloadUrl.toString(),
2823
+ url: urlToString(payloadUrl, initialUrlWasAbsolute),
2760
2824
  preserveScroll: true,
2761
2825
  preserveState: true
2762
2826
  });
2763
2827
  }
2764
- initialUrl = payloadUrl = null;
2828
+ initialUrl = payloadUrl = initialUrlWasAbsolute = null;
2765
2829
  });
2766
2830
  };
2767
2831
  const onItemIntersected = debounce((itemElement) => {