@wcstack/router 4.2.0 → 4.4.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.esm.js CHANGED
@@ -1522,6 +1522,12 @@ class LayoutOutlet extends HTMLElement {
1522
1522
  }
1523
1523
  }
1524
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;
1525
1531
  if (!this._initialized) {
1526
1532
  this._disconnectedDuringInit = false;
1527
1533
  await this._initialize();
@@ -1680,14 +1686,24 @@ function _duplicateCheck(routesByPath, route) {
1680
1686
  routesByPath.set(route.absolutePath, routes);
1681
1687
  }
1682
1688
  }
1683
- async function _parseNode(routerNode, node, routes, routesByPath) {
1684
- 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) {
1685
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;
1686
1700
  const childNodes = Array.from(node.childNodes);
1687
1701
  for (const childNode of childNodes) {
1688
1702
  if (childNode.nodeType === Node.ELEMENT_NODE) {
1689
1703
  let appendNode = childNode;
1690
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;
1691
1707
  const tagName = element.tagName.toLowerCase();
1692
1708
  if (tagName === config.tagNames.route) {
1693
1709
  const childFragment = document.createDocumentFragment();
@@ -1701,21 +1717,33 @@ async function _parseNode(routerNode, node, routes, routesByPath) {
1701
1717
  const route = cloneElement;
1702
1718
  route.initialize(routerNode, routeParentNode);
1703
1719
  _duplicateCheck(routesByPath, route);
1704
- routes.push(route);
1705
1720
  appendNode = route.placeHolder;
1706
1721
  element = route;
1722
+ childParent = route;
1723
+ previousRoute = route;
1707
1724
  }
1708
1725
  else if (tagName === config.tagNames.guardHandler) {
1709
- if (routes.length > 0) {
1710
- const route = routes[routes.length - 1];
1711
- const script = element.querySelector('script[type="module"]');
1712
- if (script) {
1713
- // ハンドラがあれば guard を有効にする(`guard` 属性が無くても)。読み込みより前に —
1714
- // 最初のナビゲーションの guard 相がハンドラを待てるように。属性が無いと待たずに通し、
1715
- // 最初の表示だけ守られなかった(docs/seam-apps.md、router-auth)
1716
- route.enableGuard();
1717
- 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.`);
1718
1741
  }
1742
+ // ハンドラがあれば guard を有効にする(`guard` 属性が無くても)。読み込みより前に —
1743
+ // 最初のナビゲーションの guard 相がハンドラを待てるように。属性が無いと待たずに通し、
1744
+ // 最初の表示だけ守られなかった(docs/seam-apps.md、router-auth)
1745
+ route.enableGuard();
1746
+ loadGuardHandler(script, route);
1719
1747
  }
1720
1748
  continue;
1721
1749
  }
@@ -1748,7 +1776,7 @@ async function _parseNode(routerNode, node, routes, routesByPath) {
1748
1776
  appendNode = layoutOutlet;
1749
1777
  element = cloneElement;
1750
1778
  }
1751
- const children = await _parseNode(routerNode, element, routes, routesByPath);
1779
+ const children = await _parseNode(routerNode, element, childParent, routesByPath);
1752
1780
  // 空文字の innerHTML 代入は Trusted Types 下でも通る実装が多いが、その細目に
1753
1781
  // 寄りかからずノード操作で書く(docs/csp.md §7)。意図としても「子を全消しして
1754
1782
  // 差し替える」のほうが直接的。
@@ -1764,7 +1792,7 @@ async function _parseNode(routerNode, node, routes, routesByPath) {
1764
1792
  }
1765
1793
  async function parse(routerNode) {
1766
1794
  const routesByPath = new Map();
1767
- const fr = await _parseNode(routerNode, routerNode.template.content, [], routesByPath);
1795
+ const fr = await _parseNode(routerNode, routerNode.template.content, null, routesByPath);
1768
1796
  return fr;
1769
1797
  }
1770
1798
 
@@ -3352,6 +3380,15 @@ class Router extends HTMLElement {
3352
3380
  if (this._basename === "" && !hasBaseTag && url.pathname !== "/") {
3353
3381
  raiseError(`${config.tagNames.router} basename is empty, but current path is not "/".`);
3354
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
+ }
3355
3392
  this._outlet = this._getOutlet();
3356
3393
  this._outlet.routesNode = this;
3357
3394
  this._template = this._getTemplate();
@@ -3849,7 +3886,11 @@ class Link extends HTMLElement {
3849
3886
  raiseError(`${config.tagNames.link} is not connected to a router.`);
3850
3887
  }
3851
3888
  _initialize() {
3852
- 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";
3853
3894
  this._childNodeArray = Array.from(this.childNodes);
3854
3895
  this._path = this.getAttribute('to') || '';
3855
3896
  this._initialized = true;
@@ -3880,6 +3921,13 @@ class Link extends HTMLElement {
3880
3921
  return path.startsWith('/') || path.startsWith('?');
3881
3922
  }
3882
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
+ }
3883
3931
  if (this._isInternalTarget(path)) {
3884
3932
  // basename 結合・正規化は pathname にのみ適用し、search / hash は再結合する。
3885
3933
  // pathname 空(to="?k=v")は「現在 pathname + 指定クエリ」で組み立てる。
@@ -3905,9 +3953,17 @@ class Link extends HTMLElement {
3905
3953
  // anchor 化される。パースは同期完了するので 1 microtask 譲る。
3906
3954
  // renderToString は待機プロトコル要素(state / router)の await で
3907
3955
  // microtask を消化するため、serialize より先にこの初期化は完了する。
3956
+ // microtask の中では投げない: 捕まえる者がいないので、Node ではプロセスが落ちる — router の無い
3957
+ // ページ、URL として読めない `to` で(docs/seam-apps.md、ssr-news)。サーバーのコンソールに報告し、
3958
+ // リンクは描かない(ホストが子を持ったまま出力される。クライアントでは従来どおり throw する)
3908
3959
  queueMicrotask(() => {
3909
3960
  if (this.isConnected) {
3910
- this._connect();
3961
+ try {
3962
+ this._connect();
3963
+ }
3964
+ catch (error) {
3965
+ console.error(error);
3966
+ }
3911
3967
  }
3912
3968
  });
3913
3969
  return;
@@ -3980,7 +4036,8 @@ class Link extends HTMLElement {
3980
4036
  window.addEventListener('popstate', this._updateActiveState);
3981
4037
  // Navigation API が無い場合は、クリックで router.navigate にフォールバック
3982
4038
  // (`?` 始まりのクエリのみリンクも対象 — 素の href だとフルページ遷移になる)
3983
- if (this._isInternalTarget(this._path) && !getNavigation()?.navigate) {
4039
+ // (`to` がまだ空のリンクも: 後から来る行き先は内部のことが多い。外部ならクリック時に素通しする)
4040
+ if ((this._path === '' || this._isInternalTarget(this._path)) && !getNavigation()?.navigate) {
3984
4041
  this._onClick = async (e) => {
3985
4042
  // only left-click without modifiers
3986
4043
  if (e.defaultPrevented)
@@ -4064,8 +4121,9 @@ class Link extends HTMLElement {
4064
4121
  : pathname);
4065
4122
  // match="prefix": 下のパスでも active(サイドナビの「商品」が /products/3 でも印を付ける —
4066
4123
  // docs/seam-apps.md、router-admin)。そのページそのものではないので aria-current は "true"
4067
- const exact = currentPath === linkPath;
4068
- 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 + '/');
4069
4127
  if (this._anchorElement) {
4070
4128
  if (exact || within) {
4071
4129
  this._anchorElement.classList.add('active');
@@ -4110,7 +4168,6 @@ class Head extends HTMLElement {
4110
4168
  _childElementArray = [];
4111
4169
  constructor() {
4112
4170
  super();
4113
- this.style.display = 'none';
4114
4171
  }
4115
4172
  _initialize() {
4116
4173
  if (this._initialized) {
@@ -4118,6 +4175,14 @@ class Head extends HTMLElement {
4118
4175
  }
4119
4176
  this._initialized = true;
4120
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';
4121
4186
  for (const child of this._childElementArray) {
4122
4187
  this.removeChild(child);
4123
4188
  }
@@ -4362,7 +4427,7 @@ function bootstrapRouter(config, registry) {
4362
4427
  registerComponents(registry);
4363
4428
  }
4364
4429
 
4365
- var version = "4.2.0";
4430
+ var version = "4.4.0";
4366
4431
  var pkg = {
4367
4432
  version: version};
4368
4433