@inertiajs/core 2.3.8 → 2.3.10

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
@@ -575,7 +575,116 @@ var PrefetchedRequests = class {
575
575
  };
576
576
  var prefetchedRequests = new PrefetchedRequests();
577
577
 
578
+ // src/domUtils.ts
579
+ var elementInViewport = (el) => {
580
+ if (el.offsetParent === null) {
581
+ return false;
582
+ }
583
+ const rect = el.getBoundingClientRect();
584
+ const verticallyVisible = rect.top < window.innerHeight && rect.bottom >= 0;
585
+ const horizontallyVisible = rect.left < window.innerWidth && rect.right >= 0;
586
+ return verticallyVisible && horizontallyVisible;
587
+ };
588
+ var getScrollableParent = (element) => {
589
+ const allowsVerticalScroll = (el) => {
590
+ const computedStyle = window.getComputedStyle(el);
591
+ if (["scroll", "overlay"].includes(computedStyle.overflowY)) {
592
+ return true;
593
+ }
594
+ if (computedStyle.overflowY !== "auto") {
595
+ return false;
596
+ }
597
+ if (["visible", "clip"].includes(computedStyle.overflowX)) {
598
+ return true;
599
+ }
600
+ return hasDimensionConstraint(computedStyle.maxHeight, el.style.height);
601
+ };
602
+ const allowsHorizontalScroll = (el) => {
603
+ const computedStyle = window.getComputedStyle(el);
604
+ if (["scroll", "overlay"].includes(computedStyle.overflowX)) {
605
+ return true;
606
+ }
607
+ if (computedStyle.overflowX !== "auto") {
608
+ return false;
609
+ }
610
+ if (["visible", "clip"].includes(computedStyle.overflowY)) {
611
+ return true;
612
+ }
613
+ return hasDimensionConstraint(computedStyle.maxWidth, el.style.width);
614
+ };
615
+ const hasDimensionConstraint = (computedMaxDimension, inlineStyleDimension) => {
616
+ if (computedMaxDimension && computedMaxDimension !== "none" && computedMaxDimension !== "0px") {
617
+ return true;
618
+ }
619
+ if (inlineStyleDimension && inlineStyleDimension !== "auto" && inlineStyleDimension !== "0") {
620
+ return true;
621
+ }
622
+ return false;
623
+ };
624
+ let parent = element?.parentElement;
625
+ while (parent) {
626
+ const allowsScroll = allowsVerticalScroll(parent) || allowsHorizontalScroll(parent);
627
+ if (window.getComputedStyle(parent).display !== "contents" && allowsScroll) {
628
+ return parent;
629
+ }
630
+ parent = parent.parentElement;
631
+ }
632
+ return null;
633
+ };
634
+ var getElementsInViewportFromCollection = (elements, referenceElement) => {
635
+ if (!referenceElement) {
636
+ return elements.filter((element) => elementInViewport(element));
637
+ }
638
+ const referenceIndex = elements.indexOf(referenceElement);
639
+ const upwardElements = [];
640
+ const downwardElements = [];
641
+ for (let i = referenceIndex; i >= 0; i--) {
642
+ const element = elements[i];
643
+ if (elementInViewport(element)) {
644
+ upwardElements.push(element);
645
+ } else {
646
+ break;
647
+ }
648
+ }
649
+ for (let i = referenceIndex + 1; i < elements.length; i++) {
650
+ const element = elements[i];
651
+ if (elementInViewport(element)) {
652
+ downwardElements.push(element);
653
+ } else {
654
+ break;
655
+ }
656
+ }
657
+ return [...upwardElements.reverse(), ...downwardElements];
658
+ };
659
+ var requestAnimationFrame = (cb, times = 1) => {
660
+ window.requestAnimationFrame(() => {
661
+ if (times > 1) {
662
+ requestAnimationFrame(cb, times - 1);
663
+ } else {
664
+ cb();
665
+ }
666
+ });
667
+ };
668
+ var getInitialPageFromDOM = (id, useScriptElement = false) => {
669
+ if (typeof window === "undefined") {
670
+ return null;
671
+ }
672
+ if (!useScriptElement) {
673
+ const el = document.getElementById(id);
674
+ if (el?.dataset.page) {
675
+ return JSON.parse(el.dataset.page);
676
+ }
677
+ }
678
+ const scriptEl = document.querySelector(`script[data-page="${id}"][type="application/json"]`);
679
+ if (scriptEl?.textContent) {
680
+ return JSON.parse(scriptEl.textContent);
681
+ }
682
+ return null;
683
+ };
684
+
578
685
  // src/scroll.ts
686
+ var isServer = typeof window === "undefined";
687
+ var isFirefox = !isServer && /Firefox/i.test(window.navigator.userAgent);
579
688
  var Scroll = class {
580
689
  static save() {
581
690
  history.saveScrollPositions(this.getScrollRegions());
@@ -589,10 +698,16 @@ var Scroll = class {
589
698
  static regions() {
590
699
  return document.querySelectorAll("[scroll-region]");
591
700
  }
701
+ static scrollToTop() {
702
+ if (isFirefox && getComputedStyle(document.documentElement).scrollBehavior === "smooth") {
703
+ return requestAnimationFrame(() => window.scrollTo(0, 0), 2);
704
+ }
705
+ window.scrollTo(0, 0);
706
+ }
592
707
  static reset() {
593
- const anchorHash = typeof window !== "undefined" ? window.location.hash : null;
708
+ const anchorHash = isServer ? null : window.location.hash;
594
709
  if (!anchorHash) {
595
- window.scrollTo(0, 0);
710
+ this.scrollToTop();
596
711
  }
597
712
  this.regions().forEach((region) => {
598
713
  if (typeof region.scrollTo === "function") {
@@ -606,16 +721,16 @@ var Scroll = class {
606
721
  this.scrollToAnchor();
607
722
  }
608
723
  static scrollToAnchor() {
609
- const anchorHash = typeof window !== "undefined" ? window.location.hash : null;
724
+ const anchorHash = isServer ? null : window.location.hash;
610
725
  if (anchorHash) {
611
726
  setTimeout(() => {
612
727
  const anchorElement = document.getElementById(anchorHash.slice(1));
613
- anchorElement ? anchorElement.scrollIntoView() : window.scrollTo(0, 0);
728
+ anchorElement ? anchorElement.scrollIntoView() : this.scrollToTop();
614
729
  });
615
730
  }
616
731
  }
617
732
  static restore(scrollRegions) {
618
- if (typeof window === "undefined") {
733
+ if (isServer) {
619
734
  return;
620
735
  }
621
736
  window.requestAnimationFrame(() => {
@@ -624,7 +739,7 @@ var Scroll = class {
624
739
  });
625
740
  }
626
741
  static restoreScrollRegions(scrollRegions) {
627
- if (typeof window === "undefined") {
742
+ if (isServer) {
628
743
  return;
629
744
  }
630
745
  this.regions().forEach((region, index) => {
@@ -817,6 +932,9 @@ var CurrentPage = class {
817
932
  component: page2.component,
818
933
  url: page2.url
819
934
  };
935
+ if (page2.initialDeferredProps === void 0) {
936
+ page2.initialDeferredProps = page2.deferredProps;
937
+ }
820
938
  }
821
939
  this.componentId = {};
822
940
  const componentId = this.componentId;
@@ -828,9 +946,9 @@ var CurrentPage = class {
828
946
  return;
829
947
  }
830
948
  page2.rememberedState ?? (page2.rememberedState = {});
831
- const isServer2 = typeof window === "undefined";
832
- const location = !isServer2 ? window.location : new URL(page2.url);
833
- const scrollRegions = !isServer2 && preserveScroll ? Scroll.getScrollRegions() : [];
949
+ const isServer3 = typeof window === "undefined";
950
+ const location = !isServer3 ? window.location : new URL(page2.url);
951
+ const scrollRegions = !isServer3 && preserveScroll ? Scroll.getScrollRegions() : [];
834
952
  replace = replace || isSameUrlWithoutHash(hrefToUrl(page2.url), location);
835
953
  const pageForHistory = { ...page2, flash: {} };
836
954
  return new Promise(
@@ -990,9 +1108,9 @@ var Queue = class {
990
1108
  };
991
1109
 
992
1110
  // src/history.ts
993
- var isServer = typeof window === "undefined";
1111
+ var isServer2 = typeof window === "undefined";
994
1112
  var queue = new Queue();
995
- var isChromeIOS = !isServer && /CriOS/.test(window.navigator.userAgent);
1113
+ var isChromeIOS = !isServer2 && /CriOS/.test(window.navigator.userAgent);
996
1114
  var History = class {
997
1115
  constructor() {
998
1116
  this.rememberedState = "rememberedState";
@@ -1012,12 +1130,12 @@ var History = class {
1012
1130
  });
1013
1131
  }
1014
1132
  restore(key) {
1015
- if (!isServer) {
1133
+ if (!isServer2) {
1016
1134
  return this.current[this.rememberedState]?.[key] !== void 0 ? this.current[this.rememberedState]?.[key] : this.initialState?.[this.rememberedState]?.[key];
1017
1135
  }
1018
1136
  }
1019
1137
  pushState(page2, cb = null) {
1020
- if (isServer) {
1138
+ if (isServer2) {
1021
1139
  return;
1022
1140
  }
1023
1141
  if (this.preserveUrl) {
@@ -1058,7 +1176,7 @@ var History = class {
1058
1176
  return queue.process();
1059
1177
  }
1060
1178
  decrypt(page2 = null) {
1061
- if (isServer) {
1179
+ if (isServer2) {
1062
1180
  return Promise.resolve(page2 ?? page.get());
1063
1181
  }
1064
1182
  const pageData = page2 ?? window.history.state?.page;
@@ -1121,7 +1239,7 @@ var History = class {
1121
1239
  return;
1122
1240
  }
1123
1241
  page.merge(page2);
1124
- if (isServer) {
1242
+ if (isServer2) {
1125
1243
  return;
1126
1244
  }
1127
1245
  if (this.preserveUrl) {
@@ -1199,7 +1317,7 @@ var History = class {
1199
1317
  }
1200
1318
  }
1201
1319
  browserHasHistoryEntry() {
1202
- return !isServer && !!window.history.state?.page;
1320
+ return !isServer2 && !!window.history.state?.page;
1203
1321
  }
1204
1322
  clear() {
1205
1323
  SessionStorage.remove(historySessionStorageKeys.key);
@@ -1277,10 +1395,21 @@ var EventHandler = class {
1277
1395
  this.onMissingHistoryItem();
1278
1396
  return;
1279
1397
  }
1280
- router.cancelAll();
1398
+ router.cancelAll({ prefetch: false });
1281
1399
  page.setQuietly(data, { preserveState: false }).then(() => {
1282
1400
  Scroll.restore(history.getScrollRegions());
1283
1401
  fireNavigateEvent(page.get());
1402
+ const pendingDeferred = {};
1403
+ const pageProps = page.get().props;
1404
+ for (const [group, props] of Object.entries(data.initialDeferredProps ?? data.deferredProps ?? {})) {
1405
+ const missing = props.filter((prop) => pageProps[prop] === void 0);
1406
+ if (missing.length > 0) {
1407
+ pendingDeferred[group] = missing;
1408
+ }
1409
+ }
1410
+ if (Object.keys(pendingDeferred).length > 0) {
1411
+ this.fireInternalEvent("loadDeferredProps", pendingDeferred);
1412
+ }
1284
1413
  });
1285
1414
  }).catch(() => {
1286
1415
  this.onMissingHistoryItem();
@@ -1513,6 +1642,9 @@ var RequestParams = class _RequestParams {
1513
1642
  isPartial() {
1514
1643
  return this.params.only.length > 0 || this.params.except.length > 0 || this.params.reset.length > 0;
1515
1644
  }
1645
+ isPrefetch() {
1646
+ return this.params.prefetch === true;
1647
+ }
1516
1648
  isDeferredPropsRequest() {
1517
1649
  return this.params.deferredProps === true;
1518
1650
  }
@@ -1782,7 +1914,10 @@ var Response = class _Response {
1782
1914
  }
1783
1915
  getPageResponse() {
1784
1916
  const data = this.getDataFromResponse(this.response.data);
1785
- return this.response.data = { ...data, flash: data.flash ?? {} };
1917
+ if (typeof data === "object") {
1918
+ return this.response.data = { ...data, flash: data.flash ?? {} };
1919
+ }
1920
+ return this.response.data = data;
1786
1921
  }
1787
1922
  async handleNonInertiaResponse() {
1788
1923
  if (this.isLocationVisit()) {
@@ -1966,6 +2101,10 @@ var Response = class _Response {
1966
2101
  ...page.get().flash,
1967
2102
  ...this.requestParams.isDeferredPropsRequest() ? {} : pageResponse.flash
1968
2103
  };
2104
+ const currentOriginalDeferred = page.get().initialDeferredProps;
2105
+ if (currentOriginalDeferred && Object.keys(currentOriginalDeferred).length > 0) {
2106
+ pageResponse.initialDeferredProps = currentOriginalDeferred;
2107
+ }
1969
2108
  }
1970
2109
  mergeOrMatchItems(existingItems, newItems, matchProp, matchPropsOn, shouldAppend = true) {
1971
2110
  const items = Array.isArray(existingItems) ? existingItems : [];
@@ -2039,6 +2178,9 @@ var Request = class _Request {
2039
2178
  static create(params, page2) {
2040
2179
  return new _Request(params, page2);
2041
2180
  }
2181
+ isPrefetch() {
2182
+ return this.requestParams.isPrefetch();
2183
+ }
2042
2184
  async send() {
2043
2185
  this.requestParams.onCancelToken(() => this.cancel({ cancelled: true }));
2044
2186
  fireStartEvent(this.requestParams.all());
@@ -2155,20 +2297,17 @@ var RequestStream = class {
2155
2297
  interruptInFlight() {
2156
2298
  this.cancel({ interrupted: true }, false);
2157
2299
  }
2158
- cancelInFlight() {
2159
- this.cancel({ cancelled: true }, true);
2300
+ cancelInFlight({ prefetch = true } = {}) {
2301
+ this.requests.filter((request) => prefetch || !request.isPrefetch()).forEach((request) => request.cancel({ cancelled: true }));
2160
2302
  }
2161
- cancel({ cancelled = false, interrupted = false } = {}, force) {
2162
- if (!this.shouldCancel(force)) {
2303
+ cancel({ cancelled = false, interrupted = false } = {}, force = false) {
2304
+ if (!force && !this.shouldCancel()) {
2163
2305
  return;
2164
2306
  }
2165
2307
  const request = this.requests.shift();
2166
- request?.cancel({ interrupted, cancelled });
2308
+ request?.cancel({ cancelled, interrupted });
2167
2309
  }
2168
- shouldCancel(force) {
2169
- if (force) {
2170
- return true;
2171
- }
2310
+ shouldCancel() {
2172
2311
  return this.interruptible && this.requests.length >= this.maxConcurrent;
2173
2312
  }
2174
2313
  };
@@ -2258,12 +2397,19 @@ var Router = class {
2258
2397
  }
2259
2398
  return eventHandler.onGlobalEvent(type, callback);
2260
2399
  }
2400
+ /**
2401
+ * @deprecated Use cancelAll() instead.
2402
+ */
2261
2403
  cancel() {
2262
2404
  this.syncRequestStream.cancelInFlight();
2263
2405
  }
2264
- cancelAll() {
2265
- this.asyncRequestStream.cancelInFlight();
2266
- this.syncRequestStream.cancelInFlight();
2406
+ cancelAll({ async = true, prefetch = true, sync = true } = {}) {
2407
+ if (async) {
2408
+ this.asyncRequestStream.cancelInFlight({ prefetch });
2409
+ }
2410
+ if (sync) {
2411
+ this.syncRequestStream.cancelInFlight();
2412
+ }
2267
2413
  }
2268
2414
  poll(interval, requestOptions = {}, options = {}) {
2269
2415
  return polls.add(interval, () => this.reload(requestOptions), {
@@ -2280,8 +2426,12 @@ var Router = class {
2280
2426
  if (events.onBefore(visit) === false || !fireBeforeEvent(visit)) {
2281
2427
  return;
2282
2428
  }
2283
- const requestStream = visit.async ? this.asyncRequestStream : this.syncRequestStream;
2284
- requestStream.interruptInFlight();
2429
+ if (!isSameUrlWithoutHash(visit.url, hrefToUrl(page.get().url))) {
2430
+ this.asyncRequestStream.cancelInFlight({ prefetch: false });
2431
+ }
2432
+ if (!visit.async) {
2433
+ this.syncRequestStream.interruptInFlight();
2434
+ }
2285
2435
  if (!page.isCleared() && !visit.preserveUrl) {
2286
2436
  Scroll.save();
2287
2437
  }
@@ -2295,6 +2445,7 @@ var Router = class {
2295
2445
  prefetchedRequests.use(prefetched, requestParams);
2296
2446
  } else {
2297
2447
  progress.reveal(true);
2448
+ const requestStream = visit.async ? this.asyncRequestStream : this.syncRequestStream;
2298
2449
  requestStream.send(Request.create(requestParams, page.get()));
2299
2450
  }
2300
2451
  }
@@ -2440,7 +2591,10 @@ var Router = class {
2440
2591
  }
2441
2592
  performClientVisit(params, { replace = false } = {}) {
2442
2593
  const current = page.get();
2443
- const props = typeof params.props === "function" ? params.props(current.props) : params.props ?? current.props;
2594
+ const onceProps = typeof params.props === "function" ? Object.fromEntries(
2595
+ Object.values(current.onceProps ?? {}).map((onceProp) => [onceProp.prop, current.props[onceProp.prop]])
2596
+ ) : {};
2597
+ const props = typeof params.props === "function" ? params.props(current.props, onceProps) : params.props ?? current.props;
2444
2598
  const flash = typeof params.flash === "function" ? params.flash(current.flash) : params.flash;
2445
2599
  const { viewTransition, onError, onFinish, onFlash, onSuccess, ...pageParams } = params;
2446
2600
  const page2 = {
@@ -2674,113 +2828,6 @@ var UseFormUtils = class {
2674
2828
  }
2675
2829
  };
2676
2830
 
2677
- // src/domUtils.ts
2678
- var elementInViewport = (el) => {
2679
- if (el.offsetParent === null) {
2680
- return false;
2681
- }
2682
- const rect = el.getBoundingClientRect();
2683
- const verticallyVisible = rect.top < window.innerHeight && rect.bottom >= 0;
2684
- const horizontallyVisible = rect.left < window.innerWidth && rect.right >= 0;
2685
- return verticallyVisible && horizontallyVisible;
2686
- };
2687
- var getScrollableParent = (element) => {
2688
- const allowsVerticalScroll = (el) => {
2689
- const computedStyle = window.getComputedStyle(el);
2690
- if (["scroll", "overlay"].includes(computedStyle.overflowY)) {
2691
- return true;
2692
- }
2693
- if (computedStyle.overflowY !== "auto") {
2694
- return false;
2695
- }
2696
- if (["visible", "clip"].includes(computedStyle.overflowX)) {
2697
- return true;
2698
- }
2699
- return hasDimensionConstraint(computedStyle.maxHeight, el.style.height);
2700
- };
2701
- const allowsHorizontalScroll = (el) => {
2702
- const computedStyle = window.getComputedStyle(el);
2703
- if (["scroll", "overlay"].includes(computedStyle.overflowX)) {
2704
- return true;
2705
- }
2706
- if (computedStyle.overflowX !== "auto") {
2707
- return false;
2708
- }
2709
- if (["visible", "clip"].includes(computedStyle.overflowY)) {
2710
- return true;
2711
- }
2712
- return hasDimensionConstraint(computedStyle.maxWidth, el.style.width);
2713
- };
2714
- const hasDimensionConstraint = (computedMaxDimension, inlineStyleDimension) => {
2715
- if (computedMaxDimension && computedMaxDimension !== "none" && computedMaxDimension !== "0px") {
2716
- return true;
2717
- }
2718
- if (inlineStyleDimension && inlineStyleDimension !== "auto" && inlineStyleDimension !== "0") {
2719
- return true;
2720
- }
2721
- return false;
2722
- };
2723
- let parent = element?.parentElement;
2724
- while (parent) {
2725
- const allowsScroll = allowsVerticalScroll(parent) || allowsHorizontalScroll(parent);
2726
- if (window.getComputedStyle(parent).display !== "contents" && allowsScroll) {
2727
- return parent;
2728
- }
2729
- parent = parent.parentElement;
2730
- }
2731
- return null;
2732
- };
2733
- var getElementsInViewportFromCollection = (elements, referenceElement) => {
2734
- if (!referenceElement) {
2735
- return elements.filter((element) => elementInViewport(element));
2736
- }
2737
- const referenceIndex = elements.indexOf(referenceElement);
2738
- const upwardElements = [];
2739
- const downwardElements = [];
2740
- for (let i = referenceIndex; i >= 0; i--) {
2741
- const element = elements[i];
2742
- if (elementInViewport(element)) {
2743
- upwardElements.push(element);
2744
- } else {
2745
- break;
2746
- }
2747
- }
2748
- for (let i = referenceIndex + 1; i < elements.length; i++) {
2749
- const element = elements[i];
2750
- if (elementInViewport(element)) {
2751
- downwardElements.push(element);
2752
- } else {
2753
- break;
2754
- }
2755
- }
2756
- return [...upwardElements.reverse(), ...downwardElements];
2757
- };
2758
- var requestAnimationFrame = (cb, times = 1) => {
2759
- window.requestAnimationFrame(() => {
2760
- if (times > 1) {
2761
- requestAnimationFrame(cb, times - 1);
2762
- } else {
2763
- cb();
2764
- }
2765
- });
2766
- };
2767
- var getInitialPageFromDOM = (id, useScriptElement = false) => {
2768
- if (typeof window === "undefined") {
2769
- return null;
2770
- }
2771
- if (!useScriptElement) {
2772
- const el = document.getElementById(id);
2773
- if (el?.dataset.page) {
2774
- return JSON.parse(el.dataset.page);
2775
- }
2776
- }
2777
- const scriptEl = document.querySelector(`script[data-page="${id}"][type="application/json"]`);
2778
- if (scriptEl?.textContent) {
2779
- return JSON.parse(scriptEl.textContent);
2780
- }
2781
- return null;
2782
- };
2783
-
2784
2831
  // src/formObject.ts
2785
2832
  import { get as get4, set as set4 } from "lodash-es";
2786
2833
  function undotKey(key) {
@@ -2917,7 +2964,7 @@ var Renderer = {
2917
2964
  sourceElements.forEach((element) => document.head.appendChild(element));
2918
2965
  }, 1)
2919
2966
  };
2920
- function createHeadManager(isServer2, titleCallback, onUpdate) {
2967
+ function createHeadManager(isServer3, titleCallback, onUpdate) {
2921
2968
  const states = {};
2922
2969
  let lastProviderId = 0;
2923
2970
  function connect() {
@@ -2969,7 +3016,7 @@ function createHeadManager(isServer2, titleCallback, onUpdate) {
2969
3016
  return Object.values(elements);
2970
3017
  }
2971
3018
  function commit() {
2972
- isServer2 ? onUpdate(collect()) : Renderer.update(collect());
3019
+ isServer3 ? onUpdate(collect()) : Renderer.update(collect());
2973
3020
  }
2974
3021
  commit();
2975
3022
  return {
@@ -3866,6 +3913,7 @@ function setupProgress({
3866
3913
  }
3867
3914
 
3868
3915
  // src/resetFormFields.ts
3916
+ var FormComponentResetSymbol = Symbol("FormComponentReset");
3869
3917
  function isFormElement(element) {
3870
3918
  return element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
3871
3919
  }
@@ -3996,13 +4044,16 @@ function resetFormFields(formElement, defaults, fieldNames) {
3996
4044
  }
3997
4045
  });
3998
4046
  if (hasChanged && resetEntireForm) {
3999
- formElement.dispatchEvent(new Event("reset", { bubbles: true }));
4047
+ formElement.dispatchEvent(
4048
+ new CustomEvent("reset", { bubbles: true, cancelable: true, detail: { [FormComponentResetSymbol]: true } })
4049
+ );
4000
4050
  }
4001
4051
  }
4002
4052
 
4003
4053
  // src/index.ts
4004
4054
  var router = new Router();
4005
4055
  export {
4056
+ FormComponentResetSymbol,
4006
4057
  UseFormUtils,
4007
4058
  config,
4008
4059
  createHeadManager,