@wcstack/router 4.11.0 → 4.12.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
@@ -78,7 +78,7 @@ function setConfig(partialConfig) {
78
78
  }
79
79
 
80
80
  var name = "@wcstack/router";
81
- var version = "4.11.0";
81
+ var version = "4.12.0";
82
82
  var pkg = {
83
83
  name: name,
84
84
  version: version};
@@ -247,6 +247,20 @@ function queryIncludes(current, required) {
247
247
  function queryEquals(a, b) {
248
248
  return queryIncludes(a, b) && queryIncludes(b, a);
249
249
  }
250
+ /**
251
+ * 2 つのクエリが、名指したキーについてだけ同じ値を持つか(重複キーは同じ値の組、順序は問わない。どちらにも
252
+ * 無いキーは等しい)。`<wcs-link query-keys>` の判定 — ページもクエリにある一覧の「すべて」のタブ
253
+ * (`to="/notices?" query-keys="cat"`)が、どのページでも現在のタブになるように(docs/seam-apps.md、homeroom の取り込み)。
254
+ */
255
+ function queryKeysEqual(a, b, keys) {
256
+ const left = new URLSearchParams(a);
257
+ const right = new URLSearchParams(b);
258
+ return keys.every((key) => {
259
+ const x = left.getAll(key).sort();
260
+ const y = right.getAll(key).sort();
261
+ return x.length === y.length && x.every((value, i) => value === y[i]);
262
+ });
263
+ }
250
264
 
251
265
  /**
252
266
  * guard 判定関数に渡す第 3 引数を組み立てる。
@@ -2418,8 +2432,11 @@ function bindRouteContent(route) {
2418
2432
  * showRouteContent の相 1 であると同時に、SSR ハイドレーション
2419
2433
  * (docs/ssr-router-design.md §4 — 採用はレンダリング最適化であって認可の
2420
2434
  * スキップではない)からも同じ規則で呼ばれるため抽出した。
2435
+ *
2436
+ * `superseded` が真なら(ガードを待つ間に次のナビゲーションが始まった)、結果が通過でも
2437
+ * 拒否でも false を返し、リダイレクトもしない — 利用者はもう別の場所へ向かっている。
2421
2438
  */
2422
- async function runGuardPhase(routerNode, matchResult) {
2439
+ async function runGuardPhase(routerNode, matchResult, superseded = () => false) {
2423
2440
  try {
2424
2441
  // guard がオブジェクトを返したルートのデータを親→子の順で浅くマージし、
2425
2442
  // matchResult.data に載せる(applyRoute / SSR 採用が commit へ運ぶ)。
@@ -2431,11 +2448,15 @@ async function runGuardPhase(routerNode, matchResult) {
2431
2448
  data = data === null ? routeData : Object.assign({}, data, routeData);
2432
2449
  }
2433
2450
  }
2451
+ if (superseded())
2452
+ return false;
2434
2453
  matchResult.data = data;
2435
2454
  redirectCounts.delete(routerNode);
2436
2455
  }
2437
2456
  catch (e) {
2438
2457
  if (e instanceof GuardCancel) {
2458
+ if (superseded())
2459
+ return false;
2439
2460
  const count = (redirectCounts.get(routerNode) ?? 0) + 1;
2440
2461
  if (count > MAX_GUARD_REDIRECTS) {
2441
2462
  redirectCounts.delete(routerNode);
@@ -2475,24 +2496,36 @@ async function runGuardPhase(routerNode, matchResult) {
2475
2496
  *
2476
2497
  * `beforeChange` はガード相を通った後、変更相の前に呼ぶ(applyRoute はここで commit する)。
2477
2498
  *
2499
+ * `displayed` は画面に出ているルートの持ち主(applyRoute は outlet を渡す)。変更相はこれと
2500
+ * **当てる時点で**比べ、当てたらこれを書き換える。遷移に渡した変更は後で当たるので、
2501
+ * `lastRoutes`(呼んだ時点のルート)と比べると、前の遷移が当たる前に始まった次の
2502
+ * ナビゲーションは前の遷移の前のルートと比べてしまう — 戻るの 1 フレーム以内の進むは
2503
+ * 自分のルートを「もう出ている」と読んで何も置かず、URL と画面が食い違った
2504
+ * (docs/seam-apps.md、homeroom の取り込み)。裁定者は変更を渡された順に当てる。
2505
+ *
2506
+ * `superseded` はこのナビゲーションが後のナビゲーションに追い越されたか(runGuardPhase)。
2507
+ *
2478
2508
  * @returns ガードチェックを通過してコンテンツ表示が成立した場合 true、
2479
- * GuardCancel により中断(フォールバックへ再ナビゲート)した場合 false。
2509
+ * GuardCancel により中断(フォールバックへ再ナビゲート)した場合と、ガードを待つ間に
2510
+ * 追い越された場合は false。
2480
2511
  * 呼び出し側(applyRoute)は false の場合、router.path / outlet.lastRoutes を
2481
2512
  * 更新しないことで「拒否されたパスでの path-changed 発火」を防ぐ。
2482
2513
  */
2483
- async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChange) {
2514
+ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChange, displayed = { lastRoutes }, superseded) {
2484
2515
  // --- ガード相 ---
2485
- if (!(await runGuardPhase(routerNode, matchResult))) {
2516
+ if (!(await runGuardPhase(routerNode, matchResult, superseded))) {
2486
2517
  return false;
2487
2518
  }
2488
2519
  // 中身を描く前(applyRoute の commit — 中身のバインディングがこの遷移の出力を読めるように)
2489
2520
  beforeChange?.();
2490
2521
  // --- 変更相 ---
2491
2522
  const routesSet = new Set(matchResult.routes);
2492
- const lastRouteSet = new Set(lastRoutes);
2493
2523
  const mutate = () => {
2524
+ // 当てる時点で出ているルートと比べる(上の `displayed`)
2525
+ const previous = displayed.lastRoutes;
2526
+ const lastRouteSet = new Set(previous);
2494
2527
  // Hide previous routes
2495
- for (const route of lastRoutes) {
2528
+ for (const route of previous) {
2496
2529
  if (!routesSet.has(route)) {
2497
2530
  hideRoute(route);
2498
2531
  }
@@ -2518,10 +2551,11 @@ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChang
2518
2551
  // 挿入の後。初回描画(lastRoutes が空)の内容は state のバインド構築時に
2519
2552
  // document に居るので、そこは binder に渡す必要も報告する必要も無い。
2520
2553
  // `bind()` 自体は冪等なので渡しても壊れないが、渡さないほうが安い。
2521
- if (lastRoutes.length > 0 && !again) {
2554
+ if (previous.length > 0 && !again) {
2522
2555
  bindRouteContent(route);
2523
2556
  }
2524
2557
  }
2558
+ displayed.lastRoutes = matchResult.routes;
2525
2559
  };
2526
2560
  // 初回描画(=置き換える旧ルートが無い)は遷移に渡さない。state 側の
2527
2561
  // 「初期レンダリングは決して包まない、包むのは drain だけ」と同じ規則で、
@@ -2548,6 +2582,8 @@ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChang
2548
2582
  return true;
2549
2583
  }
2550
2584
 
2585
+ /** router ごとの、最後に始まったナビゲーションの番号(追い越されたナビゲーションを見分ける) */
2586
+ const latestNavigation = new WeakMap();
2551
2587
  /**
2552
2588
  * ルートを適用する。返り値は committed — guard 拒否(GuardCancel)で中断された
2553
2589
  * 場合のみ false。呼び出し側はこれで commit 後の処理(フォールバック経路の
@@ -2559,6 +2595,13 @@ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChang
2559
2595
  */
2560
2596
  async function applyRoute(routerNode, outlet, fullPath, lastPath, search = "") {
2561
2597
  const path = sliceBasename(fullPath, routerNode.basename);
2598
+ // このナビゲーションの番号。ガードを待つ間に次のナビゲーションが始まったら(読み込み中に別のリンク)、
2599
+ // ガード相の後で何もせずに降りる — 当てると、URL は後のナビゲーションのまま前のルートが画面に出た
2600
+ // (4.11 までは両方のルートが出た。docs/seam-apps.md、homeroom の取り込み)。クエリだけの
2601
+ // ナビゲーションも追い越す
2602
+ const navigation = (latestNavigation.get(routerNode) ?? 0) + 1;
2603
+ latestNavigation.set(routerNode, navigation);
2604
+ const superseded = () => latestNavigation.get(routerNode) !== navigation;
2562
2605
  // same-match 高速パス(docs/router-state-contract-design.md §4.4)。
2563
2606
  // guard はルートへの進入を守るものであり、クエリ変化は進入ではない —
2564
2607
  // matchRoutes / guard 相 / showRouteContent をスキップし、transition-runner にも
@@ -2598,7 +2641,9 @@ async function applyRoute(routerNode, outlet, fullPath, lastPath, search = "") {
2598
2641
  const lastRoutes = outlet.lastRoutes;
2599
2642
  // guard 相を通ったら、中身を描く前に commit する(path-changed だけ描いた後)。中身の
2600
2643
  // バインディングが最初からこの遷移の params / data を読めるように(Router.commitNavigation)。
2601
- // GuardCancel で中断された場合は commit しない(拒否されたパスでの発火を防ぐため)
2644
+ // GuardCancel で中断された場合は commit しない(拒否されたパスでの発火を防ぐため)。
2645
+ // outlet.lastRoutes は変更相が当てたときに書き換える(遷移を待った後のここではない — 遷移の中で
2646
+ // 待つ間に次のナビゲーションが当てたルートを、前のナビゲーションが古い値で上書きする)
2602
2647
  const committed = await showRouteContent(routerNode, matchResult, lastRoutes, () => {
2603
2648
  // routeName は最深マッチの name。fallback 時は fallback ルートの name(D8)。
2604
2649
  routerNode.commitNavigation({
@@ -2610,11 +2655,14 @@ async function applyRoute(routerNode, outlet, fullPath, lastPath, search = "") {
2610
2655
  search,
2611
2656
  path,
2612
2657
  }, true);
2613
- });
2658
+ }, outlet, superseded);
2614
2659
  if (!committed)
2615
2660
  return false;
2616
2661
  routerNode.releasePath();
2617
- outlet.lastRoutes = matchResult.routes;
2662
+ // 遷移を待つ間に追い越された(戻るの直後の進む): 変更は当たっているが、フォーカスとアナウンスは
2663
+ // 後のナビゲーションのもの
2664
+ if (superseded())
2665
+ return true;
2618
2666
  // オプトインの focus/announce は commit 直後・mutate() の外で適用する(D3)。
2619
2667
  // 初回描画(lastRoutes が空)では動かない — ページロードはブラウザの担当で、
2620
2668
  // view-transition の「初回は包まない」と同じ判定・同じ理由(§3-5)。
@@ -4067,7 +4115,7 @@ class Router extends HTMLElement {
4067
4115
  const MIRRORED_ATTRIBUTES = ['title', 'rel', 'target', 'download', 'hreflang', 'lang', 'dir'];
4068
4116
  class Link extends HTMLElement {
4069
4117
  static get observedAttributes() {
4070
- return ['to', 'match', ...MIRRORED_ATTRIBUTES];
4118
+ return ['to', 'match', 'query-keys', ...MIRRORED_ATTRIBUTES];
4071
4119
  }
4072
4120
  _childNodeArray = [];
4073
4121
  _uuid = getUUID();
@@ -4302,7 +4350,7 @@ class Link extends HTMLElement {
4302
4350
  this._router = null;
4303
4351
  }
4304
4352
  attributeChangedCallback(name, oldValue, newValue) {
4305
- if (name === 'match' && oldValue !== newValue) {
4353
+ if ((name === 'match' || name === 'query-keys') && oldValue !== newValue) {
4306
4354
  // anchor 生成前(upgrade の属性の通知 — まだ router の下に居ないことがある)は接続時の判定に任せる
4307
4355
  if (this._anchorElement) {
4308
4356
  this._updateActiveState();
@@ -4343,12 +4391,18 @@ class Link extends HTMLElement {
4343
4391
  // `?` で終わる `to`(`to="/notices?"`、`to="?"` — 明示した空のクエリ)もクエリのある `to` で、そのページは
4344
4392
  // クエリの無いページだけ(href からは `?` を落とす)。クエリの無い `to` と同じに pathname だけで比べていたので、
4345
4393
  // 「すべて」のタブが `/notices?cat=event` でも現在のページで、現在のリンクが 2 つになった(docs/seam-apps.md、homeroom)
4394
+ //
4395
+ // `query-keys` はクエリのうち比べるキーを名指す。そのページそのもの("page")は今までどおりクエリ全体が等しいとき、
4396
+ // 名指したキーだけが等しければ "true"(どちらにも無いキーは等しい)。クエリの無い `to` もキーで比べる。ページも
4397
+ // クエリにある一覧では、`to="/notices?"` の「すべて」のタブは 1 ページ目でしか現在にならず、`match="prefix"` なら
4398
+ // どの区分でも現在だった — `query-keys="cat"` ならどのページでも「すべて」だけ(docs/seam-apps.md、homeroom の取り込み)
4346
4399
  const current = new URL(window.location.href);
4347
4400
  const currentPath = this._normalizePathname(current.pathname);
4348
4401
  const { pathname, search } = splitUrlTarget(this._path);
4349
4402
  const wanted = effectiveSearch(search);
4403
+ const keys = (this.getAttribute('query-keys') ?? '').split(/[\s,]+/).filter((key) => key !== '');
4350
4404
  const queryIncluded = queryIncludes(current.search, wanted);
4351
- const queryEqual = search === '' || queryEquals(current.search, wanted);
4405
+ const queryEqual = (search === '' && keys.length === 0) || queryEquals(current.search, wanted);
4352
4406
  const linkPath = this._normalizePathname(this._isInternalTarget(this._path)
4353
4407
  ? (pathname === ""
4354
4408
  ? window.location.pathname
@@ -4359,9 +4413,12 @@ class Link extends HTMLElement {
4359
4413
  // aria-current は "true"
4360
4414
  // 行き先の無いリンク(`to` が空)はどこでも active ではない(空のパスは "/" に正規化される)
4361
4415
  const samePath = currentPath === linkPath;
4416
+ const prefix = this.getAttribute('match') === 'prefix';
4417
+ const below = prefix && currentPath.startsWith(linkPath + '/');
4362
4418
  const exact = this._path !== '' && samePath && queryEqual;
4363
- const within = !exact && this._path !== '' && queryIncluded && this.getAttribute('match') === 'prefix'
4364
- && (samePath || currentPath.startsWith(linkPath + '/'));
4419
+ const within = !exact && this._path !== '' && (samePath || below) && (keys.length > 0
4420
+ ? queryKeysEqual(current.search, wanted, keys)
4421
+ : prefix && queryIncluded);
4365
4422
  if (this._anchorElement) {
4366
4423
  if (exact || within) {
4367
4424
  this._anchorElement.classList.add('active');