@inertiajs/core 2.1.1 → 2.1.2

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
@@ -34,6 +34,7 @@ __export(index_exports, {
34
34
  formDataToObject: () => formDataToObject,
35
35
  hideProgress: () => hide,
36
36
  hrefToUrl: () => hrefToUrl,
37
+ isUrlMethodPair: () => isUrlMethodPair,
37
38
  mergeDataIntoQueryString: () => mergeDataIntoQueryString,
38
39
  objectToFormData: () => objectToFormData,
39
40
  resetFormFields: () => resetFormFields,
@@ -431,6 +432,9 @@ var setHashIfSameUrl = (originUrl, destinationUrl) => {
431
432
  var isSameUrlWithoutHash = (url1, url2) => {
432
433
  return urlWithoutHash(url1).href === urlWithoutHash(url2).href;
433
434
  };
435
+ function isUrlMethodPair(href) {
436
+ return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
437
+ }
434
438
 
435
439
  // src/page.ts
436
440
  var CurrentPage = class {
@@ -1052,7 +1056,7 @@ var PrefetchedRequests = class {
1052
1056
  this.removalTimers = [];
1053
1057
  this.currentUseId = null;
1054
1058
  }
1055
- add(params, sendFunc, { cacheFor }) {
1059
+ add(params, sendFunc, { cacheFor, cacheTags }) {
1056
1060
  const inFlight = this.findInFlight(params);
1057
1061
  if (inFlight) {
1058
1062
  return Promise.resolve();
@@ -1097,7 +1101,8 @@ var PrefetchedRequests = class {
1097
1101
  response: promise,
1098
1102
  singleUse: expires === 0,
1099
1103
  timestamp: Date.now(),
1100
- inFlight: false
1104
+ inFlight: false,
1105
+ tags: Array.isArray(cacheTags) ? cacheTags : [cacheTags]
1101
1106
  });
1102
1107
  this.scheduleForRemoval(params, expires);
1103
1108
  this.removeFromInFlight(params);
@@ -1119,6 +1124,11 @@ var PrefetchedRequests = class {
1119
1124
  });
1120
1125
  this.removalTimers = [];
1121
1126
  }
1127
+ removeByTags(tags) {
1128
+ this.cached = this.cached.filter((prefetched) => {
1129
+ return !prefetched.tags.some((tag) => tags.includes(tag));
1130
+ });
1131
+ }
1122
1132
  remove(params) {
1123
1133
  this.cached = this.cached.filter((prefetched) => {
1124
1134
  return !this.paramsAreEqual(prefetched.params, params);
@@ -1468,6 +1478,7 @@ var Response = class _Response {
1468
1478
  fireErrorEvent(scopedErrors);
1469
1479
  return this.requestParams.all().onError(scopedErrors);
1470
1480
  }
1481
+ router.flushByCacheTags(this.requestParams.all().invalidateCacheTags || []);
1471
1482
  fireSuccessEvent(page.get());
1472
1483
  await this.requestParams.all().onSuccess(page.get());
1473
1484
  history.preserveUrl = false;
@@ -1910,11 +1921,15 @@ var Router = class {
1910
1921
  flushAll() {
1911
1922
  prefetchedRequests.removeAll();
1912
1923
  }
1924
+ flushByCacheTags(tags) {
1925
+ prefetchedRequests.removeByTags(Array.isArray(tags) ? tags : [tags]);
1926
+ }
1913
1927
  getPrefetching(href, options = {}) {
1914
1928
  return prefetchedRequests.findInFlight(this.getPrefetchParams(href, options));
1915
1929
  }
1916
- prefetch(href, options = {}, { cacheFor = 3e4 }) {
1917
- if (options.method !== "get") {
1930
+ prefetch(href, options = {}, prefetchOptions = {}) {
1931
+ const method = options.method ?? (isUrlMethodPair(href) ? href.method : "get");
1932
+ if (method !== "get") {
1918
1933
  throw new Error("Prefetch requests must use the GET method");
1919
1934
  }
1920
1935
  const visit = this.getPendingVisit(href, {
@@ -1956,7 +1971,11 @@ var Router = class {
1956
1971
  (params) => {
1957
1972
  this.asyncRequestStream.send(Request.create(params, page.get()));
1958
1973
  },
1959
- { cacheFor }
1974
+ {
1975
+ cacheFor: 3e4,
1976
+ cacheTags: [],
1977
+ ...prefetchOptions
1978
+ }
1960
1979
  );
1961
1980
  });
1962
1981
  }
@@ -2011,6 +2030,11 @@ var Router = class {
2011
2030
  };
2012
2031
  }
2013
2032
  getPendingVisit(href, options, pendingVisitOptions = {}) {
2033
+ if (isUrlMethodPair(href)) {
2034
+ const urlMethodPair = href;
2035
+ href = urlMethodPair.url;
2036
+ options.method = options.method ?? urlMethodPair.method;
2037
+ }
2014
2038
  const mergedOptions = {
2015
2039
  method: "get",
2016
2040
  data: {},
@@ -2029,6 +2053,7 @@ var Router = class {
2029
2053
  reset: [],
2030
2054
  preserveUrl: false,
2031
2055
  prefetch: false,
2056
+ invalidateCacheTags: [],
2032
2057
  ...options
2033
2058
  };
2034
2059
  const [url, _data] = transformUrlAndData(
@@ -2247,6 +2272,9 @@ function resetFieldElements(elements, defaultValues) {
2247
2272
  return hasChanged;
2248
2273
  }
2249
2274
  function resetFormFields(formElement, defaults, fieldNames) {
2275
+ if (!formElement) {
2276
+ return;
2277
+ }
2250
2278
  if (!fieldNames || fieldNames.length === 0) {
2251
2279
  const formData = new FormData(formElement);
2252
2280
  const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);