@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.esm.js CHANGED
@@ -96,6 +96,9 @@ var firePrefetchedEvent = (response, visit) => {
96
96
  var firePrefetchingEvent = (visit) => {
97
97
  return fireEvent("prefetching", { detail: { visit } });
98
98
  };
99
+ var fireFlashEvent = (flash) => {
100
+ return fireEvent("flash", { detail: { flash } });
101
+ };
99
102
 
100
103
  // src/history.ts
101
104
  import { cloneDeep as cloneDeep2, isEqual } from "lodash-es";
@@ -526,6 +529,7 @@ var PrefetchedRequests = class {
526
529
  "onCancel",
527
530
  "onSuccess",
528
531
  "onError",
532
+ "onFlash",
529
533
  "onPrefetched",
530
534
  "onCancelToken",
531
535
  "onPrefetching",
@@ -731,7 +735,7 @@ function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets"
731
735
  const hasHash = href.toString().includes("#");
732
736
  const url = new URL(href.toString(), typeof window === "undefined" ? "http://localhost" : window.location.toString());
733
737
  if (hasDataForQueryString) {
734
- const parseOptions = { ignoreQueryPrefix: true, parseArrays: false };
738
+ const parseOptions = { ignoreQueryPrefix: true, arrayLimit: -1 };
735
739
  url.search = qs.stringify(
736
740
  { ...qs.parse(url.search, parseOptions), ...data },
737
741
  {
@@ -768,7 +772,7 @@ function isUrlMethodPair(href) {
768
772
  return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
769
773
  }
770
774
  function urlHasProtocol(url) {
771
- return /^[a-z][a-z0-9+.-]*:\/\//i.test(url);
775
+ return /^([a-z][a-z0-9+.-]*:)?\/\/[^/]/i.test(url);
772
776
  }
773
777
  function urlToString(url, absolute) {
774
778
  const urlObj = typeof url === "string" ? hrefToUrl(url) : url;
@@ -787,11 +791,13 @@ var CurrentPage = class {
787
791
  init({
788
792
  initialPage,
789
793
  swapComponent,
790
- resolveComponent
794
+ resolveComponent,
795
+ onFlash
791
796
  }) {
792
- this.page = initialPage;
797
+ this.page = { ...initialPage, flash: initialPage.flash ?? {} };
793
798
  this.swapComponent = swapComponent;
794
799
  this.resolveComponent = resolveComponent;
800
+ this.onFlashCallback = onFlash;
795
801
  return this;
796
802
  }
797
803
  set(page2, {
@@ -821,9 +827,10 @@ var CurrentPage = class {
821
827
  const location = !isServer2 ? window.location : new URL(page2.url);
822
828
  const scrollRegions = !isServer2 && preserveScroll ? Scroll.getScrollRegions() : [];
823
829
  replace = replace || isSameUrlWithoutHash(hrefToUrl(page2.url), location);
824
- return new Promise((resolve) => {
825
- replace ? history.replaceState(page2, () => resolve(null)) : history.pushState(page2, () => resolve(null));
826
- }).then(() => {
830
+ const pageForHistory = { ...page2, flash: {} };
831
+ return new Promise(
832
+ (resolve) => replace ? history.replaceState(pageForHistory, resolve) : history.pushState(pageForHistory, resolve)
833
+ ).then(() => {
827
834
  const isNewComponent = !this.isTheSame(page2);
828
835
  if (!isNewComponent && Object.keys(page2.props.errors || {}).length > 0) {
829
836
  viewTransition = false;
@@ -881,12 +888,19 @@ var CurrentPage = class {
881
888
  get() {
882
889
  return this.page;
883
890
  }
891
+ getWithoutFlashData() {
892
+ return { ...this.page, flash: {} };
893
+ }
884
894
  hasOnceProps() {
885
895
  return Object.keys(this.page.onceProps ?? {}).length > 0;
886
896
  }
887
897
  merge(data) {
888
898
  this.page = { ...this.page, ...data };
889
899
  }
900
+ setFlash(flash) {
901
+ this.page = { ...this.page, flash };
902
+ this.onFlashCallback?.(flash);
903
+ }
890
904
  setUrlHash(hash) {
891
905
  if (!this.page.url.includes(hash)) {
892
906
  this.page.url += hash;
@@ -981,7 +995,7 @@ var History = class {
981
995
  }
982
996
  remember(data, key) {
983
997
  this.replaceState({
984
- ...page.get(),
998
+ ...page.getWithoutFlashData(),
985
999
  rememberedState: {
986
1000
  ...page.get()?.rememberedState ?? {},
987
1001
  [key]: data
@@ -1211,7 +1225,7 @@ var EventHandler = class {
1211
1225
  if (state === null) {
1212
1226
  const url = hrefToUrl(page.get().url);
1213
1227
  url.hash = window.location.hash;
1214
- history.replaceState({ ...page.get(), url: url.href });
1228
+ history.replaceState({ ...page.getWithoutFlashData(), url: url.href });
1215
1229
  Scroll.reset();
1216
1230
  return;
1217
1231
  }
@@ -1329,7 +1343,12 @@ var InitialVisit = class {
1329
1343
  } else {
1330
1344
  Scroll.scrollToAnchor();
1331
1345
  }
1332
- fireNavigateEvent(page.get());
1346
+ const page2 = page.get();
1347
+ fireNavigateEvent(page2);
1348
+ const flash = page2.flash;
1349
+ if (Object.keys(flash).length > 0) {
1350
+ fireFlashEvent(flash);
1351
+ }
1333
1352
  });
1334
1353
  }
1335
1354
  };
@@ -1427,6 +1446,7 @@ var RequestParams = class _RequestParams {
1427
1446
  onCancel: this.wrapCallback(params, "onCancel"),
1428
1447
  onSuccess: this.wrapCallback(params, "onSuccess"),
1429
1448
  onError: this.wrapCallback(params, "onError"),
1449
+ onFlash: this.wrapCallback(params, "onFlash"),
1430
1450
  onCancelToken: this.wrapCallback(params, "onCancelToken"),
1431
1451
  onPrefetched: this.wrapCallback(params, "onPrefetched"),
1432
1452
  onPrefetching: this.wrapCallback(params, "onPrefetching")
@@ -1696,6 +1716,7 @@ var Response = class _Response {
1696
1716
  }
1697
1717
  await history.processQueue();
1698
1718
  history.preserveUrl = this.requestParams.all().preserveUrl;
1719
+ const previousFlash = page.get().flash;
1699
1720
  await this.setPage();
1700
1721
  const errors = page.get().props.errors || {};
1701
1722
  if (Object.keys(errors).length > 0) {
@@ -1707,6 +1728,11 @@ var Response = class _Response {
1707
1728
  if (!this.wasPrefetched) {
1708
1729
  router.flush(page.get().url);
1709
1730
  }
1731
+ const { flash } = page.get();
1732
+ if (Object.keys(flash).length > 0 && (!this.requestParams.isPartial() || !isEqual2(flash, previousFlash))) {
1733
+ fireFlashEvent(flash);
1734
+ this.requestParams.all().onFlash(flash);
1735
+ }
1710
1736
  fireSuccessEvent(page.get());
1711
1737
  await this.requestParams.all().onSuccess(page.get());
1712
1738
  history.preserveUrl = false;
@@ -1715,7 +1741,8 @@ var Response = class _Response {
1715
1741
  this.requestParams.merge(params);
1716
1742
  }
1717
1743
  getPageResponse() {
1718
- return this.response.data = this.getDataFromResponse(this.response.data);
1744
+ const data = this.getDataFromResponse(this.response.data);
1745
+ return this.response.data = { ...data, flash: data.flash ?? {} };
1719
1746
  }
1720
1747
  async handleNonInertiaResponse() {
1721
1748
  if (this.isLocationVisit()) {
@@ -1895,6 +1922,10 @@ var Response = class _Response {
1895
1922
  ...pageResponse.onceProps || {}
1896
1923
  };
1897
1924
  }
1925
+ pageResponse.flash = {
1926
+ ...page.get().flash,
1927
+ ...this.requestParams.isDeferredPropsRequest() ? {} : pageResponse.flash
1928
+ };
1898
1929
  }
1899
1930
  mergeOrMatchItems(existingItems, newItems, matchProp, matchPropsOn, shouldAppend = true) {
1900
1931
  const items = Array.isArray(existingItems) ? existingItems : [];
@@ -2118,12 +2149,14 @@ var Router = class {
2118
2149
  init({
2119
2150
  initialPage,
2120
2151
  resolveComponent,
2121
- swapComponent
2152
+ swapComponent,
2153
+ onFlash
2122
2154
  }) {
2123
2155
  page.init({
2124
2156
  initialPage,
2125
2157
  resolveComponent,
2126
- swapComponent
2158
+ swapComponent,
2159
+ onFlash
2127
2160
  });
2128
2161
  InitialVisit.handle();
2129
2162
  eventHandler.init();
@@ -2342,16 +2375,35 @@ var Router = class {
2342
2375
  push(params) {
2343
2376
  this.clientVisit(params);
2344
2377
  }
2378
+ flash(keyOrData, value) {
2379
+ const current = page.get().flash;
2380
+ let flash;
2381
+ if (typeof keyOrData === "function") {
2382
+ flash = keyOrData(current);
2383
+ } else if (typeof keyOrData === "string") {
2384
+ flash = { ...current, [keyOrData]: value };
2385
+ } else if (keyOrData && Object.keys(keyOrData).length) {
2386
+ flash = { ...current, ...keyOrData };
2387
+ } else {
2388
+ return;
2389
+ }
2390
+ page.setFlash(flash);
2391
+ if (Object.keys(flash).length) {
2392
+ fireFlashEvent(flash);
2393
+ }
2394
+ }
2345
2395
  clientVisit(params, { replace = false } = {}) {
2346
2396
  this.clientVisitQueue.add(() => this.performClientVisit(params, { replace }));
2347
2397
  }
2348
2398
  performClientVisit(params, { replace = false } = {}) {
2349
2399
  const current = page.get();
2350
2400
  const props = typeof params.props === "function" ? params.props(current.props) : params.props ?? current.props;
2351
- const { viewTransition, onError, onFinish, onSuccess, ...pageParams } = params;
2401
+ const flash = typeof params.flash === "function" ? params.flash(current.flash) : params.flash;
2402
+ const { viewTransition, onError, onFinish, onFlash, onSuccess, ...pageParams } = params;
2352
2403
  const page2 = {
2353
2404
  ...current,
2354
2405
  ...pageParams,
2406
+ flash: flash ?? {},
2355
2407
  props
2356
2408
  };
2357
2409
  const preserveScroll = RequestParams.resolvePreserveOption(params.preserveScroll ?? false, page2);
@@ -2362,6 +2414,11 @@ var Router = class {
2362
2414
  preserveState,
2363
2415
  viewTransition
2364
2416
  }).then(() => {
2417
+ const currentFlash = page.get().flash;
2418
+ if (Object.keys(currentFlash).length > 0) {
2419
+ fireFlashEvent(currentFlash);
2420
+ onFlash?.(currentFlash);
2421
+ }
2365
2422
  const errors = page.get().props.errors || {};
2366
2423
  if (Object.keys(errors).length === 0) {
2367
2424
  onSuccess?.(page.get());
@@ -2455,6 +2512,8 @@ var Router = class {
2455
2512
  }),
2456
2513
  onError: options.onError || (() => {
2457
2514
  }),
2515
+ onFlash: options.onFlash || (() => {
2516
+ }),
2458
2517
  onPrefetched: options.onPrefetched || (() => {
2459
2518
  }),
2460
2519
  onPrefetching: options.onPrefetching || (() => {
@@ -2915,7 +2974,7 @@ var useInfiniteScrollData = (options) => {
2915
2974
  if (typeof window !== "undefined") {
2916
2975
  resetState();
2917
2976
  const rememberedState = router.restore(getRememberKey());
2918
- if (rememberedState && typeof rememberedState === "object") {
2977
+ if (rememberedState && typeof rememberedState === "object" && rememberedState.lastLoadedPage === getScrollPropFromCurrentPage().currentPage) {
2919
2978
  state.previousPage = rememberedState.previousPage;
2920
2979
  state.nextPage = rememberedState.nextPage;
2921
2980
  state.lastLoadedPage = rememberedState.lastLoadedPage;