@inertiajs/core 2.1.0 → 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.esm.js CHANGED
@@ -342,14 +342,15 @@ var transformUrlAndData = (href, data, method, forceFormData, queryStringArrayFo
342
342
  return [hrefToUrl(_href), _data];
343
343
  };
344
344
  function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets") {
345
+ const hasDataForQueryString = method === "get" && !isFormData(data) && Object.keys(data).length > 0;
345
346
  const hasHost = /^[a-z][a-z0-9+.-]*:\/\//i.test(href.toString());
346
- const hasAbsolutePath = hasHost || href.toString().startsWith("/");
347
+ const hasAbsolutePath = hasHost || href.toString().startsWith("/") || href.toString() === "";
347
348
  const hasRelativePath = !hasAbsolutePath && !href.toString().startsWith("#") && !href.toString().startsWith("?");
348
349
  const hasRelativePathWithDotPrefix = /^[.]{1,2}([/]|$)/.test(href.toString());
349
- const hasSearch = href.toString().includes("?") || method === "get" && Object.keys(data).length;
350
+ const hasSearch = href.toString().includes("?") || hasDataForQueryString;
350
351
  const hasHash = href.toString().includes("#");
351
352
  const url = new URL(href.toString(), typeof window === "undefined" ? "http://localhost" : window.location.toString());
352
- if (method === "get" && Object.keys(data).length) {
353
+ if (hasDataForQueryString) {
353
354
  const parseOptions = { ignoreQueryPrefix: true, parseArrays: false };
354
355
  url.search = qs.stringify(
355
356
  { ...qs.parse(url.search, parseOptions), ...data },
@@ -358,7 +359,6 @@ function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets"
358
359
  arrayFormat: qsArrayFormat
359
360
  }
360
361
  );
361
- data = {};
362
362
  }
363
363
  return [
364
364
  [
@@ -368,7 +368,7 @@ function mergeDataIntoQueryString(method, href, data, qsArrayFormat = "brackets"
368
368
  hasSearch ? url.search : "",
369
369
  hasHash ? url.hash : ""
370
370
  ].join(""),
371
- data
371
+ hasDataForQueryString ? {} : data
372
372
  ];
373
373
  }
374
374
  function urlWithoutHash(url) {
@@ -384,6 +384,9 @@ var setHashIfSameUrl = (originUrl, destinationUrl) => {
384
384
  var isSameUrlWithoutHash = (url1, url2) => {
385
385
  return urlWithoutHash(url1).href === urlWithoutHash(url2).href;
386
386
  };
387
+ function isUrlMethodPair(href) {
388
+ return href !== null && typeof href === "object" && href !== void 0 && "url" in href && "method" in href;
389
+ }
387
390
 
388
391
  // src/page.ts
389
392
  var CurrentPage = class {
@@ -1005,7 +1008,7 @@ var PrefetchedRequests = class {
1005
1008
  this.removalTimers = [];
1006
1009
  this.currentUseId = null;
1007
1010
  }
1008
- add(params, sendFunc, { cacheFor }) {
1011
+ add(params, sendFunc, { cacheFor, cacheTags }) {
1009
1012
  const inFlight = this.findInFlight(params);
1010
1013
  if (inFlight) {
1011
1014
  return Promise.resolve();
@@ -1036,6 +1039,10 @@ var PrefetchedRequests = class {
1036
1039
  },
1037
1040
  onPrefetchResponse(response) {
1038
1041
  resolve(response);
1042
+ },
1043
+ onPrefetchError(error) {
1044
+ prefetchedRequests.removeFromInFlight(params);
1045
+ reject(error);
1039
1046
  }
1040
1047
  });
1041
1048
  }).then((response) => {
@@ -1046,12 +1053,11 @@ var PrefetchedRequests = class {
1046
1053
  response: promise,
1047
1054
  singleUse: expires === 0,
1048
1055
  timestamp: Date.now(),
1049
- inFlight: false
1056
+ inFlight: false,
1057
+ tags: Array.isArray(cacheTags) ? cacheTags : [cacheTags]
1050
1058
  });
1051
1059
  this.scheduleForRemoval(params, expires);
1052
- this.inFlightRequests = this.inFlightRequests.filter((prefetching) => {
1053
- return !this.paramsAreEqual(prefetching.params, params);
1054
- });
1060
+ this.removeFromInFlight(params);
1055
1061
  response.handlePrefetch();
1056
1062
  return response;
1057
1063
  });
@@ -1070,12 +1076,22 @@ var PrefetchedRequests = class {
1070
1076
  });
1071
1077
  this.removalTimers = [];
1072
1078
  }
1079
+ removeByTags(tags) {
1080
+ this.cached = this.cached.filter((prefetched) => {
1081
+ return !prefetched.tags.some((tag) => tags.includes(tag));
1082
+ });
1083
+ }
1073
1084
  remove(params) {
1074
1085
  this.cached = this.cached.filter((prefetched) => {
1075
1086
  return !this.paramsAreEqual(prefetched.params, params);
1076
1087
  });
1077
1088
  this.clearTimer(params);
1078
1089
  }
1090
+ removeFromInFlight(params) {
1091
+ this.inFlightRequests = this.inFlightRequests.filter((prefetching) => {
1092
+ return !this.paramsAreEqual(prefetching.params, params);
1093
+ });
1094
+ }
1079
1095
  extractStaleValues(cacheFor) {
1080
1096
  const [stale, expires] = this.cacheForToStaleAndExpires(cacheFor);
1081
1097
  return [timeToMs(stale), timeToMs(expires)];
@@ -1207,6 +1223,8 @@ var RequestParams = class _RequestParams {
1207
1223
  ...params,
1208
1224
  ...wrappedCallbacks,
1209
1225
  onPrefetchResponse: params.onPrefetchResponse || (() => {
1226
+ }),
1227
+ onPrefetchError: params.onPrefetchError || (() => {
1210
1228
  })
1211
1229
  };
1212
1230
  }
@@ -1256,6 +1274,11 @@ var RequestParams = class _RequestParams {
1256
1274
  this.params.onPrefetchResponse(response);
1257
1275
  }
1258
1276
  }
1277
+ onPrefetchError(error) {
1278
+ if (this.params.onPrefetchError) {
1279
+ this.params.onPrefetchError(error);
1280
+ }
1281
+ }
1259
1282
  all() {
1260
1283
  return this.params;
1261
1284
  }
@@ -1407,6 +1430,7 @@ var Response = class _Response {
1407
1430
  fireErrorEvent(scopedErrors);
1408
1431
  return this.requestParams.all().onError(scopedErrors);
1409
1432
  }
1433
+ router.flushByCacheTags(this.requestParams.all().invalidateCacheTags || []);
1410
1434
  fireSuccessEvent(page.get());
1411
1435
  await this.requestParams.all().onSuccess(page.get());
1412
1436
  history.preserveUrl = false;
@@ -1638,6 +1662,9 @@ var Request = class _Request {
1638
1662
  return;
1639
1663
  }
1640
1664
  if (fireExceptionEvent(error)) {
1665
+ if (originallyPrefetch) {
1666
+ this.requestParams.onPrefetchError(error);
1667
+ }
1641
1668
  return Promise.reject(error);
1642
1669
  }
1643
1670
  }).finally(() => {
@@ -1846,11 +1873,15 @@ var Router = class {
1846
1873
  flushAll() {
1847
1874
  prefetchedRequests.removeAll();
1848
1875
  }
1876
+ flushByCacheTags(tags) {
1877
+ prefetchedRequests.removeByTags(Array.isArray(tags) ? tags : [tags]);
1878
+ }
1849
1879
  getPrefetching(href, options = {}) {
1850
1880
  return prefetchedRequests.findInFlight(this.getPrefetchParams(href, options));
1851
1881
  }
1852
- prefetch(href, options = {}, { cacheFor = 3e4 }) {
1853
- if (options.method !== "get") {
1882
+ prefetch(href, options = {}, prefetchOptions = {}) {
1883
+ const method = options.method ?? (isUrlMethodPair(href) ? href.method : "get");
1884
+ if (method !== "get") {
1854
1885
  throw new Error("Prefetch requests must use the GET method");
1855
1886
  }
1856
1887
  const visit = this.getPendingVisit(href, {
@@ -1892,7 +1923,11 @@ var Router = class {
1892
1923
  (params) => {
1893
1924
  this.asyncRequestStream.send(Request.create(params, page.get()));
1894
1925
  },
1895
- { cacheFor }
1926
+ {
1927
+ cacheFor: 3e4,
1928
+ cacheTags: [],
1929
+ ...prefetchOptions
1930
+ }
1896
1931
  );
1897
1932
  });
1898
1933
  }
@@ -1947,6 +1982,11 @@ var Router = class {
1947
1982
  };
1948
1983
  }
1949
1984
  getPendingVisit(href, options, pendingVisitOptions = {}) {
1985
+ if (isUrlMethodPair(href)) {
1986
+ const urlMethodPair = href;
1987
+ href = urlMethodPair.url;
1988
+ options.method = options.method ?? urlMethodPair.method;
1989
+ }
1950
1990
  const mergedOptions = {
1951
1991
  method: "get",
1952
1992
  data: {},
@@ -1965,6 +2005,7 @@ var Router = class {
1965
2005
  reset: [],
1966
2006
  preserveUrl: false,
1967
2007
  prefetch: false,
2008
+ invalidateCacheTags: [],
1968
2009
  ...options
1969
2010
  };
1970
2011
  const [url, _data] = transformUrlAndData(
@@ -2071,6 +2112,140 @@ function formDataToObject(source) {
2071
2112
  return form;
2072
2113
  }
2073
2114
 
2115
+ // src/resetFormFields.ts
2116
+ function isFormElement(element) {
2117
+ return element instanceof HTMLInputElement || element instanceof HTMLSelectElement || element instanceof HTMLTextAreaElement;
2118
+ }
2119
+ function resetInputElement(input, defaultValues) {
2120
+ const oldValue = input.value;
2121
+ const oldChecked = input.checked;
2122
+ switch (input.type.toLowerCase()) {
2123
+ case "checkbox":
2124
+ input.checked = defaultValues.includes(input.value);
2125
+ break;
2126
+ case "radio":
2127
+ input.checked = defaultValues[0] === input.value;
2128
+ break;
2129
+ case "file":
2130
+ input.value = "";
2131
+ break;
2132
+ case "button":
2133
+ case "submit":
2134
+ case "reset":
2135
+ case "image":
2136
+ break;
2137
+ default:
2138
+ input.value = defaultValues[0] !== null && defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
2139
+ }
2140
+ return input.value !== oldValue || input.checked !== oldChecked;
2141
+ }
2142
+ function resetSelectElement(select, defaultValues) {
2143
+ const oldValue = select.value;
2144
+ const oldSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
2145
+ if (select.multiple) {
2146
+ const defaultStrings = defaultValues.map((value) => String(value));
2147
+ Array.from(select.options).forEach((option) => {
2148
+ option.selected = defaultStrings.includes(option.value);
2149
+ });
2150
+ } else {
2151
+ select.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
2152
+ }
2153
+ const newSelectedOptions = Array.from(select.selectedOptions).map((opt) => opt.value);
2154
+ const hasChanged = select.multiple ? JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort()) : select.value !== oldValue;
2155
+ return hasChanged;
2156
+ }
2157
+ function resetFormElement(element, defaultValues) {
2158
+ if (element.disabled) {
2159
+ if (element instanceof HTMLInputElement) {
2160
+ const oldValue = element.value;
2161
+ const oldChecked = element.checked;
2162
+ switch (element.type.toLowerCase()) {
2163
+ case "checkbox":
2164
+ case "radio":
2165
+ element.checked = element.defaultChecked;
2166
+ return element.checked !== oldChecked;
2167
+ case "file":
2168
+ element.value = "";
2169
+ return oldValue !== "";
2170
+ case "button":
2171
+ case "submit":
2172
+ case "reset":
2173
+ case "image":
2174
+ return false;
2175
+ default:
2176
+ element.value = element.defaultValue;
2177
+ return element.value !== oldValue;
2178
+ }
2179
+ } else if (element instanceof HTMLSelectElement) {
2180
+ const oldSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
2181
+ Array.from(element.options).forEach((option) => {
2182
+ option.selected = option.defaultSelected;
2183
+ });
2184
+ const newSelectedOptions = Array.from(element.selectedOptions).map((opt) => opt.value);
2185
+ return JSON.stringify(oldSelectedOptions.sort()) !== JSON.stringify(newSelectedOptions.sort());
2186
+ } else if (element instanceof HTMLTextAreaElement) {
2187
+ const oldValue = element.value;
2188
+ element.value = element.defaultValue;
2189
+ return element.value !== oldValue;
2190
+ }
2191
+ return false;
2192
+ }
2193
+ if (element instanceof HTMLInputElement) {
2194
+ return resetInputElement(element, defaultValues);
2195
+ } else if (element instanceof HTMLSelectElement) {
2196
+ return resetSelectElement(element, defaultValues);
2197
+ } else if (element instanceof HTMLTextAreaElement) {
2198
+ const oldValue = element.value;
2199
+ element.value = defaultValues[0] !== void 0 ? String(defaultValues[0]) : "";
2200
+ return element.value !== oldValue;
2201
+ }
2202
+ return false;
2203
+ }
2204
+ function resetFieldElements(elements, defaultValues) {
2205
+ let hasChanged = false;
2206
+ if (elements instanceof RadioNodeList || elements instanceof HTMLCollection) {
2207
+ Array.from(elements).forEach((node, index) => {
2208
+ if (node instanceof Element && isFormElement(node)) {
2209
+ if (node instanceof HTMLInputElement && ["checkbox", "radio"].includes(node.type.toLowerCase())) {
2210
+ if (resetFormElement(node, defaultValues)) {
2211
+ hasChanged = true;
2212
+ }
2213
+ } else {
2214
+ const indexedDefaultValues = defaultValues[index] !== void 0 ? [defaultValues[index]] : [defaultValues[0] ?? null].filter(Boolean);
2215
+ if (resetFormElement(node, indexedDefaultValues)) {
2216
+ hasChanged = true;
2217
+ }
2218
+ }
2219
+ }
2220
+ });
2221
+ } else if (isFormElement(elements)) {
2222
+ hasChanged = resetFormElement(elements, defaultValues);
2223
+ }
2224
+ return hasChanged;
2225
+ }
2226
+ function resetFormFields(formElement, defaults, fieldNames) {
2227
+ if (!formElement) {
2228
+ return;
2229
+ }
2230
+ if (!fieldNames || fieldNames.length === 0) {
2231
+ const formData = new FormData(formElement);
2232
+ const formElementNames = Array.from(formElement.elements).map((el) => isFormElement(el) ? el.name : "").filter(Boolean);
2233
+ fieldNames = [.../* @__PURE__ */ new Set([...defaults.keys(), ...formData.keys(), ...formElementNames])];
2234
+ }
2235
+ let hasChanged = false;
2236
+ fieldNames.forEach((fieldName) => {
2237
+ const elements = formElement.elements.namedItem(fieldName);
2238
+ if (elements) {
2239
+ if (resetFieldElements(elements, defaults.getAll(fieldName))) {
2240
+ hasChanged = true;
2241
+ }
2242
+ }
2243
+ });
2244
+ if (hasChanged) {
2245
+ formElement.dispatchEvent(new Event("reset", { bubbles: true }));
2246
+ }
2247
+ }
2248
+
2074
2249
  // src/head.ts
2075
2250
  var Renderer = {
2076
2251
  buildDOMElement(tag) {
@@ -2530,8 +2705,10 @@ export {
2530
2705
  formDataToObject,
2531
2706
  hide as hideProgress,
2532
2707
  hrefToUrl,
2708
+ isUrlMethodPair,
2533
2709
  mergeDataIntoQueryString,
2534
2710
  objectToFormData,
2711
+ resetFormFields,
2535
2712
  reveal as revealProgress,
2536
2713
  router,
2537
2714
  setupProgress,