@wcstack/router 4.6.2 → 4.8.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
@@ -791,6 +791,10 @@ declare class Head extends HTMLElement {
791
791
  connectedCallback(): void;
792
792
  disconnectedCallback(): void;
793
793
  get childElementArray(): Element[];
794
+ /**
795
+ * head へ入れる子のクローン。束縛を持つ子は、束ねたクローンを使い回す(boundClones)
796
+ */
797
+ private _cloneOf;
794
798
  /**
795
799
  * 要素の一意キーを生成(WeakMap でキャッシュ)
796
800
  */
package/dist/index.esm.js CHANGED
@@ -1733,7 +1733,12 @@ async function _parseNode(routerNode, node, routeParentNode, routesByPath) {
1733
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
1734
  }
1735
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.`);
1736
+ // スクリプトの無いハンドラは、読み込めなかったハンドラと同じく閉じる側に倒す: 守るはずのページを開けない。
1737
+ // バンドラが HTML のインラインスクリプトを取り去ると、この形になる。以前は「守られない」と知らせて開け、
1738
+ // `guard` 属性があれば知らせとは逆に、来ないハンドラを待って描かなかった(docs/seam-apps.md、prod-booking)
1739
+ route.enableGuard();
1740
+ route.notifyGuardHandlerLoadFailed();
1741
+ console.error(`[@wcstack/router] [wcs/guard-handler-ignored] The <${config.tagNames.guardHandler}> of route "${route.absolutePath}" has no <script type="module">, so its guard cannot run: every navigation into the route is redirected to "${route.getAttribute("guard") || "/"}".`);
1737
1742
  }
1738
1743
  else {
1739
1744
  if (routeParentNode === null) {
@@ -3325,7 +3330,11 @@ class Router extends HTMLElement {
3325
3330
  // InvalidStateError が投げられる(docs/seam-apps.md、router-admin)
3326
3331
  navEvent.defaultPrevented ||
3327
3332
  navEvent.hashChange ||
3328
- navEvent.downloadRequest !== null) {
3333
+ navEvent.downloadRequest !== null ||
3334
+ // 読み直し(location.reload() / history.go(0) / navigation.reload())は文書を読み直す。受けると同じ
3335
+ // 文書でルートを描き直すだけになり、更新を促す「再読み込み」も Service Worker の更新も効かなかった
3336
+ // (docs/seam-apps.md、prod-booking)。未保存の変更の確認は beforeunload の側(leaveConfirm)
3337
+ navEvent.navigationType === "reload") {
3329
3338
  return;
3330
3339
  }
3331
3340
  const url = new URL(navEvent.destination.url);
@@ -4183,6 +4192,14 @@ let initialHeadCaptured = false;
4183
4192
  * 要素の属性変更には追随しない(Head 内要素は初期化時に固定される前提)。
4184
4193
  */
4185
4194
  const keyCache = new WeakMap();
4195
+ /**
4196
+ * 束縛を持つ子ごとに、head へ入れる束ねたクローンを 1 つだけ持つ。
4197
+ * binder には束縛を解く口が無く(docs/binder-protocol-design.md §6-2)、state は束ねたノードを
4198
+ * エンジンの寿命まで持つ。遷移のたびに新しいクローンを束ねさせると、外したクローンが 1 つずつ
4199
+ * 残った(束ねた `<title>` で遷移ごとに 2 ノード — docs/seam-apps.md、shift-desk)。同じクローンを
4200
+ * 入れ直せば、bind は冪等で、外れている間も値は届いている。
4201
+ */
4202
+ const boundClones = new WeakMap();
4186
4203
  class Head extends HTMLElement {
4187
4204
  _initialized = false;
4188
4205
  _childElementArray = [];
@@ -4234,6 +4251,20 @@ class Head extends HTMLElement {
4234
4251
  }
4235
4252
  return this._childElementArray;
4236
4253
  }
4254
+ /**
4255
+ * head へ入れる子のクローン。束縛を持つ子は、束ねたクローンを使い回す(boundClones)
4256
+ */
4257
+ _cloneOf(child) {
4258
+ if (!hasBinding(child)) {
4259
+ return child.cloneNode(true);
4260
+ }
4261
+ let clone = boundClones.get(child);
4262
+ if (clone === undefined) {
4263
+ clone = child.cloneNode(true);
4264
+ boundClones.set(child, clone);
4265
+ }
4266
+ return clone;
4267
+ }
4237
4268
  /**
4238
4269
  * 要素の一意キーを生成(WeakMap でキャッシュ)
4239
4270
  */
@@ -4362,7 +4393,7 @@ class Head extends HTMLElement {
4362
4393
  const head = headStack[i];
4363
4394
  for (const child of head._childElementArray) {
4364
4395
  if (this._getKey(child) === key) {
4365
- targetElement = child.cloneNode(true);
4396
+ targetElement = this._cloneOf(child);
4366
4397
  break;
4367
4398
  }
4368
4399
  }
@@ -4377,6 +4408,16 @@ class Head extends HTMLElement {
4377
4408
  }
4378
4409
  // headを更新
4379
4410
  const current = headElementMap.get(key) ?? null;
4411
+ // 束ねたクローンが既に入っている(同じ Head の再適用)
4412
+ if (targetElement !== null && current === targetElement) {
4413
+ continue;
4414
+ }
4415
+ // 同じ要素は入れ直さない。入れ直すとスタイルシートを取り直し、<base> の差し替えは base-uri の
4416
+ // CSP を毎回検査させる(WebKit は遷移ごとに違反を報告した — docs/seam-apps.md、prod-booking)。
4417
+ // 束縛を持つ要素は、束ねたクローン(boundClones)が入っていれば上で残した。別の要素が入っていれば入れ直す
4418
+ if (targetElement && current?.isEqualNode(targetElement) && !hasBinding(targetElement)) {
4419
+ continue;
4420
+ }
4380
4421
  if (targetElement) {
4381
4422
  if (current) {
4382
4423
  current.replaceWith(targetElement);
@@ -4447,7 +4488,7 @@ function bootstrapRouter(config, registry) {
4447
4488
  registerComponents(registry);
4448
4489
  }
4449
4490
 
4450
- var version = "4.6.2";
4491
+ var version = "4.8.0";
4451
4492
  var pkg = {
4452
4493
  version: version};
4453
4494