@wcstack/router 4.1.0 → 4.2.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
@@ -341,6 +341,8 @@ declare class Router extends HTMLElement implements IRouter {
341
341
  private _leaveChecked;
342
342
  /** 離脱が確認された: 次の commit まで(その遷移の guard のリダイレクトで聞き直さない) */
343
343
  private _leaveConfirmed;
344
+ /** ページを離れるナビゲーションが確認された: beforeunload で二度聞かない(次のタスクで戻す) */
345
+ private _unloadConfirmed;
344
346
  private _listeningUnload;
345
347
  private _connectedCallbackPromise;
346
348
  private _resolveConnectedCallback;
@@ -392,6 +394,15 @@ declare class Router extends HTMLElement implements IRouter {
392
394
  * (クエリだけの遷移)・確認済みの遷移の続き(guard のリダイレクト)では聞かない。
393
395
  */
394
396
  private _confirmLeave;
397
+ /**
398
+ * ページを離れるナビゲーション(どの router も受けない)の確認。leaveConfirm の文言で聞き、断られたら
399
+ * 取り消す。受け入れたら、そのナビゲーションの beforeunload で二度聞かない(Chromium では navigate の
400
+ * 直後、次のタスクより前に来る。遅れて来るブラウザでは二度聞くだけ)。取り消せないもの(別の文書への
401
+ * 戻る / 進む)と最初の表示の前は聞かない — beforeunload の側に任せる。
402
+ */
403
+ private _confirmUnload;
404
+ /** 同じ文書の別の router が受ける URL か(マルチ Router: そちらの遷移はページを離れない) */
405
+ private _ownedByAnotherRouter;
395
406
  private _getOutlet;
396
407
  private _getTemplate;
397
408
  get outlet(): IOutlet;
package/dist/index.esm.js CHANGED
@@ -1350,6 +1350,72 @@ function moveInto(target, nodes) {
1350
1350
  }
1351
1351
  }
1352
1352
 
1353
+ /**
1354
+ * `<wcs-router>` の直後に無い `<wcs-outlet>` を loud に報告する
1355
+ * (docs/router-outlet-placement.md)。
1356
+ *
1357
+ * router が使う outlet は、自分の直後の兄弟要素だけである(マルチ Router で router と
1358
+ * outlet を 1 対 1 に結ぶための規則。SSR の採用経路もこの位置を前提にする)。直後に
1359
+ * 無ければ、router は outlet を作って自分の直後に置く。そのとき別の場所に置かれた
1360
+ * `<wcs-outlet>` は使われず、ページは router の直後に描かれ、置いた outlet は空のまま
1361
+ * になる — これまでは何も報告されなかった。
1362
+ *
1363
+ * **挙動は変えない。** 変えるのは「黙って別の場所に描く」を「原因を指す警告」にする
1364
+ * ことだけ。outlet を探す範囲を広げる案・属性で明示的に結ぶ案は、実需が出てから決める。
1365
+ *
1366
+ * router が outlet を自分のものにするのは「直後にある」か「直後に作った」かのどちら
1367
+ * かなので、直前の要素が router でない outlet は、どの router にも使われない。その判定
1368
+ * だけで足り、ほかの router の初期化の順番を待つ必要は無い。
1369
+ *
1370
+ * レイアウトのテンプレートが持ち込んだ outlet(スロットの代わりに置いた形)は、router が outlet を
1371
+ * 作った後で文書に入るので、レイアウトを置いたところでも同じ判定をする(docs/seam-apps.md、router-admin)。
1372
+ */
1373
+ const warned$1 = new WeakSet();
1374
+ /**
1375
+ * 直後に outlet を持たない router が outlet を作る直前に呼ぶ。同じ root(document か
1376
+ * shadow root)に、どの router にも使われない `<wcs-outlet>` があれば 1 回だけ警告する。
1377
+ */
1378
+ function warnMisplacedOutlet(router) {
1379
+ const root = router.getRootNode();
1380
+ // 文書に入っていない router(root は自分自身)には、比べる相手のページが無い
1381
+ if (root.nodeType !== Node.DOCUMENT_NODE && root.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
1382
+ return;
1383
+ }
1384
+ warnStrayOutlets(root);
1385
+ }
1386
+ /** scope の中の、どの router にも使われない `<wcs-outlet>` を 1 回だけ警告する。 */
1387
+ function warnStrayOutlets(scope) {
1388
+ const routerTag = config.tagNames.router;
1389
+ const outletTag = config.tagNames.outlet;
1390
+ for (const outlet of scope.querySelectorAll(outletTag)) {
1391
+ if (warned$1.has(outlet) || outlet.previousElementSibling?.matches(routerTag)) {
1392
+ continue;
1393
+ }
1394
+ warned$1.add(outlet);
1395
+ console.warn(`[@wcstack/router] [wcs/router-outlet-misplaced] A <${routerTag}> uses only the element right after it ` +
1396
+ `as its outlet, and this page has a <${outletTag}> that no router uses. The router makes an outlet of ` +
1397
+ `its own right after itself, so the routes render there and the <${outletTag}> you placed stays empty. ` +
1398
+ `Move the <${outletTag}> right after the <${routerTag}>.`, outlet);
1399
+ }
1400
+ }
1401
+
1402
+ /** 何かを置くノードか(空白だけのテキストとコメントは置かない) */
1403
+ const hasContent = (nodes) => {
1404
+ for (const node of nodes) {
1405
+ if (node.nodeType === Node.ELEMENT_NODE || (node.nodeType === Node.TEXT_NODE && node.textContent.trim() !== ''))
1406
+ return true;
1407
+ }
1408
+ return false;
1409
+ };
1410
+ /**
1411
+ * 既定の `<slot>` の無いテンプレートに、名前付きのスロットの外の子(入れ子のルートとそのページ)を
1412
+ * 渡した: 子は表示されない — これまでは何も報告されなかった(docs/seam-apps.md、router-admin)。
1413
+ */
1414
+ function warnNoDefaultSlot(outlet) {
1415
+ console.warn(`[@wcstack/router] [wcs/layout-slot-missing] This layout's template has no default <slot>, so what its ` +
1416
+ `<${config.tagNames.layout}> holds outside the named slots — the nested routes and their pages — is not shown. ` +
1417
+ `Put a <slot></slot> where the page goes.`, outlet);
1418
+ }
1353
1419
  class LayoutOutlet extends HTMLElement {
1354
1420
  _layout = null;
1355
1421
  _initialized = false;
@@ -1391,10 +1457,16 @@ class LayoutOutlet extends HTMLElement {
1391
1457
  }
1392
1458
  if (this.shadowRoot) {
1393
1459
  this.shadowRoot.appendChild(template.content.cloneNode(true));
1460
+ const unslotted = [];
1394
1461
  for (const childNode of Array.from(this.layout.childNodes)) {
1395
1462
  this._layoutChildNodes.push(childNode);
1463
+ if (!(childNode instanceof Element) || !childNode.getAttribute('slot'))
1464
+ unslotted.push(childNode);
1396
1465
  this.appendChild(childNode);
1397
1466
  }
1467
+ if (!this.shadowRoot.querySelector('slot:not([name]), slot[name=""]') && hasContent(unslotted))
1468
+ warnNoDefaultSlot(this);
1469
+ warnStrayOutlets(this.shadowRoot);
1398
1470
  }
1399
1471
  else {
1400
1472
  const fragmentForTemplate = template.content.cloneNode(true);
@@ -1434,7 +1506,11 @@ class LayoutOutlet extends HTMLElement {
1434
1506
  if (defaultSlot) {
1435
1507
  defaultSlot.replaceWith(fragmentForChildNodes);
1436
1508
  }
1509
+ else if (hasContent(fragmentForChildNodes.childNodes)) {
1510
+ warnNoDefaultSlot(this);
1511
+ }
1437
1512
  this.appendChild(fragmentForTemplate);
1513
+ warnStrayOutlets(this);
1438
1514
  }
1439
1515
  // 置いた後で binder へ渡す(D5)。遷移で入ったルートの内容は router が渡す時点では文書の外で、
1440
1516
  // レイアウトのテンプレート自身の束縛もここでしか渡せない(shadow root の中は対象外)。light DOM は
@@ -1692,48 +1768,6 @@ async function parse(routerNode) {
1692
1768
  return fr;
1693
1769
  }
1694
1770
 
1695
- /**
1696
- * `<wcs-router>` の直後に無い `<wcs-outlet>` を loud に報告する
1697
- * (docs/router-outlet-placement.md)。
1698
- *
1699
- * router が使う outlet は、自分の直後の兄弟要素だけである(マルチ Router で router と
1700
- * outlet を 1 対 1 に結ぶための規則。SSR の採用経路もこの位置を前提にする)。直後に
1701
- * 無ければ、router は outlet を作って自分の直後に置く。そのとき別の場所に置かれた
1702
- * `<wcs-outlet>` は使われず、ページは router の直後に描かれ、置いた outlet は空のまま
1703
- * になる — これまでは何も報告されなかった。
1704
- *
1705
- * **挙動は変えない。** 変えるのは「黙って別の場所に描く」を「原因を指す警告」にする
1706
- * ことだけ。outlet を探す範囲を広げる案・属性で明示的に結ぶ案は、実需が出てから決める。
1707
- *
1708
- * router が outlet を自分のものにするのは「直後にある」か「直後に作った」かのどちら
1709
- * かなので、直前の要素が router でない outlet は、どの router にも使われない。その判定
1710
- * だけで足り、ほかの router の初期化の順番を待つ必要は無い。
1711
- */
1712
- const warned$1 = new WeakSet();
1713
- /**
1714
- * 直後に outlet を持たない router が outlet を作る直前に呼ぶ。同じ root(document か
1715
- * shadow root)に、どの router にも使われない `<wcs-outlet>` があれば 1 回だけ警告する。
1716
- */
1717
- function warnMisplacedOutlet(router) {
1718
- const root = router.getRootNode();
1719
- // 文書に入っていない router(root は自分自身)には、比べる相手のページが無い
1720
- if (root.nodeType !== Node.DOCUMENT_NODE && root.nodeType !== Node.DOCUMENT_FRAGMENT_NODE) {
1721
- return;
1722
- }
1723
- const routerTag = config.tagNames.router;
1724
- const outletTag = config.tagNames.outlet;
1725
- for (const outlet of root.querySelectorAll(outletTag)) {
1726
- if (warned$1.has(outlet) || outlet.previousElementSibling?.matches(routerTag)) {
1727
- continue;
1728
- }
1729
- warned$1.add(outlet);
1730
- console.warn(`[@wcstack/router] [wcs/router-outlet-misplaced] A <${routerTag}> uses only the element right after it ` +
1731
- `as its outlet, and this page has a <${outletTag}> that no router uses. The router makes an outlet of ` +
1732
- `its own right after itself, so the routes render there and the <${outletTag}> you placed stays empty. ` +
1733
- `Move the <${outletTag}> right after the <${routerTag}>.`, outlet);
1734
- }
1735
- }
1736
-
1737
1771
  /**
1738
1772
  * route commit 後のオプトイン a11y ポリシー適用(docs/a11y-design.md §3-4 / D1〜D3)。
1739
1773
  *
@@ -2263,6 +2297,16 @@ function whenLoadOrderSettled(check) {
2263
2297
  check();
2264
2298
  }
2265
2299
 
2300
+ /**
2301
+ * A navigation another one took over — a guard's redirect, a newer navigation — rejects with an
2302
+ * `AbortError` (Navigation API). That is the designed outcome, not a failure: the write surfaces
2303
+ * (`navigateUrl` / `replaceUrl`) and the guard's fallback do not report it (docs/seam-apps.md,
2304
+ * router-auth — `navigateUrl` to a guarded page logged "navigate failed: AbortError").
2305
+ */
2306
+ function isAbortError(error) {
2307
+ return error?.name === "AbortError";
2308
+ }
2309
+
2266
2310
  /** 続けて起きた guard のリダイレクトの数(guard 相を通った遷移で数え直す) */
2267
2311
  const redirectCounts = new WeakMap();
2268
2312
  /** これを超えて続くリダイレクトはループとして止める(docs/seam-apps.md、router-auth) */
@@ -2340,7 +2384,8 @@ async function runGuardPhase(routerNode, matchResult) {
2340
2384
  .catch(() => { })
2341
2385
  .then(() => routerNode.replace(e.fallbackPath))
2342
2386
  .catch((err) => {
2343
- console.error('Fallback navigation failed:', err);
2387
+ if (!isAbortError(err))
2388
+ console.error('Fallback navigation failed:', err);
2344
2389
  });
2345
2390
  return false;
2346
2391
  }
@@ -2737,6 +2782,8 @@ class Router extends HTMLElement {
2737
2782
  _leaveChecked = false;
2738
2783
  /** 離脱が確認された: 次の commit まで(その遷移の guard のリダイレクトで聞き直さない) */
2739
2784
  _leaveConfirmed = false;
2785
+ /** ページを離れるナビゲーションが確認された: beforeunload で二度聞かない(次のタスクで戻す) */
2786
+ _unloadConfirmed = false;
2740
2787
  _listeningUnload = false;
2741
2788
  _connectedCallbackPromise;
2742
2789
  _resolveConnectedCallback = null;
@@ -2848,8 +2895,9 @@ class Router extends HTMLElement {
2848
2895
  reflectAttribute(this, "basename", value);
2849
2896
  }
2850
2897
  // `leaveConfirm` は input(属性は leave-confirm)。空でない文字列の間、別のルートへ出るナビゲーション
2851
- // (リンク・戻る / 進む・navigate())の前に confirm(値) を出し、断られたら出ない。ページを閉じる・
2852
- // 読み直すときはブラウザ自身の確認(beforeunload)。未保存の変更を守る口(docs/seam-apps.md、
2898
+ // (リンク・戻る / 進む・navigate())と、ページを離れる取り消せるナビゲーション(basename の外・別の
2899
+ // オリジンへのリンク、フォームの送信)の前に confirm(値) を出し、断られたら出ない。ページを閉じる・
2900
+ // 読み直すとき(と Navigation API の無いブラウザでページを離れるとき)はブラウザ自身の確認(beforeunload)。未保存の変更を守る口(docs/seam-apps.md、
2853
2901
  // router-admin — Navigation API を直接使うしかなかった)。null は属性を外し、undefined は最初の属性へ戻す。
2854
2902
  get leaveConfirm() {
2855
2903
  return this.getAttribute("leave-confirm") || null;
@@ -2859,7 +2907,7 @@ class Router extends HTMLElement {
2859
2907
  this._syncBeforeUnload();
2860
2908
  }
2861
2909
  _onBeforeUnload = (e) => {
2862
- if (this.leaveConfirm !== null)
2910
+ if (this.leaveConfirm !== null && !this._unloadConfirmed)
2863
2911
  e.preventDefault();
2864
2912
  };
2865
2913
  /** beforeunload は leaveConfirm がある間だけ聞く(リスナーがあるだけで bfcache を外すブラウザがある) */
@@ -2885,6 +2933,33 @@ class Router extends HTMLElement {
2885
2933
  }
2886
2934
  return (this._leaveConfirmed = window.confirm(message));
2887
2935
  }
2936
+ /**
2937
+ * ページを離れるナビゲーション(どの router も受けない)の確認。leaveConfirm の文言で聞き、断られたら
2938
+ * 取り消す。受け入れたら、そのナビゲーションの beforeunload で二度聞かない(Chromium では navigate の
2939
+ * 直後、次のタスクより前に来る。遅れて来るブラウザでは二度聞くだけ)。取り消せないもの(別の文書への
2940
+ * 戻る / 進む)と最初の表示の前は聞かない — beforeunload の側に任せる。
2941
+ */
2942
+ _confirmUnload(navEvent) {
2943
+ const message = this.leaveConfirm;
2944
+ if (message === null || navEvent.cancelable === false || !this._hasCommitted || this._unloadConfirmed)
2945
+ return;
2946
+ if (!window.confirm(message)) {
2947
+ navEvent.preventDefault?.();
2948
+ return;
2949
+ }
2950
+ this._unloadConfirmed = true;
2951
+ setTimeout(() => {
2952
+ this._unloadConfirmed = false;
2953
+ }, 0);
2954
+ }
2955
+ /** 同じ文書の別の router が受ける URL か(マルチ Router: そちらの遷移はページを離れない) */
2956
+ _ownedByAnotherRouter(fullPath) {
2957
+ for (const node of document.querySelectorAll(config.tagNames.router)) {
2958
+ if (node !== this && node instanceof Router && node._isOwnPath(fullPath))
2959
+ return true;
2960
+ }
2961
+ return false;
2962
+ }
2888
2963
  _getOutlet() {
2889
2964
  // outlet は自身の直後の兄弟だけ(docs/router-outlet-placement.md)。マルチ Router で router と
2890
2965
  // outlet を 1 対 1 に結ぶための規則で、SSR の採用経路もこの位置を前提にする。
@@ -3079,7 +3154,8 @@ class Router extends HTMLElement {
3079
3154
  return;
3080
3155
  this._navigateUrl = value;
3081
3156
  this.navigate(value).catch((err) => {
3082
- console.error(`${config.tagNames.router} navigate failed:`, err);
3157
+ if (!isAbortError(err))
3158
+ console.error(`${config.tagNames.router} navigate failed:`, err);
3083
3159
  }).finally(() => {
3084
3160
  this._navigateUrl = null;
3085
3161
  this.dispatchEvent(new CustomEvent("wcs-router:navigate-url-changed", {
@@ -3104,7 +3180,8 @@ class Router extends HTMLElement {
3104
3180
  return;
3105
3181
  this._replaceUrl = value;
3106
3182
  this.replace(value).catch((err) => {
3107
- console.error(`${config.tagNames.router} replace failed:`, err);
3183
+ if (!isAbortError(err))
3184
+ console.error(`${config.tagNames.router} replace failed:`, err);
3108
3185
  }).finally(() => {
3109
3186
  this._replaceUrl = null;
3110
3187
  this.dispatchEvent(new CustomEvent("wcs-router:replace-url-changed", {
@@ -3195,19 +3272,24 @@ class Router extends HTMLElement {
3195
3272
  return fullPath === this._basename || fullPath.startsWith(this._basename + "/");
3196
3273
  }
3197
3274
  _onNavigateFunc(navEvent) {
3198
- if (!navEvent.canIntercept ||
3199
- // ページ側が取り消したナビゲーション(未保存の確認など)に intercept() を呼ぶと
3200
- // InvalidStateError が投げられる(docs/seam-apps.md、router-admin)
3201
- navEvent.defaultPrevented ||
3275
+ if (
3276
+ // ページ側が取り消したナビゲーション(未保存の確認など)に intercept() を呼ぶと
3277
+ // InvalidStateError が投げられる(docs/seam-apps.md、router-admin)
3278
+ navEvent.defaultPrevented ||
3202
3279
  navEvent.hashChange ||
3203
3280
  navEvent.downloadRequest !== null) {
3204
3281
  return;
3205
3282
  }
3206
3283
  const url = new URL(navEvent.destination.url);
3207
3284
  const fullPath = this._normalizePathname(url.pathname);
3208
- // basename 配下でない URL は無視(マルチ Router 対応)
3209
- if (!this._isOwnPath(fullPath))
3285
+ // basename 配下でない URL は受けない(マルチ Router 対応)。どの router も受けないもの(別の
3286
+ // オリジン、basename の外 — 言語を basename に持つページの言語の切り替え)はページを離れる:
3287
+ // leaveConfirm の文言で聞く(docs/seam-apps.md、router-admin)
3288
+ if (!navEvent.canIntercept || !this._isOwnPath(fullPath)) {
3289
+ if (!navEvent.canIntercept || !this._ownedByAnotherRouter(fullPath))
3290
+ this._confirmUnload(navEvent);
3210
3291
  return;
3292
+ }
3211
3293
  // 別のルートへ出る前の確認(leaveConfirm)。取り消せないナビゲーション(一部の戻る / 進む)では
3212
3294
  // 聞けない。自分の navigate() / replace() からのものは確認済み
3213
3295
  if (!this._leaveChecked && navEvent.cancelable !== false && !this._confirmLeave(fullPath)) {
@@ -4280,7 +4362,7 @@ function bootstrapRouter(config, registry) {
4280
4362
  registerComponents(registry);
4281
4363
  }
4282
4364
 
4283
- var version = "4.1.0";
4365
+ var version = "4.2.0";
4284
4366
  var pkg = {
4285
4367
  version: version};
4286
4368