Skip to content

Support pre-loading on hover #72080

Open
Open

Description

@quentinderoubaix

Which @angular/* package(s) are relevant/related to the feature request?

router

Description

When a complex Angular app with lazy-loaded routes has many possible routes that are connected, it is hard to define a PreloadingStrategy that does not pre-load too much while keeping the best User Experience / Performance.

Let's say if the courrent route is page /b12 and you can possibly navigate to /b1, /b2, /b3, ... plus /a1, /c1, /d1, /e1... for the best User Experience you might want to preload all those routes, but not routes like /a2, /a3,
This is a lot of routes to pre-load, lots of network requests, and makes the PreloadingStrategy implementation bloated.

The best compromise I have seen in fwks so far is SvelteKit's preload on hover where there is nothing to setup for users. In the previous example, it would mean zero pre-loading until the user hovers on of the connected links.

I am aware of the library ngx-hover-preload but it relies on either an Angular Module HoverPreloadModule or importing LinkDirective everywhere routerLink is used...

Would you consider including in Angular's default router apis a way to preload on hover?

Proposed solution

Propose a new PreloadingStrategy that supports preload on hover.
It would most likely involve a Service that is injected in the new PreloadOnHoverStrategy and optionally injected in RouterLink.

Alternatives considered

We can use ngx-hover-preload already, but not a fan of using modules in modern angular or repeating LinkDirective imports everywhere we are importing RouterLink.

We can also fork the RouterLink code and enhance it but that's very far from what we want, we highly value using Angular's default apis when we can.

Activity

  1. added this to the needsTriage milestone on Nov 18, 2026
  2. added this to the needsTriage milestone on Nov 18, 2026
  3. SkyZeroZx commented on Nov 18, 2026

    @SkyZeroZx
    Contributor

    I think this could be an opt in directive that injects RouterLink and triggers preloading on hover or focus. You’d only import it in the components that need it.
    Adding this directly to RouterLink could increase bundle size for apps that don’t use the feature, depending on how it’s implemented.
    Eg:

    import {DestroyRef, Directive, Service, inject} from '@angular/core';
    import {takeUntilDestroyed} from '@angular/core/rxjs-interop';
    import {
      PRIMARY_OUTLET,
      PreloadingStrategy,
      Route,
      Router,
      RouterLink,
      UrlTree,
    } from '@angular/router';
    import {EMPTY, Observable, catchError, defer} from 'rxjs';
    
    @Service()
    export class HoverPreloadStrategy implements PreloadingStrategy {
      private readonly router = inject(Router);
      private readonly destroyRef = inject(DestroyRef);
    
      private readonly loaders = new WeakMap<Route, () => Observable<unknown>>();
      private readonly requested = new WeakSet<Route>();
      private readonly started = new WeakSet<Route>();
    
      preload(route: Route, load: () => Observable<unknown>): Observable<unknown> {
        this.loaders.set(route, load);
    
        if (this.requested.has(route)) {
          this.start(route);
        }
    
        // Complete immediately instead of waiting for a hover.
        return EMPTY;
      }
    
      preloadUrl(url: UrlTree): void {
        const primary = url.root.children[PRIMARY_OUTLET];
    
        if (
          !primary ||
          primary.hasChildren() ||
          Object.keys(url.root.children).length !== 1
        ) {
          return;
        }
    
        const path = primary.segments.map(segment => segment.path).join('/');
    
        const route = this.router.config.find(candidate =>
          candidate.path === path &&
          !!candidate.path &&
          !candidate.path.includes(':') &&
          candidate.path !== '**' &&
          candidate.redirectTo === undefined &&
          candidate.matcher === undefined &&
          (candidate.outlet ?? PRIMARY_OUTLET) === PRIMARY_OUTLET
        );
    
        if (!route) return;
    
        this.requested.add(route);
        this.start(route);
      }
    
      private start(route: Route): void {
        const load = this.loaders.get(route);
    
        if (!load || this.started.has(route)) return;
    
        this.started.add(route);
    
        defer(load).pipe(
          catchError(() => {
            // Allow another interaction to retry a failed preload.
            this.started.delete(route);
            return EMPTY;
          }),
          takeUntilDestroyed(this.destroyRef),
        ).subscribe();
      }
    }
    
    @Directive({
      selector: 'a[routerLink]',
      host: {
        '(pointerenter)': 'preload()',
        '(focusin)': 'preload()',
      },
    })
    export class HoverPreloadDirective {
      private readonly link = inject(RouterLink, {self: true});
      private readonly strategy = inject(HoverPreloadStrategy);
    
      protected preload(): void {
        const url = this.link.urlTree;
    
        if (url) {
          this.strategy.preloadUrl(url);
        }
      }
    }
  4. atscott commented on Nov 18, 2026

    @atscott
    Contributor

    Right, I don’t think this can be something that’d be added to RouterLink due to bundle size impact. That said, I find the existing preloading strategy quite convoluted. A more straightforward method that preloads link’s target is something that’s planned as part of overall performance work

  5. added
    feature
    area: performance
    feature: in backlog
    on Nov 18, 2026
  6. modified the milestones: needsTriage, Backlog on Nov 18, 2026
  7. SkyZeroZx commented on Nov 18, 2026

    @SkyZeroZx
    Contributor

    In fact, maybe in userland we could use the Speculation Rules API, which I think is better since is native from platform

    See https://developer.mozilla.org/en-US/docs/Web/API/Speculation_Rules_API

    The only drawback is that it only works in Chromium-based browsers so far

  8. Flarette commented on Nov 19, 2026

    @Flarette
    Contributor

    You guys still prefetch onHover? That's soooo last month. My grandma just rewrote her app to pre-fetch on pre-hover: https://github.com/spaansba/ForesightJS
    Something like this would be interesting as a preloading strategy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: performancearea: routerfeaturefeature: in backlog

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions