@wcstack/router 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
@@ -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 は
@@ -1446,6 +1522,12 @@ class LayoutOutlet extends HTMLElement {
1446
1522
  }
1447
1523
  }
1448
1524
  async connectedCallback() {
1525
+ // レイアウトの無い outlet: サーバーが描いた <wcs-layout-outlet> がクライアントで upgrade された(router は
1526
+ // レイアウトを採用せず、内容を描き直して置き換える — README「Server-side rendering」)。何もしない。
1527
+ // 以前は `layout` の getter が throw し、深いリンクのたびに捕まらないエラーが出ていた(docs/seam-apps.md、
1528
+ // ssr-news)。outlet は router が作るもので、ページには書かない
1529
+ if (this._layout === null)
1530
+ return;
1449
1531
  if (!this._initialized) {
1450
1532
  this._disconnectedDuringInit = false;
1451
1533
  await this._initialize();
@@ -1604,14 +1686,24 @@ function _duplicateCheck(routesByPath, route) {
1604
1686
  routesByPath.set(route.absolutePath, routes);
1605
1687
  }
1606
1688
  }
1607
- async function _parseNode(routerNode, node, routes, routesByPath) {
1608
- const routeParentNode = routes.length > 0 ? routes[routes.length - 1] : null;
1689
+ /**
1690
+ * `routeParentNode` は `node` を囲む最も内側の `<wcs-route>`(無ければ null)。再帰の引数で渡す —
1691
+ * それまでに解析したルートの一覧の最後で決めると、前の兄弟(とその子孫)が親に化けた: `<div>` や
1692
+ * `<wcs-layout>` の中のルートが前のルートの下のパスになるか、絶対パスなら「親がある」で落ち、
1693
+ * 入れ子のルートの後に置いた `<wcs-guard-handler>` は外側でなく子のルートを守った
1694
+ * (docs/mistake-catalogue.md)。
1695
+ */
1696
+ async function _parseNode(routerNode, node, routeParentNode, routesByPath) {
1609
1697
  const fragment = document.createDocumentFragment();
1698
+ // the last <wcs-route> among node's children so far: the route an outside <wcs-guard-handler> after it guarded
1699
+ let previousRoute = null;
1610
1700
  const childNodes = Array.from(node.childNodes);
1611
1701
  for (const childNode of childNodes) {
1612
1702
  if (childNode.nodeType === Node.ELEMENT_NODE) {
1613
1703
  let appendNode = childNode;
1614
1704
  let element = childNode;
1705
+ // the route the children are parsed under: this one when it is a <wcs-route>, else the enclosing one
1706
+ let childParent = routeParentNode;
1615
1707
  const tagName = element.tagName.toLowerCase();
1616
1708
  if (tagName === config.tagNames.route) {
1617
1709
  const childFragment = document.createDocumentFragment();
@@ -1625,21 +1717,33 @@ async function _parseNode(routerNode, node, routes, routesByPath) {
1625
1717
  const route = cloneElement;
1626
1718
  route.initialize(routerNode, routeParentNode);
1627
1719
  _duplicateCheck(routesByPath, route);
1628
- routes.push(route);
1629
1720
  appendNode = route.placeHolder;
1630
1721
  element = route;
1722
+ childParent = route;
1723
+ previousRoute = route;
1631
1724
  }
1632
1725
  else if (tagName === config.tagNames.guardHandler) {
1633
- if (routes.length > 0) {
1634
- const route = routes[routes.length - 1];
1635
- const script = element.querySelector('script[type="module"]');
1636
- if (script) {
1637
- // ハンドラがあれば guard を有効にする(`guard` 属性が無くても)。読み込みより前に —
1638
- // 最初のナビゲーションの guard 相がハンドラを待てるように。属性が無いと待たずに通し、
1639
- // 最初の表示だけ守られなかった(docs/seam-apps.md、router-auth)
1640
- route.enableGuard();
1641
- loadGuardHandler(script, route);
1726
+ // 囲むルートを守る(子のルートの前でも後でも)。ルートの外のものは、同じ階層の直前のルートを守る —
1727
+ // 以前は「直前に解析したルート」に付いたので、その形で守ってきたページを、更新で黙って開けないため
1728
+ // (README は外に置けば無視されると書いていた)。どちらでもない形と、守れない形は黙らない — その
1729
+ // ルートは守られず、保護したページが見えるので console.error(docs/mistake-catalogue.md)
1730
+ const route = routeParentNode ?? previousRoute;
1731
+ const script = element.querySelector('script[type="module"]');
1732
+ if (route === null) {
1733
+ console.error(`[@wcstack/router] [wcs/guard-handler-ignored] A <${config.tagNames.guardHandler}> outside a <${config.tagNames.route}> guards nothing: put it inside the route it guards.`);
1734
+ }
1735
+ else if (!script) {
1736
+ console.error(`[@wcstack/router] [wcs/guard-handler-ignored] The <${config.tagNames.guardHandler}> of route "${route.absolutePath}" has no <script type="module">, so the route is not guarded.`);
1737
+ }
1738
+ else {
1739
+ if (routeParentNode === null) {
1740
+ console.error(`[@wcstack/router] [wcs/guard-handler-ignored] A <${config.tagNames.guardHandler}> outside a <${config.tagNames.route}> guards the route before it, "${route.absolutePath}": put it inside that route.`);
1642
1741
  }
1742
+ // ハンドラがあれば guard を有効にする(`guard` 属性が無くても)。読み込みより前に —
1743
+ // 最初のナビゲーションの guard 相がハンドラを待てるように。属性が無いと待たずに通し、
1744
+ // 最初の表示だけ守られなかった(docs/seam-apps.md、router-auth)
1745
+ route.enableGuard();
1746
+ loadGuardHandler(script, route);
1643
1747
  }
1644
1748
  continue;
1645
1749
  }
@@ -1672,7 +1776,7 @@ async function _parseNode(routerNode, node, routes, routesByPath) {
1672
1776
  appendNode = layoutOutlet;
1673
1777
  element = cloneElement;
1674
1778
  }
1675
- const children = await _parseNode(routerNode, element, routes, routesByPath);
1779
+ const children = await _parseNode(routerNode, element, childParent, routesByPath);
1676
1780
  // 空文字の innerHTML 代入は Trusted Types 下でも通る実装が多いが、その細目に
1677
1781
  // 寄りかからずノード操作で書く(docs/csp.md §7)。意図としても「子を全消しして
1678
1782
  // 差し替える」のほうが直接的。
@@ -1688,52 +1792,10 @@ async function _parseNode(routerNode, node, routes, routesByPath) {
1688
1792
  }
1689
1793
  async function parse(routerNode) {
1690
1794
  const routesByPath = new Map();
1691
- const fr = await _parseNode(routerNode, routerNode.template.content, [], routesByPath);
1795
+ const fr = await _parseNode(routerNode, routerNode.template.content, null, routesByPath);
1692
1796
  return fr;
1693
1797
  }
1694
1798
 
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
1799
  /**
1738
1800
  * route commit 後のオプトイン a11y ポリシー適用(docs/a11y-design.md §3-4 / D1〜D3)。
1739
1801
  *
@@ -2748,6 +2810,8 @@ class Router extends HTMLElement {
2748
2810
  _leaveChecked = false;
2749
2811
  /** 離脱が確認された: 次の commit まで(その遷移の guard のリダイレクトで聞き直さない) */
2750
2812
  _leaveConfirmed = false;
2813
+ /** ページを離れるナビゲーションが確認された: beforeunload で二度聞かない(次のタスクで戻す) */
2814
+ _unloadConfirmed = false;
2751
2815
  _listeningUnload = false;
2752
2816
  _connectedCallbackPromise;
2753
2817
  _resolveConnectedCallback = null;
@@ -2859,8 +2923,9 @@ class Router extends HTMLElement {
2859
2923
  reflectAttribute(this, "basename", value);
2860
2924
  }
2861
2925
  // `leaveConfirm` は input(属性は leave-confirm)。空でない文字列の間、別のルートへ出るナビゲーション
2862
- // (リンク・戻る / 進む・navigate())の前に confirm(値) を出し、断られたら出ない。ページを閉じる・
2863
- // 読み直すときはブラウザ自身の確認(beforeunload)。未保存の変更を守る口(docs/seam-apps.md、
2926
+ // (リンク・戻る / 進む・navigate())と、ページを離れる取り消せるナビゲーション(basename の外・別の
2927
+ // オリジンへのリンク、フォームの送信)の前に confirm(値) を出し、断られたら出ない。ページを閉じる・
2928
+ // 読み直すとき(と Navigation API の無いブラウザでページを離れるとき)はブラウザ自身の確認(beforeunload)。未保存の変更を守る口(docs/seam-apps.md、
2864
2929
  // router-admin — Navigation API を直接使うしかなかった)。null は属性を外し、undefined は最初の属性へ戻す。
2865
2930
  get leaveConfirm() {
2866
2931
  return this.getAttribute("leave-confirm") || null;
@@ -2870,7 +2935,7 @@ class Router extends HTMLElement {
2870
2935
  this._syncBeforeUnload();
2871
2936
  }
2872
2937
  _onBeforeUnload = (e) => {
2873
- if (this.leaveConfirm !== null)
2938
+ if (this.leaveConfirm !== null && !this._unloadConfirmed)
2874
2939
  e.preventDefault();
2875
2940
  };
2876
2941
  /** beforeunload は leaveConfirm がある間だけ聞く(リスナーがあるだけで bfcache を外すブラウザがある) */
@@ -2896,6 +2961,33 @@ class Router extends HTMLElement {
2896
2961
  }
2897
2962
  return (this._leaveConfirmed = window.confirm(message));
2898
2963
  }
2964
+ /**
2965
+ * ページを離れるナビゲーション(どの router も受けない)の確認。leaveConfirm の文言で聞き、断られたら
2966
+ * 取り消す。受け入れたら、そのナビゲーションの beforeunload で二度聞かない(Chromium では navigate の
2967
+ * 直後、次のタスクより前に来る。遅れて来るブラウザでは二度聞くだけ)。取り消せないもの(別の文書への
2968
+ * 戻る / 進む)と最初の表示の前は聞かない — beforeunload の側に任せる。
2969
+ */
2970
+ _confirmUnload(navEvent) {
2971
+ const message = this.leaveConfirm;
2972
+ if (message === null || navEvent.cancelable === false || !this._hasCommitted || this._unloadConfirmed)
2973
+ return;
2974
+ if (!window.confirm(message)) {
2975
+ navEvent.preventDefault?.();
2976
+ return;
2977
+ }
2978
+ this._unloadConfirmed = true;
2979
+ setTimeout(() => {
2980
+ this._unloadConfirmed = false;
2981
+ }, 0);
2982
+ }
2983
+ /** 同じ文書の別の router が受ける URL か(マルチ Router: そちらの遷移はページを離れない) */
2984
+ _ownedByAnotherRouter(fullPath) {
2985
+ for (const node of document.querySelectorAll(config.tagNames.router)) {
2986
+ if (node !== this && node instanceof Router && node._isOwnPath(fullPath))
2987
+ return true;
2988
+ }
2989
+ return false;
2990
+ }
2899
2991
  _getOutlet() {
2900
2992
  // outlet は自身の直後の兄弟だけ(docs/router-outlet-placement.md)。マルチ Router で router と
2901
2993
  // outlet を 1 対 1 に結ぶための規則で、SSR の採用経路もこの位置を前提にする。
@@ -3208,19 +3300,24 @@ class Router extends HTMLElement {
3208
3300
  return fullPath === this._basename || fullPath.startsWith(this._basename + "/");
3209
3301
  }
3210
3302
  _onNavigateFunc(navEvent) {
3211
- if (!navEvent.canIntercept ||
3212
- // ページ側が取り消したナビゲーション(未保存の確認など)に intercept() を呼ぶと
3213
- // InvalidStateError が投げられる(docs/seam-apps.md、router-admin)
3214
- navEvent.defaultPrevented ||
3303
+ if (
3304
+ // ページ側が取り消したナビゲーション(未保存の確認など)に intercept() を呼ぶと
3305
+ // InvalidStateError が投げられる(docs/seam-apps.md、router-admin)
3306
+ navEvent.defaultPrevented ||
3215
3307
  navEvent.hashChange ||
3216
3308
  navEvent.downloadRequest !== null) {
3217
3309
  return;
3218
3310
  }
3219
3311
  const url = new URL(navEvent.destination.url);
3220
3312
  const fullPath = this._normalizePathname(url.pathname);
3221
- // basename 配下でない URL は無視(マルチ Router 対応)
3222
- if (!this._isOwnPath(fullPath))
3313
+ // basename 配下でない URL は受けない(マルチ Router 対応)。どの router も受けないもの(別の
3314
+ // オリジン、basename の外 — 言語を basename に持つページの言語の切り替え)はページを離れる:
3315
+ // leaveConfirm の文言で聞く(docs/seam-apps.md、router-admin)
3316
+ if (!navEvent.canIntercept || !this._isOwnPath(fullPath)) {
3317
+ if (!navEvent.canIntercept || !this._ownedByAnotherRouter(fullPath))
3318
+ this._confirmUnload(navEvent);
3223
3319
  return;
3320
+ }
3224
3321
  // 別のルートへ出る前の確認(leaveConfirm)。取り消せないナビゲーション(一部の戻る / 進む)では
3225
3322
  // 聞けない。自分の navigate() / replace() からのものは確認済み
3226
3323
  if (!this._leaveChecked && navEvent.cancelable !== false && !this._confirmLeave(fullPath)) {
@@ -3283,6 +3380,15 @@ class Router extends HTMLElement {
3283
3380
  if (this._basename === "" && !hasBaseTag && url.pathname !== "/") {
3284
3381
  raiseError(`${config.tagNames.router} basename is empty, but current path is not "/".`);
3285
3382
  }
3383
+ // basename の属性も <base href> も無いと、basename は今のページの URL から導かれる — アプリの根でない
3384
+ // URL(ディープリンク・再読み込み)では、そのページのパスが basename になり、根のルートが表示され、
3385
+ // リンクもその下で解決される。lint と同じ code で知らせる(docs/mistake-catalogue.md)
3386
+ if (!ssr && this._basename !== "" && !hasBaseTag && !this.getAttribute("basename")) {
3387
+ console.warn(`[@wcstack/router] [wcs/base-href-missing] The basename "${this._basename}" was taken from this page's URL: ` +
3388
+ `the page has no <base href> and <${config.tagNames.router}> no basename, so routes and links resolve under it, ` +
3389
+ `and a deep link or a reload away from the app's root shows the wrong route. ` +
3390
+ `Add <base href="/"> (or the app's folder) to <head>.`);
3391
+ }
3286
3392
  this._outlet = this._getOutlet();
3287
3393
  this._outlet.routesNode = this;
3288
3394
  this._template = this._getTemplate();
@@ -3780,7 +3886,11 @@ class Link extends HTMLElement {
3780
3886
  raiseError(`${config.tagNames.link} is not connected to a router.`);
3781
3887
  }
3782
3888
  _initialize() {
3783
- this.style.display = "none";
3889
+ // サーバーでは書かない: 直列化されると style 属性になり、厳しい style-src が拒む(docs/seam-apps.md、
3890
+ // ssr-news)。サーバーのホストは子を anchor へ移して空なので、隠さなくても見えない。
3891
+ // クライアントは upgrade でここを通り、CSSOM で書く
3892
+ if (!inSsr())
3893
+ this.style.display = "none";
3784
3894
  this._childNodeArray = Array.from(this.childNodes);
3785
3895
  this._path = this.getAttribute('to') || '';
3786
3896
  this._initialized = true;
@@ -3811,6 +3921,13 @@ class Link extends HTMLElement {
3811
3921
  return path.startsWith('/') || path.startsWith('?');
3812
3922
  }
3813
3923
  _setAnchorHref(anchor, path) {
3924
+ if (path === '') {
3925
+ // 行き先がまだ無い: `attr.to:` の束縛が書く前に接続した(router を state より先に読むページ、
3926
+ // SSR の描き直す行 — docs/seam-apps.md、ssr-news)。href の無い <a> にして、`to` が来たら
3927
+ // attributeChangedCallback が書く。以前は外部 URL として throw し、anchor を作らずに終わっていた
3928
+ anchor.removeAttribute('href');
3929
+ return;
3930
+ }
3814
3931
  if (this._isInternalTarget(path)) {
3815
3932
  // basename 結合・正規化は pathname にのみ適用し、search / hash は再結合する。
3816
3933
  // pathname 空(to="?k=v")は「現在 pathname + 指定クエリ」で組み立てる。
@@ -3836,9 +3953,17 @@ class Link extends HTMLElement {
3836
3953
  // anchor 化される。パースは同期完了するので 1 microtask 譲る。
3837
3954
  // renderToString は待機プロトコル要素(state / router)の await で
3838
3955
  // microtask を消化するため、serialize より先にこの初期化は完了する。
3956
+ // microtask の中では投げない: 捕まえる者がいないので、Node ではプロセスが落ちる — router の無い
3957
+ // ページ、URL として読めない `to` で(docs/seam-apps.md、ssr-news)。サーバーのコンソールに報告し、
3958
+ // リンクは描かない(ホストが子を持ったまま出力される。クライアントでは従来どおり throw する)
3839
3959
  queueMicrotask(() => {
3840
3960
  if (this.isConnected) {
3841
- this._connect();
3961
+ try {
3962
+ this._connect();
3963
+ }
3964
+ catch (error) {
3965
+ console.error(error);
3966
+ }
3842
3967
  }
3843
3968
  });
3844
3969
  return;
@@ -3911,7 +4036,8 @@ class Link extends HTMLElement {
3911
4036
  window.addEventListener('popstate', this._updateActiveState);
3912
4037
  // Navigation API が無い場合は、クリックで router.navigate にフォールバック
3913
4038
  // (`?` 始まりのクエリのみリンクも対象 — 素の href だとフルページ遷移になる)
3914
- if (this._isInternalTarget(this._path) && !getNavigation()?.navigate) {
4039
+ // (`to` がまだ空のリンクも: 後から来る行き先は内部のことが多い。外部ならクリック時に素通しする)
4040
+ if ((this._path === '' || this._isInternalTarget(this._path)) && !getNavigation()?.navigate) {
3915
4041
  this._onClick = async (e) => {
3916
4042
  // only left-click without modifiers
3917
4043
  if (e.defaultPrevented)
@@ -3995,8 +4121,9 @@ class Link extends HTMLElement {
3995
4121
  : pathname);
3996
4122
  // match="prefix": 下のパスでも active(サイドナビの「商品」が /products/3 でも印を付ける —
3997
4123
  // docs/seam-apps.md、router-admin)。そのページそのものではないので aria-current は "true"
3998
- const exact = currentPath === linkPath;
3999
- const within = !exact && this.getAttribute('match') === 'prefix' && currentPath.startsWith(linkPath + '/');
4124
+ // 行き先の無いリンク(`to` が空)はどこでも active ではない(空のパスは "/" に正規化される)
4125
+ const exact = this._path !== '' && currentPath === linkPath;
4126
+ const within = !exact && this._path !== '' && this.getAttribute('match') === 'prefix' && currentPath.startsWith(linkPath + '/');
4000
4127
  if (this._anchorElement) {
4001
4128
  if (exact || within) {
4002
4129
  this._anchorElement.classList.add('active');
@@ -4041,7 +4168,6 @@ class Head extends HTMLElement {
4041
4168
  _childElementArray = [];
4042
4169
  constructor() {
4043
4170
  super();
4044
- this.style.display = 'none';
4045
4171
  }
4046
4172
  _initialize() {
4047
4173
  if (this._initialized) {
@@ -4049,6 +4175,14 @@ class Head extends HTMLElement {
4049
4175
  }
4050
4176
  this._initialized = true;
4051
4177
  this._childElementArray = Array.from(this.children);
4178
+ // サーバーでは子を残す: レンダリングの document.head は直列化されない(@wcstack/server は body だけを
4179
+ // 返す)ので、子を移すと出力から消え、クライアントが採用したルートの <wcs-head> は空のまま —
4180
+ // そのセッションの間 <title> が二度と変わらなかった(docs/seam-apps.md、ssr-news)。残した子は
4181
+ // クライアントの upgrade がここで引き取る。<title> / <meta> は既定で表示されない。
4182
+ // 隠すのもクライアントだけ(サーバーで書くと style 属性になり、厳しい style-src が拒む)
4183
+ if (inSsr())
4184
+ return;
4185
+ this.style.display = 'none';
4052
4186
  for (const child of this._childElementArray) {
4053
4187
  this.removeChild(child);
4054
4188
  }
@@ -4293,7 +4427,7 @@ function bootstrapRouter(config, registry) {
4293
4427
  registerComponents(registry);
4294
4428
  }
4295
4429
 
4296
- var version = "4.1.1";
4430
+ var version = "4.3.0";
4297
4431
  var pkg = {
4298
4432
  version: version};
4299
4433