@real-router/angular 0.14.2 → 0.16.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/README.md CHANGED
@@ -491,7 +491,7 @@ Structural directive used inside `<route-view>`. Marks an `ng-template` as the e
491
491
  </route-view>
492
492
  ```
493
493
 
494
- **Template priority** inside `<route-view>`: `routeMatch` (segment prefix) → `routeSelf` (exact-match for `routeNode`) → `routeNotFound` (`UNKNOWN_ROUTE` only). First-wins for `routeMatch` and `routeSelf`, last-wins for `routeNotFound`.
494
+ **Template priority** inside `<route-view>`: `routeMatch` (segment prefix) → `routeSelf` (exact-match for `routeNode`) → `routeNotFound` (`UNKNOWN_ROUTE` only). First-wins for all three — `routeMatch`, `routeSelf`, and `routeNotFound` (#1439): the first matching template in declaration order wins, later duplicates are ignored.
495
495
 
496
496
  ### `routeNotFound`
497
497
 
package/dist/README.md CHANGED
@@ -491,7 +491,7 @@ Structural directive used inside `<route-view>`. Marks an `ng-template` as the e
491
491
  </route-view>
492
492
  ```
493
493
 
494
- **Template priority** inside `<route-view>`: `routeMatch` (segment prefix) → `routeSelf` (exact-match for `routeNode`) → `routeNotFound` (`UNKNOWN_ROUTE` only). First-wins for `routeMatch` and `routeSelf`, last-wins for `routeNotFound`.
494
+ **Template priority** inside `<route-view>`: `routeMatch` (segment prefix) → `routeSelf` (exact-match for `routeNode`) → `routeNotFound` (`UNKNOWN_ROUTE` only). First-wins for all three — `routeMatch`, `routeSelf`, and `routeNotFound` (#1439): the first matching template in declaration order wins, later duplicates are ignored.
495
495
 
496
496
  ### `routeNotFound`
497
497
 
@@ -1,7 +1,7 @@
1
1
  import * as i0 from '@angular/core';
2
- import { inject, DestroyRef, ApplicationRef, signal, InjectionToken, provideEnvironmentInitializer, makeEnvironmentProviders, makeStateKey, REQUEST, provideAppInitializer, TransferState, assertInInjectionContext, ChangeDetectorRef, effect, input, TemplateRef, Directive, contentChildren, computed, Component, output, ElementRef } from '@angular/core';
2
+ import { inject, DestroyRef, ApplicationRef, signal, InjectionToken, provideEnvironmentInitializer, makeEnvironmentProviders, makeStateKey, REQUEST, provideAppInitializer, TransferState, assertInInjectionContext, ChangeDetectorRef, effect, untracked, input, TemplateRef, Directive, contentChildren, computed, Component, output, ElementRef } from '@angular/core';
3
3
  import { getNavigator, UNKNOWN_ROUTE } from '@real-router/core';
4
- import { getTransitionSource, createRouteSource, primeErrorSource, createRouteNodeSource, createActiveSource, createDismissableError } from '@real-router/sources';
4
+ import { getTransitionSource, createRouteSource, primeErrorSource, createRouteNodeSource, createActiveSource, guardLeaveListener, createRouteEnterGate, createDismissableError } from '@real-router/sources';
5
5
  import { cloneRouter, getPluginApi } from '@real-router/core/api';
6
6
  import { hydrateRouter, serializeRouterState } from '@real-router/core/utils';
7
7
  import { getRouteUtils, startsWithSegment } from '@real-router/route-utils';
@@ -1964,8 +1964,10 @@ function injectIsActiveRoute(routeName, params, options) {
1964
1964
  * or `runInInjectionContext`).
1965
1965
  *
1966
1966
  * If the handler returns a Promise, the router blocks on it. If the
1967
- * Promise resolves, navigation proceeds. If it rejects, the router emits
1968
- * `TRANSITION_CANCELLED`.
1967
+ * Promise resolves, navigation proceeds. If it **rejects**, the router
1968
+ * rejects `navigate()` with the handler's **original error** and emits
1969
+ * `TRANSITION_ERROR` — it is NOT re-coded to `TRANSITION_CANCELLED` (that
1970
+ * arises only when the navigation's `signal` aborts).
1969
1971
  *
1970
1972
  * **Handler reactivity (Angular):** `inject*` functions run **once**
1971
1973
  * during component construction; `handler` is captured in closure at the
@@ -2012,15 +2014,10 @@ function injectRouteExit(handler, options) {
2012
2014
  const router = injectRouter();
2013
2015
  const destroyRef = inject(DestroyRef);
2014
2016
  const skipSameRoute = options?.skipSameRoute ?? true;
2015
- const off = router.subscribeLeave(({ route, nextRoute, signal }) => {
2016
- if (skipSameRoute && route.name === nextRoute.name) {
2017
- return;
2018
- }
2019
- if (signal.aborted) {
2020
- return;
2021
- }
2022
- return handler({ route, nextRoute, signal });
2023
- });
2017
+ // The same-route + reentrant-abort guards and the Promise passthrough live in
2018
+ // the shared listener (@real-router/sources, #1435); the handler is captured
2019
+ // at injection time (Angular `inject*` runs once at construction).
2020
+ const off = router.subscribeLeave(guardLeaveListener(handler, { skipSameRoute }));
2024
2021
  destroyRef.onDestroy(off);
2025
2022
  }
2026
2023
 
@@ -2085,29 +2082,33 @@ function injectRouteEnter(handler, options) {
2085
2082
  assertInInjectionContext(injectRouteEnter);
2086
2083
  const { routeState } = injectRoute();
2087
2084
  const skipSameRoute = options?.skipSameRoute ?? true;
2085
+ // The canonical enter-guard set lives in the shared gate (@real-router/
2086
+ // sources, #1435). Angular `inject*` runs once at construction, so a plain
2087
+ // const holds the gate. The gate owns skip-initial / same-route / the
2088
+ // `!previousRoute` guard (the sole defense of the non-nullable
2089
+ // `RouteEnterContext.previousRoute` contract) plus a StrictMode dedupe arm —
2090
+ // a defensive no-op in Angular (the effect below only re-runs on a fresh
2091
+ // `route` reference; see the `untracked` note), kept for parity with React and
2092
+ // tested once in sources, so no per-adapter v8-ignore is needed.
2093
+ const gate = createRouteEnterGate();
2088
2094
  effect(() => {
2089
2095
  const { route, previousRoute } = routeState();
2090
- // Early-exit guards, top-down:
2091
- //
2092
- // - **Skip-initial**: `state.transition.from` is undefined only
2093
- // for the very first state committed by `router.start()`.
2094
- // - **Skip-same-route**: query-only navigations have
2095
- // `transition.from === route.name`. Opt-out via
2096
- // `skipSameRoute: false`.
2097
- if (!route.transition.from) {
2098
- return;
2099
- }
2100
- if (skipSameRoute && route.transition.from === route.name) {
2101
- return;
2102
- }
2103
- // `previousRoute` is guaranteed populated whenever `route.transition.from`
2104
- // is set — core writes them together. The dead-code throw-guard that used
2105
- // to live here (review §8a LOW) is removed; the narrowing below is the
2106
- // type-safe equivalent and avoids the no-non-null-assertion lint.
2107
- if (!previousRoute) {
2108
- return;
2096
+ const context = gate(route, previousRoute, skipSameRoute);
2097
+ if (context) {
2098
+ // `untracked`: scope this effect's dependencies to `routeState()` alone.
2099
+ // Angular's `effect()` tracks signals read inside a synchronously-called
2100
+ // function, so WITHOUT this a handler-read signal changing (no navigation)
2101
+ // would re-run the effect and — pre-#1435 — re-fire the enter handler (a
2102
+ // spurious re-fire vs the "fire once per nav-driven mount" contract). With
2103
+ // the dispatch untracked the effect re-runs only on real navigations (a
2104
+ // fresh `route` reference each time), so the handler fires once per mount —
2105
+ // Angular parity with the other five adapters (empirically confirmed
2106
+ // 2026-07-17). A small beneficial BEHAVIOR change, not behavior-neutral
2107
+ // (hence the `minor` bump).
2108
+ untracked(() => {
2109
+ handler(context);
2110
+ });
2109
2111
  }
2110
- handler({ route, previousRoute });
2111
2112
  });
2112
2113
  }
2113
2114
 
@@ -2189,20 +2190,26 @@ class RouteView {
2189
2190
  }, /* @ts-ignore */
2190
2191
  ...(ngDevMode ? [{ debugName: "matchedTemplate" }] : /* istanbul ignore next */ []));
2191
2192
  // Fallback chain — only consulted when `matchedTemplate()` returned `null`.
2192
- // Template priority: Self → NotFound. Selection rules differ on purpose:
2193
- // - **Self uses first-wins** (`.at(0)`) for parity with React / Preact /
2194
- // Solid / Vue, where the first matching `<Self>` token in declaration
2195
- // order wins.
2196
- // - **NotFound uses last-wins** (`.at(-1)`) intentionally — the fallback
2197
- // should be the most-recently-declared template so that consumers can
2198
- // override an inherited `<ng-template routeNotFound>` simply by
2199
- // re-declaring it lower in the projected content.
2193
+ // Template priority: Self → NotFound. Both use **first-wins** (`.at(0)`) for
2194
+ // parity with React / Preact / Solid / Vue (#1439): the first matching
2195
+ // `<ng-template routeSelf>` / `<ng-template routeNotFound>` token in
2196
+ // declaration order wins; later duplicates are ignored. `contentChildren`
2197
+ // resolves matched directives in DOM/source order, so `.at(0)` is the
2198
+ // first-declared marker.
2200
2199
  fallbackTemplate = computed(() => {
2201
2200
  const route = this.routeState().route;
2202
2201
  if (!route) {
2203
2202
  return null;
2204
2203
  }
2205
2204
  const routeName = route.name;
2205
+ // The Self/NotFound fallback resolution reads contentChildren(RouteSelf /
2206
+ // RouteNotFound), which Angular 22 JIT / TestBed leaves empty (signal-input
2207
+ // structural directives aren't registered without AOT), so these guarded
2208
+ // `.at(0)` returns are unreachable in the unit suite — they fire only under
2209
+ // AOT (real apps / e2e). Excluded from coverage until AOT tests land (#1512);
2210
+ // see "Coverage Ceiling" in CLAUDE.md. The #1439 first-wins order is verified
2211
+ // structurally (`.at(0)` = first-declared, parity with `selfs().at(0)`).
2212
+ /* v8 ignore start */
2206
2213
  if (routeName === this.nodeName()) {
2207
2214
  const first = this.selfs().at(0);
2208
2215
  if (first) {
@@ -2210,11 +2217,12 @@ class RouteView {
2210
2217
  }
2211
2218
  }
2212
2219
  if (routeName === UNKNOWN_ROUTE) {
2213
- const last = this.notFounds().at(-1);
2214
- if (last) {
2215
- return last.templateRef;
2220
+ const first = this.notFounds().at(0);
2221
+ if (first) {
2222
+ return first.templateRef;
2216
2223
  }
2217
2224
  }
2225
+ /* v8 ignore stop */
2218
2226
  return null;
2219
2227
  }, /* @ts-ignore */
2220
2228
  ...(ngDevMode ? [{ debugName: "fallbackTemplate" }] : /* istanbul ignore next */ []));