@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.esm.js CHANGED
@@ -51,6 +51,9 @@ var firePrefetchingEvent = (visit) => {
51
51
  return fireEvent("prefetching", { detail: { visit } });
52
52
  };
53
53
 
54
+ // src/history.ts
55
+ import { isEqual } from "lodash-es";
56
+
54
57
  // src/sessionStorage.ts
55
58
  var SessionStorage = class {
56
59
  static set(key, value) {
@@ -260,18 +263,24 @@ var Scroll = class {
260
263
  }
261
264
  window.requestAnimationFrame(() => {
262
265
  this.restoreDocument();
263
- this.regions().forEach((region, index) => {
264
- const scrollPosition = scrollRegions[index];
265
- if (!scrollPosition) {
266
- return;
267
- }
268
- if (typeof region.scrollTo === "function") {
269
- region.scrollTo(scrollPosition.left, scrollPosition.top);
270
- } else {
271
- region.scrollTop = scrollPosition.top;
272
- region.scrollLeft = scrollPosition.left;
273
- }
274
- });
266
+ this.restoreScrollRegions(scrollRegions);
267
+ });
268
+ }
269
+ static restoreScrollRegions(scrollRegions) {
270
+ if (typeof window === "undefined") {
271
+ return;
272
+ }
273
+ this.regions().forEach((region, index) => {
274
+ const scrollPosition = scrollRegions[index];
275
+ if (!scrollPosition) {
276
+ return;
277
+ }
278
+ if (typeof region.scrollTo === "function") {
279
+ region.scrollTo(scrollPosition.left, scrollPosition.top);
280
+ } else {
281
+ region.scrollTop = scrollPosition.top;
282
+ region.scrollLeft = scrollPosition.left;
283
+ }
275
284
  });
276
285
  }
277
286
  static restoreDocument() {
@@ -352,7 +361,7 @@ var transformUrlAndData = (href, data, method, forceFormData, queryStringArrayFo
352
361
  };
353
362
  function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets") {
354
363
  const hasDataForQueryString = method === "get" && !isFormData(data) && Object.keys(data).length > 0;
355
- const hasHost = /^[a-z][a-z0-9+.-]*:\/\//i.test(href.toString());
364
+ const hasHost = urlHasProtocol(href.toString());
356
365
  const hasAbsolutePath = hasHost || href.toString().startsWith("/") || href.toString() === "";
357
366
  const hasRelativePath = !hasAbsolutePath && !href.toString().startsWith("#") && !href.toString().startsWith("?");
358
367
  const hasRelativePathWithDotPrefix = /^[.]{1,2}([/]|$)/.test(href.toString());
@@ -396,6 +405,13 @@ var isSameUrlWithoutHash = (url1, url2) => {
396
405
  function isUrlMethodPair(href) {
397
406
  return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
398
407
  }
408
+ function urlHasProtocol(url) {
409
+ return /^[a-z][a-z0-9+.-]*:\/\//i.test(url);
410
+ }
411
+ function urlToString(url, absolute) {
412
+ const urlObj = typeof url === "string" ? hrefToUrl(url) : url;
413
+ return absolute ? `${urlObj.protocol}//${urlObj.host}${urlObj.pathname}${urlObj.search}${urlObj.hash}` : `${urlObj.pathname}${urlObj.search}${urlObj.hash}`;
414
+ }
399
415
 
400
416
  // src/page.ts
401
417
  var CurrentPage = class {
@@ -434,7 +450,9 @@ var CurrentPage = class {
434
450
  return;
435
451
  }
436
452
  page2.rememberedState ?? (page2.rememberedState = {});
437
- const location = typeof window !== "undefined" ? window.location : new URL(page2.url);
453
+ const isServer2 = typeof window === "undefined";
454
+ const location = !isServer2 ? window.location : new URL(page2.url);
455
+ const scrollRegions = !isServer2 && preserveScroll ? history.getScrollRegions() : [];
438
456
  replace = replace || isSameUrlWithoutHash(hrefToUrl(page2.url), location);
439
457
  return new Promise((resolve) => {
440
458
  replace ? history.replaceState(page2, () => resolve(null)) : history.pushState(page2, () => resolve(null));
@@ -450,7 +468,9 @@ var CurrentPage = class {
450
468
  }
451
469
  this.isFirstPageLoad = false;
452
470
  return this.swap({ component, page: page2, preserveState }).then(() => {
453
- if (!preserveScroll) {
471
+ if (preserveScroll) {
472
+ window.requestAnimationFrame(() => Scroll.restoreScrollRegions(scrollRegions));
473
+ } else {
454
474
  Scroll.reset();
455
475
  }
456
476
  if (this.pendingDeferredProps && this.pendingDeferredProps.component === page2.component && this.pendingDeferredProps.url === page2.url) {
@@ -582,15 +602,13 @@ var History = class {
582
602
  this.current = page2;
583
603
  queue.add(() => {
584
604
  return this.getPageData(page2).then((data) => {
585
- const doPush = () => {
586
- this.doPushState({ page: data }, page2.url);
587
- cb && cb();
588
- };
605
+ const doPush = () => this.doPushState({ page: data }, page2.url).then(() => cb?.());
589
606
  if (isChromeIOS) {
590
- setTimeout(doPush);
591
- } else {
592
- doPush();
607
+ return new Promise((resolve) => {
608
+ setTimeout(() => doPush().then(resolve));
609
+ });
593
610
  }
611
+ return doPush();
594
612
  });
595
613
  });
596
614
  }
@@ -628,7 +646,10 @@ var History = class {
628
646
  if (!window.history.state?.page) {
629
647
  return;
630
648
  }
631
- this.doReplaceState({
649
+ if (isEqual(this.getScrollRegions(), scrollRegions)) {
650
+ return;
651
+ }
652
+ return this.doReplaceState({
632
653
  page: window.history.state.page,
633
654
  scrollRegions
634
655
  });
@@ -641,7 +662,10 @@ var History = class {
641
662
  if (!window.history.state?.page) {
642
663
  return;
643
664
  }
644
- this.doReplaceState({
665
+ if (isEqual(this.getDocumentScrollPosition(), scrollRegion)) {
666
+ return;
667
+ }
668
+ return this.doReplaceState({
645
669
  page: window.history.state.page,
646
670
  documentScrollPosition: scrollRegion
647
671
  });
@@ -666,31 +690,31 @@ var History = class {
666
690
  this.current = page2;
667
691
  queue.add(() => {
668
692
  return this.getPageData(page2).then((data) => {
669
- const doReplace = () => {
670
- this.doReplaceState({ page: data }, page2.url);
671
- cb && cb();
672
- };
693
+ const doReplace = () => this.doReplaceState({ page: data }, page2.url).then(() => cb?.());
673
694
  if (isChromeIOS) {
674
- setTimeout(doReplace);
675
- } else {
676
- doReplace();
695
+ return new Promise((resolve) => {
696
+ setTimeout(() => doReplace().then(resolve));
697
+ });
677
698
  }
699
+ return doReplace();
678
700
  });
679
701
  });
680
702
  }
681
703
  doReplaceState(data, url) {
682
- window.history.replaceState(
683
- {
684
- ...data,
685
- scrollRegions: data.scrollRegions ?? window.history.state?.scrollRegions,
686
- documentScrollPosition: data.documentScrollPosition ?? window.history.state?.documentScrollPosition
687
- },
688
- "",
689
- url
704
+ return Promise.resolve().then(
705
+ () => window.history.replaceState(
706
+ {
707
+ ...data,
708
+ scrollRegions: data.scrollRegions ?? window.history.state?.scrollRegions,
709
+ documentScrollPosition: data.documentScrollPosition ?? window.history.state?.documentScrollPosition
710
+ },
711
+ "",
712
+ url
713
+ )
690
714
  );
691
715
  }
692
716
  doPushState(data, url) {
693
- window.history.pushState(data, "", url);
717
+ return Promise.resolve().then(() => window.history.pushState(data, "", url));
694
718
  }
695
719
  getState(key, defaultValue) {
696
720
  return this.current?.[key] ?? defaultValue;
@@ -2174,10 +2198,45 @@ var elementInViewport = (el) => {
2174
2198
  return verticallyVisible && horizontallyVisible;
2175
2199
  };
2176
2200
  var getScrollableParent = (element) => {
2201
+ const allowsVerticalScroll = (el) => {
2202
+ const computedStyle = window.getComputedStyle(el);
2203
+ if (["scroll", "overlay"].includes(computedStyle.overflowY)) {
2204
+ return true;
2205
+ }
2206
+ if (computedStyle.overflowY !== "auto") {
2207
+ return false;
2208
+ }
2209
+ if (["visible", "clip"].includes(computedStyle.overflowX)) {
2210
+ return true;
2211
+ }
2212
+ return hasDimensionConstraint(computedStyle.maxHeight, el.style.height);
2213
+ };
2214
+ const allowsHorizontalScroll = (el) => {
2215
+ const computedStyle = window.getComputedStyle(el);
2216
+ if (["scroll", "overlay"].includes(computedStyle.overflowX)) {
2217
+ return true;
2218
+ }
2219
+ if (computedStyle.overflowX !== "auto") {
2220
+ return false;
2221
+ }
2222
+ if (["visible", "clip"].includes(computedStyle.overflowY)) {
2223
+ return true;
2224
+ }
2225
+ return hasDimensionConstraint(computedStyle.maxWidth, el.style.width);
2226
+ };
2227
+ const hasDimensionConstraint = (computedMaxDimension, inlineStyleDimension) => {
2228
+ if (computedMaxDimension && computedMaxDimension !== "none" && computedMaxDimension !== "0px") {
2229
+ return true;
2230
+ }
2231
+ if (inlineStyleDimension && inlineStyleDimension !== "auto" && inlineStyleDimension !== "0") {
2232
+ return true;
2233
+ }
2234
+ return false;
2235
+ };
2177
2236
  let parent = element?.parentElement;
2178
2237
  while (parent) {
2179
- const overflowY = window.getComputedStyle(parent).overflowY;
2180
- if (overflowY === "auto" || overflowY === "scroll") {
2238
+ const allowsScroll = allowsVerticalScroll(parent) || allowsHorizontalScroll(parent);
2239
+ if (window.getComputedStyle(parent).display !== "contents" && allowsScroll) {
2181
2240
  return parent;
2182
2241
  }
2183
2242
  parent = parent.parentElement;
@@ -2679,6 +2738,7 @@ var useInfiniteScrollElementManager = (options) => {
2679
2738
  var queue3 = new Queue();
2680
2739
  var initialUrl;
2681
2740
  var payloadUrl;
2741
+ var initialUrlWasAbsolute = null;
2682
2742
  var useInfiniteScrollQueryString = (options) => {
2683
2743
  let enabled = true;
2684
2744
  const queuePageUpdate = (page2) => {
@@ -2689,8 +2749,10 @@ var useInfiniteScrollQueryString = (options) => {
2689
2749
  return resolve();
2690
2750
  }
2691
2751
  if (!initialUrl || !payloadUrl) {
2692
- initialUrl = new URL(window.location.href);
2693
- payloadUrl = new URL(window.location.href);
2752
+ const currentPageUrl = page.get().url;
2753
+ initialUrl = hrefToUrl(currentPageUrl);
2754
+ payloadUrl = hrefToUrl(currentPageUrl);
2755
+ initialUrlWasAbsolute = urlHasProtocol(currentPageUrl);
2694
2756
  }
2695
2757
  const pageName = options.getPageName();
2696
2758
  const searchParams = payloadUrl.searchParams;
@@ -2702,14 +2764,14 @@ var useInfiniteScrollQueryString = (options) => {
2702
2764
  setTimeout(() => resolve());
2703
2765
  });
2704
2766
  }).finally(() => {
2705
- if (enabled && initialUrl && payloadUrl && initialUrl.href !== payloadUrl.href) {
2767
+ if (enabled && initialUrl && payloadUrl && initialUrl.href !== payloadUrl.href && initialUrlWasAbsolute !== null) {
2706
2768
  router.replace({
2707
- url: payloadUrl.toString(),
2769
+ url: urlToString(payloadUrl, initialUrlWasAbsolute),
2708
2770
  preserveScroll: true,
2709
2771
  preserveState: true
2710
2772
  });
2711
2773
  }
2712
- initialUrl = payloadUrl = null;
2774
+ initialUrl = payloadUrl = initialUrlWasAbsolute = null;
2713
2775
  });
2714
2776
  };
2715
2777
  const onItemIntersected = debounce((itemElement) => {
@@ -3398,6 +3460,8 @@ export {
3398
3460
  setupProgress,
3399
3461
  shouldIntercept,
3400
3462
  shouldNavigate,
3463
+ urlHasProtocol,
3464
+ urlToString,
3401
3465
  urlWithoutHash,
3402
3466
  useInfiniteScroll
3403
3467
  };