@wcstack/router 4.11.0 → 4.11.2

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.11.2";
82
82
  var pkg = {
83
83
  name: name,
84
84
  version: version};
@@ -2418,8 +2418,11 @@ function bindRouteContent(route) {
2418
2418
  * showRouteContent の相 1 であると同時に、SSR ハイドレーション
2419
2419
  * (docs/ssr-router-design.md §4 — 採用はレンダリング最適化であって認可の
2420
2420
  * スキップではない)からも同じ規則で呼ばれるため抽出した。
2421
+ *
2422
+ * `superseded` が真なら(ガードを待つ間に次のナビゲーションが始まった)、結果が通過でも
2423
+ * 拒否でも false を返し、リダイレクトもしない — 利用者はもう別の場所へ向かっている。
2421
2424
  */
2422
- async function runGuardPhase(routerNode, matchResult) {
2425
+ async function runGuardPhase(routerNode, matchResult, superseded = () => false) {
2423
2426
  try {
2424
2427
  // guard がオブジェクトを返したルートのデータを親→子の順で浅くマージし、
2425
2428
  // matchResult.data に載せる(applyRoute / SSR 採用が commit へ運ぶ)。
@@ -2431,11 +2434,15 @@ async function runGuardPhase(routerNode, matchResult) {
2431
2434
  data = data === null ? routeData : Object.assign({}, data, routeData);
2432
2435
  }
2433
2436
  }
2437
+ if (superseded())
2438
+ return false;
2434
2439
  matchResult.data = data;
2435
2440
  redirectCounts.delete(routerNode);
2436
2441
  }
2437
2442
  catch (e) {
2438
2443
  if (e instanceof GuardCancel) {
2444
+ if (superseded())
2445
+ return false;
2439
2446
  const count = (redirectCounts.get(routerNode) ?? 0) + 1;
2440
2447
  if (count > MAX_GUARD_REDIRECTS) {
2441
2448
  redirectCounts.delete(routerNode);
@@ -2475,24 +2482,36 @@ async function runGuardPhase(routerNode, matchResult) {
2475
2482
  *
2476
2483
  * `beforeChange` はガード相を通った後、変更相の前に呼ぶ(applyRoute はここで commit する)。
2477
2484
  *
2485
+ * `displayed` は画面に出ているルートの持ち主(applyRoute は outlet を渡す)。変更相はこれと
2486
+ * **当てる時点で**比べ、当てたらこれを書き換える。遷移に渡した変更は後で当たるので、
2487
+ * `lastRoutes`(呼んだ時点のルート)と比べると、前の遷移が当たる前に始まった次の
2488
+ * ナビゲーションは前の遷移の前のルートと比べてしまう — 戻るの 1 フレーム以内の進むは
2489
+ * 自分のルートを「もう出ている」と読んで何も置かず、URL と画面が食い違った
2490
+ * (docs/seam-apps.md、homeroom の取り込み)。裁定者は変更を渡された順に当てる。
2491
+ *
2492
+ * `superseded` はこのナビゲーションが後のナビゲーションに追い越されたか(runGuardPhase)。
2493
+ *
2478
2494
  * @returns ガードチェックを通過してコンテンツ表示が成立した場合 true、
2479
- * GuardCancel により中断(フォールバックへ再ナビゲート)した場合 false。
2495
+ * GuardCancel により中断(フォールバックへ再ナビゲート)した場合と、ガードを待つ間に
2496
+ * 追い越された場合は false。
2480
2497
  * 呼び出し側(applyRoute)は false の場合、router.path / outlet.lastRoutes を
2481
2498
  * 更新しないことで「拒否されたパスでの path-changed 発火」を防ぐ。
2482
2499
  */
2483
- async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChange) {
2500
+ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChange, displayed = { lastRoutes }, superseded) {
2484
2501
  // --- ガード相 ---
2485
- if (!(await runGuardPhase(routerNode, matchResult))) {
2502
+ if (!(await runGuardPhase(routerNode, matchResult, superseded))) {
2486
2503
  return false;
2487
2504
  }
2488
2505
  // 中身を描く前(applyRoute の commit — 中身のバインディングがこの遷移の出力を読めるように)
2489
2506
  beforeChange?.();
2490
2507
  // --- 変更相 ---
2491
2508
  const routesSet = new Set(matchResult.routes);
2492
- const lastRouteSet = new Set(lastRoutes);
2493
2509
  const mutate = () => {
2510
+ // 当てる時点で出ているルートと比べる(上の `displayed`)
2511
+ const previous = displayed.lastRoutes;
2512
+ const lastRouteSet = new Set(previous);
2494
2513
  // Hide previous routes
2495
- for (const route of lastRoutes) {
2514
+ for (const route of previous) {
2496
2515
  if (!routesSet.has(route)) {
2497
2516
  hideRoute(route);
2498
2517
  }
@@ -2518,10 +2537,11 @@ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChang
2518
2537
  // 挿入の後。初回描画(lastRoutes が空)の内容は state のバインド構築時に
2519
2538
  // document に居るので、そこは binder に渡す必要も報告する必要も無い。
2520
2539
  // `bind()` 自体は冪等なので渡しても壊れないが、渡さないほうが安い。
2521
- if (lastRoutes.length > 0 && !again) {
2540
+ if (previous.length > 0 && !again) {
2522
2541
  bindRouteContent(route);
2523
2542
  }
2524
2543
  }
2544
+ displayed.lastRoutes = matchResult.routes;
2525
2545
  };
2526
2546
  // 初回描画(=置き換える旧ルートが無い)は遷移に渡さない。state 側の
2527
2547
  // 「初期レンダリングは決して包まない、包むのは drain だけ」と同じ規則で、
@@ -2548,6 +2568,8 @@ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChang
2548
2568
  return true;
2549
2569
  }
2550
2570
 
2571
+ /** router ごとの、最後に始まったナビゲーションの番号(追い越されたナビゲーションを見分ける) */
2572
+ const latestNavigation = new WeakMap();
2551
2573
  /**
2552
2574
  * ルートを適用する。返り値は committed — guard 拒否(GuardCancel)で中断された
2553
2575
  * 場合のみ false。呼び出し側はこれで commit 後の処理(フォールバック経路の
@@ -2559,6 +2581,13 @@ async function showRouteContent(routerNode, matchResult, lastRoutes, beforeChang
2559
2581
  */
2560
2582
  async function applyRoute(routerNode, outlet, fullPath, lastPath, search = "") {
2561
2583
  const path = sliceBasename(fullPath, routerNode.basename);
2584
+ // このナビゲーションの番号。ガードを待つ間に次のナビゲーションが始まったら(読み込み中に別のリンク)、
2585
+ // ガード相の後で何もせずに降りる — 当てると、URL は後のナビゲーションのまま前のルートが画面に出た
2586
+ // (4.11 までは両方のルートが出た。docs/seam-apps.md、homeroom の取り込み)。クエリだけの
2587
+ // ナビゲーションも追い越す
2588
+ const navigation = (latestNavigation.get(routerNode) ?? 0) + 1;
2589
+ latestNavigation.set(routerNode, navigation);
2590
+ const superseded = () => latestNavigation.get(routerNode) !== navigation;
2562
2591
  // same-match 高速パス(docs/router-state-contract-design.md §4.4)。
2563
2592
  // guard はルートへの進入を守るものであり、クエリ変化は進入ではない —
2564
2593
  // matchRoutes / guard 相 / showRouteContent をスキップし、transition-runner にも
@@ -2598,7 +2627,9 @@ async function applyRoute(routerNode, outlet, fullPath, lastPath, search = "") {
2598
2627
  const lastRoutes = outlet.lastRoutes;
2599
2628
  // guard 相を通ったら、中身を描く前に commit する(path-changed だけ描いた後)。中身の
2600
2629
  // バインディングが最初からこの遷移の params / data を読めるように(Router.commitNavigation)。
2601
- // GuardCancel で中断された場合は commit しない(拒否されたパスでの発火を防ぐため)
2630
+ // GuardCancel で中断された場合は commit しない(拒否されたパスでの発火を防ぐため)。
2631
+ // outlet.lastRoutes は変更相が当てたときに書き換える(遷移を待った後のここではない — 遷移の中で
2632
+ // 待つ間に次のナビゲーションが当てたルートを、前のナビゲーションが古い値で上書きする)
2602
2633
  const committed = await showRouteContent(routerNode, matchResult, lastRoutes, () => {
2603
2634
  // routeName は最深マッチの name。fallback 時は fallback ルートの name(D8)。
2604
2635
  routerNode.commitNavigation({
@@ -2610,11 +2641,14 @@ async function applyRoute(routerNode, outlet, fullPath, lastPath, search = "") {
2610
2641
  search,
2611
2642
  path,
2612
2643
  }, true);
2613
- });
2644
+ }, outlet, superseded);
2614
2645
  if (!committed)
2615
2646
  return false;
2616
2647
  routerNode.releasePath();
2617
- outlet.lastRoutes = matchResult.routes;
2648
+ // 遷移を待つ間に追い越された(戻るの直後の進む): 変更は当たっているが、フォーカスとアナウンスは
2649
+ // 後のナビゲーションのもの
2650
+ if (superseded())
2651
+ return true;
2618
2652
  // オプトインの focus/announce は commit 直後・mutate() の外で適用する(D3)。
2619
2653
  // 初回描画(lastRoutes が空)では動かない — ページロードはブラウザの担当で、
2620
2654
  // view-transition の「初回は包まない」と同じ判定・同じ理由(§3-5)。