ProductPromotion
Logo

Angular.JS

made by https://0x3d.site

GitHub - mgechev/ngx-quicklink: Quicklink prefetching strategy for the Angular router
Quicklink prefetching strategy for the Angular router - mgechev/ngx-quicklink
Visit Site

GitHub - mgechev/ngx-quicklink: Quicklink prefetching strategy for the Angular router

GitHub - mgechev/ngx-quicklink: Quicklink prefetching strategy for the Angular router

ngx-quicklink

quicklink implementation for Angular. It provides router preloading strategy which automatically downloads the lazy-loaded modules associated with all the visible links on the screen.

How it works

Quicklink attempts to make navigations to subsequent pages load faster. It:

  • Detects routerLinks within the viewport (using Intersection Observer)
  • Waits until the browser is idle (using requestIdleCallback)
  • Checks if the user isn't on a slow connection (using navigator.connection.effectiveType) or has data-saver enabled (using navigator.connection.saveData)
  • Prefetches the lazy loaded modules using Angular's prefetching strategy)

Why

This project aims to be a drop-in solution for sites to prefetch links based on what is in the user's viewport. It also aims to be small (~2KB minified/gzipped).

Usage

First you need to install the project:

npm i ngx-quicklink --save

After that import the QuicklinkModule to the AppModule, and use the QuicklinkStrategy as preloadingStrategy in the router's configuration. For example:

// ...
import { QuicklinkModule, QuicklinkStrategy } from 'ngx-quicklink';

@NgModule({
  declarations: [...],
  imports: [
    // ...
    QuicklinkModule,
    RouterModule.forRoot(routes, { preloadingStrategy: QuicklinkStrategy }),
  ],
  bootstrap: [...]
})
export class AppModule {}

If you want to add a route in the ignore list so that ngx-quicklink will not preload it use the data property:

export const routes: Routes = [
  {
    path: 'contact',
    loadChildren: import(() => './contact/contact.module').then(m => m.ContactModule),
    data: {
      preload: false
    }
  }
];

Note that to make the module available in lazy-loaded modules as well you need to import it in a shared module and export it. Look at this commit to see how ngx-quicklink is integrated in the angular-realworld-example-app.

Debugging

Not getting routes preloaded? Most likely the problem comes from a missing import of the QuicklinkModule. The QuicklinkModule exports a LinkDirective which matches the [routerLink] selector. It'll hook into all your router links in the scope of the module and observe their visibility. If you've not imported the QuicklinkModule correctly, this directive will be missing and the quicklink preloading strategy will not work.

How to verify Angular has made my links "quicklinks"? Inspect a router link and check if it has ngx-ql attribute. If it does not, make sure you import QuicklinkModule in the module that defines the compilation context of the template where the router link is. Alternatively, if the ngx-ql attribute is there, but the prefetching does not work as expected, please open an issue.

Polyfills

ngx-quicklink:

  • Includes a very small fallback for requestIdleCallback
  • Optionally requires IntersectionObserver to be supported (see CanIUse). On older browsers ngx-quicklink preloads all links on the page. If you want to enable the IntersectionObserver behavior on older browsers you can use conditional polyfill loading:
<script src="https://polyfill.io/v2/polyfill.min.js?features=IntersectionObserver"></script>

Alternatively, see the Intersection Observer polyfill.

FAQ

What's the difference between quicklink and ngx-quicklink?

quicklink prefetches the resource pointed by the href attribute of an anchor. This doesn't work for Angular because the value of the href attribute is not a JavaScript bundle but a path defined inside of the routing configuration. ngx-quicklink introduces some additional functionality to make the same strategy work well with Angular.

Should I use Guess.js or ngx-quicklink?

The prefetching behavior of Guess.js would most likely be more accurate compared to ngx-quicklink, which will reduce the overfetching. Guess.js, however, may take a little more effort to setup. In case you don't want to integrate with the analytics of your website ngx-quicklink is the right choice for you.

Developing & releasing

For a demo:

git clone [email protected]:mgechev/ngx-quicklink
cd ngx-quicklink && npm i
ng build --project ngx-quicklink
ng serve

To release first update the package version and after that:

npm run release
cd dist/ngx-quicklink
npm publish

Contributors

mgechev dependabot[bot] wKoza rolaveric thekiba Flyrell
Niaro krzysztof-grzybek tarsinzer mehmet-erim Timebutt pshurygin
thomashuston Komock jlilly

License

MIT

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory