@wcstack/fetch 4.1.1 → 4.3.0

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.d.ts CHANGED
@@ -214,6 +214,7 @@ declare class FetchCore extends EventTarget {
214
214
  private _objectURL;
215
215
  private _promise;
216
216
  private _lane;
217
+ private _inFlight;
217
218
  private _ready;
218
219
  private _errorInfo;
219
220
  private static readonly REQUIRED_CAPABILITIES;
@@ -262,6 +263,8 @@ declare class FetchCore extends EventTarget {
262
263
 
263
264
  declare class Fetch extends HTMLElement {
264
265
  static hasConnectedCallbackPromise: boolean;
266
+ /** It reports the request it has in flight (pendingPromise): a server render waits for it. */
267
+ static hasPendingPromise: boolean;
265
268
  static wcBindable: IWcBindable;
266
269
  static get observedAttributes(): string[];
267
270
  private _core;
@@ -292,6 +295,14 @@ declare class Fetch extends HTMLElement {
292
295
  get errorInfo(): WcsIoErrorInfo | null;
293
296
  get promise(): Promise<any>;
294
297
  get connectedCallbackPromise(): Promise<void>;
298
+ /**
299
+ * The readiness protocol's pending work (`static hasPendingPromise`): the request this element
300
+ * has in flight, or is about to start (an auto-fetch waits a microtask), settled either way; null
301
+ * when there is none. A server render waits for it, so a request a binding started — a `url`
302
+ * written from state after the element connected — is in the page it serializes
303
+ * (@wcstack/server; docs/v4-direction-2026-10.md §5.1).
304
+ */
305
+ get pendingPromise(): Promise<void> | null;
295
306
  get manual(): boolean;
296
307
  set manual(value: boolean | null | undefined);
297
308
  get body(): any;
@@ -321,6 +332,14 @@ declare class Fetch extends HTMLElement {
321
332
  * when the final state turns out to be manual / url-less / disconnected.
322
333
  */
323
334
  private _scheduleAutoFetch;
335
+ /**
336
+ * The url changed and the request it starts is a change (POST, PUT, …) with no
337
+ * body: the method stays, and the body was sent once with the previous request.
338
+ * One element that both loads (by url) and posts (by method + body + trigger)
339
+ * sent an empty POST on every navigation after the first post, without a word
340
+ * (docs/seam-apps.md, ssr-news; docs/mistake-catalogue.md).
341
+ */
342
+ private _warnUnsafeAutoFetch;
324
343
  fetch(): Promise<any>;
325
344
  attributeChangedCallback(name: string, _oldValue: string | null, _newValue: string | null): void;
326
345
  connectedCallback(): void;
@@ -331,6 +350,7 @@ declare class InfiniteScroll extends HTMLElement {
331
350
  static get observedAttributes(): string[];
332
351
  private _observer;
333
352
  private _done;
353
+ private _warnedTarget;
334
354
  get target(): string;
335
355
  set target(value: string);
336
356
  get root(): string | null;
package/dist/index.esm.js CHANGED
@@ -450,6 +450,8 @@ const FETCH_CAPABILITIES = new Map([
450
450
  ["web.abort-controller", { probe: () => typeof globalThis.AbortController === "function", compatKey: "api.AbortController" }],
451
451
  ]);
452
452
 
453
+ /** Methods that read only: superseding one loses nothing (RFC 9110 §9.2.1). */
454
+ const SAFE_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);
453
455
  class FetchCore extends EventTarget {
454
456
  static wcBindable = {
455
457
  protocol: "wc-bindable",
@@ -494,6 +496,8 @@ class FetchCore extends EventTarget {
494
496
  // relying on a coarse generation recheck, closing the "completion racing an
495
497
  // abort commits stale state" gap during body reads.
496
498
  _lane = new OperationLane("fetch", "latest", { withSignal: true });
499
+ // The request in flight (method and url), to name a change a new request supersedes.
500
+ _inFlight = null;
497
501
  // SSR (§3.8): no asynchronous probe to await, so readiness is immediate.
498
502
  _ready = Promise.resolve();
499
503
  // Phase 6 (§7.2): error taxonomy. The existing `error` property/event shape is
@@ -694,10 +698,24 @@ class FetchCore extends EventTarget {
694
698
  // attempt whose `signal` is the lane-owned AbortController for this operation.
695
699
  // `attempt.signal` is undefined when `web.abort-controller` is missing
696
700
  // (degraded): the request runs without a native abort signal.
701
+ //
702
+ // Superseding is by design, and a superseded read loses nothing. A superseded
703
+ // change does: it is aborted on the client (the server may or may not have
704
+ // applied it) and its response is dropped, without a word. Say so, naming it
705
+ // (docs/mistake-catalogue.md, "fetch-superseded-mutation").
706
+ const previous = this._inFlight;
707
+ if (previous !== null && !SAFE_METHODS.has(previous.method)) {
708
+ console.warn(`[@wcstack/fetch] [wcs/fetch-request-superseded] A ${previous.method} request to "${previous.url}" ` +
709
+ `was still in flight when another request started on the same element: it was aborted and its ` +
710
+ `response dropped (the server may have applied it). Give each change its own <wcs-fetch>, or wait ` +
711
+ `for loading to end before starting the next request.`);
712
+ }
697
713
  const started = this._lane.begin();
698
714
  // `latest` begin never returns null (exhaust is the only rejecting policy).
699
715
  const { ticket, attempt } = started;
700
716
  const signal = attempt.signal;
717
+ const current = { method: String(options.method ?? "GET").toUpperCase(), url };
718
+ this._inFlight = current;
701
719
  this._commitStep(ticket, () => this._setLoading(true));
702
720
  this._commitStep(ticket, () => { this._commitErrorInfo(null); this._setError(null); });
703
721
  const { method = "GET", body = null, contentType = null, forceText = false, responseType = "auto", timeout = 0, } = options;
@@ -846,6 +864,8 @@ class FetchCore extends EventTarget {
846
864
  return null;
847
865
  }
848
866
  finally {
867
+ if (this._inFlight === current)
868
+ this._inFlight = null;
849
869
  if (timeoutTimer !== null) {
850
870
  clearTimeout(timeoutTimer);
851
871
  }
@@ -1106,6 +1126,8 @@ function writeTargetHTML(element, html) {
1106
1126
 
1107
1127
  class Fetch extends HTMLElement {
1108
1128
  static hasConnectedCallbackPromise = true;
1129
+ /** It reports the request it has in flight (pendingPromise): a server render waits for it. */
1130
+ static hasPendingPromise = true;
1109
1131
  static wcBindable = {
1110
1132
  ...FetchCore.wcBindable,
1111
1133
  properties: [
@@ -1255,6 +1277,19 @@ class Fetch extends HTMLElement {
1255
1277
  get connectedCallbackPromise() {
1256
1278
  return this._connectedCallbackPromise;
1257
1279
  }
1280
+ /**
1281
+ * The readiness protocol's pending work (`static hasPendingPromise`): the request this element
1282
+ * has in flight, or is about to start (an auto-fetch waits a microtask), settled either way; null
1283
+ * when there is none. A server render waits for it, so a request a binding started — a `url`
1284
+ * written from state after the element connected — is in the page it serializes
1285
+ * (@wcstack/server; docs/v4-direction-2026-10.md §5.1).
1286
+ */
1287
+ get pendingPromise() {
1288
+ if (this._autoPending && !this.manual && this.url)
1289
+ return Promise.resolve().then(() => this.pendingPromise ?? undefined);
1290
+ // (FetchCore settles a failed request too: it rejects only on an empty url)
1291
+ return this.loading ? this._core.promise.then(() => undefined) : null;
1292
+ }
1258
1293
  get manual() {
1259
1294
  return this.hasAttribute("manual");
1260
1295
  }
@@ -1383,7 +1418,17 @@ class Fetch extends HTMLElement {
1383
1418
  // change would refetch. Auto-path only — explicit fetch()/trigger/command
1384
1419
  // stay unconditional (a manual refresh of the same url must work), and
1385
1420
  // `_lastFetchedUrl` is reset on disconnect so a remount refetches.
1421
+ if (!url) {
1422
+ // The url went empty (a route left, a getter returning null): the next url
1423
+ // is a change, even back to the one fetched last. Without this, A → null → A
1424
+ // sent nothing the second time — after a failed request too, so a page
1425
+ // left and opened again never retried (docs/seam-apps.md, ssr-news).
1426
+ this._lastFetchedUrl = null;
1427
+ }
1386
1428
  if (this.isConnected && !this.manual && url && url !== this._lastFetchedUrl) {
1429
+ // (not the request the element starts as it connects with a url: a POST that is meant)
1430
+ if (resolveConnect === null)
1431
+ this._warnUnsafeAutoFetch();
1387
1432
  // fetch() cannot reject here: FetchCore swallows network/HTTP errors and
1388
1433
  // only rejects on an empty url, which the `url` guard above rules out.
1389
1434
  this.fetch().finally(() => resolveConnect?.());
@@ -1393,6 +1438,23 @@ class Fetch extends HTMLElement {
1393
1438
  }
1394
1439
  });
1395
1440
  }
1441
+ /**
1442
+ * The url changed and the request it starts is a change (POST, PUT, …) with no
1443
+ * body: the method stays, and the body was sent once with the previous request.
1444
+ * One element that both loads (by url) and posts (by method + body + trigger)
1445
+ * sent an empty POST on every navigation after the first post, without a word
1446
+ * (docs/seam-apps.md, ssr-news; docs/mistake-catalogue.md).
1447
+ */
1448
+ _warnUnsafeAutoFetch() {
1449
+ const method = this.method;
1450
+ if (method === "GET" || method === "HEAD" || method === "OPTIONS")
1451
+ return;
1452
+ if (this._body !== null || this.querySelector(config.tagNames.fetchBody) !== null)
1453
+ return;
1454
+ console.warn(`[@wcstack/fetch] [wcs/fetch-auto-unsafe] The url changed to "${this.url}", so this <wcs-fetch> sent a ` +
1455
+ `${method} with no body (its method stays ${method}; a body is sent once). Give loading and each change ` +
1456
+ `its own <wcs-fetch>, or set \`manual\` on the one that changes data.`);
1457
+ }
1396
1458
  async fetch() {
1397
1459
  // Record the url for the auto-fetch same-value guard. Every fetch (explicit
1398
1460
  // included) updates it so a later auto-write of the same url is treated as a
@@ -1556,6 +1618,8 @@ class InfiniteScroll extends HTMLElement {
1556
1618
  }
1557
1619
  _observer = null;
1558
1620
  _done = false;
1621
+ // the target last warned about (once per value)
1622
+ _warnedTarget = null;
1559
1623
  get target() {
1560
1624
  return this.getAttribute("target") || "";
1561
1625
  }
@@ -1649,6 +1713,13 @@ class InfiniteScroll extends HTMLElement {
1649
1713
  _triggerFetch() {
1650
1714
  const target = document.getElementById(this.target);
1651
1715
  if (!(target instanceof Fetch)) {
1716
+ // Nothing to load from: say so once per target, instead of loading no more pages without a word
1717
+ // (docs/mistake-catalogue.md, "infinite-scroll-target").
1718
+ if (this._warnedTarget !== this.target) {
1719
+ this._warnedTarget = this.target;
1720
+ console.warn(`[@wcstack/fetch] [wcs/infinite-scroll-target] <${config.tagNames.infiniteScroll} target="${this.target}"> ` +
1721
+ `came into view, but no <${config.tagNames.fetch}> has that id, so no page was loaded.`);
1722
+ }
1652
1723
  return;
1653
1724
  }
1654
1725
  if (target.loading) {