@inertiajs/core 2.3.2 → 2.3.4

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
@@ -153,6 +153,9 @@ var firePrefetchedEvent = (response, visit) => {
153
153
  var firePrefetchingEvent = (visit) => {
154
154
  return fireEvent("prefetching", { detail: { visit } });
155
155
  };
156
+ var fireFlashEvent = (flash) => {
157
+ return fireEvent("flash", { detail: { flash } });
158
+ };
156
159
 
157
160
  // src/history.ts
158
161
  var import_lodash_es3 = require("lodash-es");
@@ -583,6 +586,7 @@ var PrefetchedRequests = class {
583
586
  "onCancel",
584
587
  "onSuccess",
585
588
  "onError",
589
+ "onFlash",
586
590
  "onPrefetched",
587
591
  "onCancelToken",
588
592
  "onPrefetching",
@@ -788,7 +792,7 @@ function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets"
788
792
  const hasHash = href.toString().includes("#");
789
793
  const url = new URL(href.toString(), typeof window === "undefined" ? "http://localhost" : window.location.toString());
790
794
  if (hasDataForQueryString) {
791
- const parseOptions = { ignoreQueryPrefix: true, parseArrays: false };
795
+ const parseOptions = { ignoreQueryPrefix: true, arrayLimit: -1 };
792
796
  url.search = qs.stringify(
793
797
  { ...qs.parse(url.search, parseOptions), ...data },
794
798
  {
@@ -825,7 +829,7 @@ function isUrlMethodPair(href) {
825
829
  return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
826
830
  }
827
831
  function urlHasProtocol(url) {
828
- return /^[a-z][a-z0-9+.-]*:\/\//i.test(url);
832
+ return /^([a-z][a-z0-9+.-]*:)?\/\/[^/]/i.test(url);
829
833
  }
830
834
  function urlToString(url, absolute) {
831
835
  const urlObj = typeof url === "string" ? hrefToUrl(url) : url;
@@ -844,11 +848,13 @@ var CurrentPage = class {
844
848
  init({
845
849
  initialPage,
846
850
  swapComponent,
847
- resolveComponent
851
+ resolveComponent,
852
+ onFlash
848
853
  }) {
849
- this.page = initialPage;
854
+ this.page = { ...initialPage, flash: initialPage.flash ?? {} };
850
855
  this.swapComponent = swapComponent;
851
856
  this.resolveComponent = resolveComponent;
857
+ this.onFlashCallback = onFlash;
852
858
  return this;
853
859
  }
854
860
  set(page2, {
@@ -878,9 +884,10 @@ var CurrentPage = class {
878
884
  const location = !isServer2 ? window.location : new URL(page2.url);
879
885
  const scrollRegions = !isServer2 && preserveScroll ? Scroll.getScrollRegions() : [];
880
886
  replace = replace || isSameUrlWithoutHash(hrefToUrl(page2.url), location);
881
- return new Promise((resolve) => {
882
- replace ? history.replaceState(page2, () => resolve(null)) : history.pushState(page2, () => resolve(null));
883
- }).then(() => {
887
+ const pageForHistory = { ...page2, flash: {} };
888
+ return new Promise(
889
+ (resolve) => replace ? history.replaceState(pageForHistory, resolve) : history.pushState(pageForHistory, resolve)
890
+ ).then(() => {
884
891
  const isNewComponent = !this.isTheSame(page2);
885
892
  if (!isNewComponent && Object.keys(page2.props.errors || {}).length > 0) {
886
893
  viewTransition = false;
@@ -938,12 +945,19 @@ var CurrentPage = class {
938
945
  get() {
939
946
  return this.page;
940
947
  }
948
+ getWithoutFlashData() {
949
+ return { ...this.page, flash: {} };
950
+ }
941
951
  hasOnceProps() {
942
952
  return Object.keys(this.page.onceProps ?? {}).length > 0;
943
953
  }
944
954
  merge(data) {
945
955
  this.page = { ...this.page, ...data };
946
956
  }
957
+ setFlash(flash) {
958
+ this.page = { ...this.page, flash };
959
+ this.onFlashCallback?.(flash);
960
+ }
947
961
  setUrlHash(hash) {
948
962
  if (!this.page.url.includes(hash)) {
949
963
  this.page.url += hash;
@@ -1038,7 +1052,7 @@ var History = class {
1038
1052
  }
1039
1053
  remember(data, key) {
1040
1054
  this.replaceState({
1041
- ...page.get(),
1055
+ ...page.getWithoutFlashData(),
1042
1056
  rememberedState: {
1043
1057
  ...page.get()?.rememberedState ?? {},
1044
1058
  [key]: data
@@ -1268,7 +1282,7 @@ var EventHandler = class {
1268
1282
  if (state === null) {
1269
1283
  const url = hrefToUrl(page.get().url);
1270
1284
  url.hash = window.location.hash;
1271
- history.replaceState({ ...page.get(), url: url.href });
1285
+ history.replaceState({ ...page.getWithoutFlashData(), url: url.href });
1272
1286
  Scroll.reset();
1273
1287
  return;
1274
1288
  }
@@ -1386,7 +1400,12 @@ var InitialVisit = class {
1386
1400
  } else {
1387
1401
  Scroll.scrollToAnchor();
1388
1402
  }
1389
- fireNavigateEvent(page.get());
1403
+ const page2 = page.get();
1404
+ fireNavigateEvent(page2);
1405
+ const flash = page2.flash;
1406
+ if (Object.keys(flash).length > 0) {
1407
+ fireFlashEvent(flash);
1408
+ }
1390
1409
  });
1391
1410
  }
1392
1411
  };
@@ -1484,6 +1503,7 @@ var RequestParams = class _RequestParams {
1484
1503
  onCancel: this.wrapCallback(params, "onCancel"),
1485
1504
  onSuccess: this.wrapCallback(params, "onSuccess"),
1486
1505
  onError: this.wrapCallback(params, "onError"),
1506
+ onFlash: this.wrapCallback(params, "onFlash"),
1487
1507
  onCancelToken: this.wrapCallback(params, "onCancelToken"),
1488
1508
  onPrefetched: this.wrapCallback(params, "onPrefetched"),
1489
1509
  onPrefetching: this.wrapCallback(params, "onPrefetching")
@@ -1753,6 +1773,7 @@ var Response = class _Response {
1753
1773
  }
1754
1774
  await history.processQueue();
1755
1775
  history.preserveUrl = this.requestParams.all().preserveUrl;
1776
+ const previousFlash = page.get().flash;
1756
1777
  await this.setPage();
1757
1778
  const errors = page.get().props.errors || {};
1758
1779
  if (Object.keys(errors).length > 0) {
@@ -1764,6 +1785,11 @@ var Response = class _Response {
1764
1785
  if (!this.wasPrefetched) {
1765
1786
  router.flush(page.get().url);
1766
1787
  }
1788
+ const { flash } = page.get();
1789
+ if (Object.keys(flash).length > 0 && (!this.requestParams.isPartial() || !(0, import_lodash_es4.isEqual)(flash, previousFlash))) {
1790
+ fireFlashEvent(flash);
1791
+ this.requestParams.all().onFlash(flash);
1792
+ }
1767
1793
  fireSuccessEvent(page.get());
1768
1794
  await this.requestParams.all().onSuccess(page.get());
1769
1795
  history.preserveUrl = false;
@@ -1772,7 +1798,8 @@ var Response = class _Response {
1772
1798
  this.requestParams.merge(params);
1773
1799
  }
1774
1800
  getPageResponse() {
1775
- return this.response.data = this.getDataFromResponse(this.response.data);
1801
+ const data = this.getDataFromResponse(this.response.data);
1802
+ return this.response.data = { ...data, flash: data.flash ?? {} };
1776
1803
  }
1777
1804
  async handleNonInertiaResponse() {
1778
1805
  if (this.isLocationVisit()) {
@@ -1952,6 +1979,10 @@ var Response = class _Response {
1952
1979
  ...pageResponse.onceProps || {}
1953
1980
  };
1954
1981
  }
1982
+ pageResponse.flash = {
1983
+ ...page.get().flash,
1984
+ ...this.requestParams.isDeferredPropsRequest() ? {} : pageResponse.flash
1985
+ };
1955
1986
  }
1956
1987
  mergeOrMatchItems(existingItems, newItems, matchProp, matchPropsOn, shouldAppend = true) {
1957
1988
  const items = Array.isArray(existingItems) ? existingItems : [];
@@ -2175,12 +2206,14 @@ var Router = class {
2175
2206
  init({
2176
2207
  initialPage,
2177
2208
  resolveComponent,
2178
- swapComponent
2209
+ swapComponent,
2210
+ onFlash
2179
2211
  }) {
2180
2212
  page.init({
2181
2213
  initialPage,
2182
2214
  resolveComponent,
2183
- swapComponent
2215
+ swapComponent,
2216
+ onFlash
2184
2217
  });
2185
2218
  InitialVisit.handle();
2186
2219
  eventHandler.init();
@@ -2399,16 +2432,35 @@ var Router = class {
2399
2432
  push(params) {
2400
2433
  this.clientVisit(params);
2401
2434
  }
2435
+ flash(keyOrData, value) {
2436
+ const current = page.get().flash;
2437
+ let flash;
2438
+ if (typeof keyOrData === "function") {
2439
+ flash = keyOrData(current);
2440
+ } else if (typeof keyOrData === "string") {
2441
+ flash = { ...current, [keyOrData]: value };
2442
+ } else if (keyOrData && Object.keys(keyOrData).length) {
2443
+ flash = { ...current, ...keyOrData };
2444
+ } else {
2445
+ return;
2446
+ }
2447
+ page.setFlash(flash);
2448
+ if (Object.keys(flash).length) {
2449
+ fireFlashEvent(flash);
2450
+ }
2451
+ }
2402
2452
  clientVisit(params, { replace = false } = {}) {
2403
2453
  this.clientVisitQueue.add(() => this.performClientVisit(params, { replace }));
2404
2454
  }
2405
2455
  performClientVisit(params, { replace = false } = {}) {
2406
2456
  const current = page.get();
2407
2457
  const props = typeof params.props === "function" ? params.props(current.props) : params.props ?? current.props;
2408
- const { viewTransition, onError, onFinish, onSuccess, ...pageParams } = params;
2458
+ const flash = typeof params.flash === "function" ? params.flash(current.flash) : params.flash;
2459
+ const { viewTransition, onError, onFinish, onFlash, onSuccess, ...pageParams } = params;
2409
2460
  const page2 = {
2410
2461
  ...current,
2411
2462
  ...pageParams,
2463
+ flash: flash ?? {},
2412
2464
  props
2413
2465
  };
2414
2466
  const preserveScroll = RequestParams.resolvePreserveOption(params.preserveScroll ?? false, page2);
@@ -2419,6 +2471,11 @@ var Router = class {
2419
2471
  preserveState,
2420
2472
  viewTransition
2421
2473
  }).then(() => {
2474
+ const currentFlash = page.get().flash;
2475
+ if (Object.keys(currentFlash).length > 0) {
2476
+ fireFlashEvent(currentFlash);
2477
+ onFlash?.(currentFlash);
2478
+ }
2422
2479
  const errors = page.get().props.errors || {};
2423
2480
  if (Object.keys(errors).length === 0) {
2424
2481
  onSuccess?.(page.get());
@@ -2512,6 +2569,8 @@ var Router = class {
2512
2569
  }),
2513
2570
  onError: options.onError || (() => {
2514
2571
  }),
2572
+ onFlash: options.onFlash || (() => {
2573
+ }),
2515
2574
  onPrefetched: options.onPrefetched || (() => {
2516
2575
  }),
2517
2576
  onPrefetching: options.onPrefetching || (() => {
@@ -2972,7 +3031,7 @@ var useInfiniteScrollData = (options) => {
2972
3031
  if (typeof window !== "undefined") {
2973
3032
  resetState();
2974
3033
  const rememberedState = router.restore(getRememberKey());
2975
- if (rememberedState && typeof rememberedState === "object") {
3034
+ if (rememberedState && typeof rememberedState === "object" && rememberedState.lastLoadedPage === getScrollPropFromCurrentPage().currentPage) {
2976
3035
  state.previousPage = rememberedState.previousPage;
2977
3036
  state.nextPage = rememberedState.nextPage;
2978
3037
  state.lastLoadedPage = rememberedState.lastLoadedPage;