Angular library for the Speculation Rules API - Enable prefetching and prerendering for faster page navigations
npm install ngx-speculation-rulesAngular library for the Speculation Rules API - Enable prefetching and prerendering for faster page navigations.
Angular SSR and Universal Friendly. Zoneless compatible.
See a live demo
Latest version available for each version of Angular
| ngx-speculation-rules | Angular |
| --------------------- | ----------- |
| 1.0.0 | 19.x - 20.x |
- 🚀 Prefetch - Load resources before they're needed
- âš¡ Prerender - Render pages in the background for instant navigation
- 🔧 Configurable - Control eagerness levels and targeting rules
- 📱 SSR Compatible - Works with Angular Universal/SSR
- 🎯 Zoneless Ready - Compatible with Angular's zoneless mode
``bash`
npm install ngx-speculation-rules
`typescript
// app.config.ts
import { ApplicationConfig } from '@angular/core';
import { provideSpeculationRules } from 'ngx-speculation-rules';
export const appConfig: ApplicationConfig = {
providers: [
provideSpeculationRules({
autoInsert: true,
defaultRules: {
prefetch: [{
source: 'document',
eagerness: 'moderate',
where: {
selector_matches: 'a[href^="/"]'
}
}]
}
})
]
};
`
`typescript`
provideSpeculationRules({
autoInsert: true,
defaultRules: {
prefetch: [{
source: 'document',
eagerness: 'moderate',
where: {
selector_matches: 'a[href^="/"]'
}
}]
}
})
`typescript`
provideSpeculationRules({
autoInsert: true,
defaultRules: {
prerender: [{
source: 'list',
urls: ['/about', '/contact', '/products'],
eagerness: 'moderate'
}]
}
})
`typescript
import { Component, inject } from '@angular/core';
import { SpeculationRulesService } from 'ngx-speculation-rules';
@Component({...})
export class MyComponent {
private speculationService = inject(SpeculationRulesService);
addPrefetchRule() {
this.speculationService.insertRules({
prefetch: [{
source: 'document',
eagerness: 'eager',
where: {
selector_matches: '.prefetch-link'
}
}]
}, 'custom-prefetch');
}
removeRule() {
this.speculationService.removeRules('custom-prefetch');
}
clearAllRules() {
this.speculationService.clearAllRules();
}
}
`
| Option | Type | Description |
| ------------ | ----------------- | ---------------------------------------------- |
| autoInsert | boolean | Automatically insert default rules on init |SpeculationRules
| defaultRules | | Default speculation rules to apply |
| Method | Return | Description |
| --------------------------- | --------------- | ---------------------------------------- |
| insertRules(rules, id?) | string \| null| Insert rules, returns script ID |removeRules(id)
| | boolean | Remove rules by ID |clearAllRules()
| | void | Remove all speculation rules |
| Property | Type | Description |
| -------------- | ------------------- | ---------------------------------------- |
| isSupported | SpeculationSupport| Browser support information |isPrerendering
| | boolean | Check if page is currently prerendering |
| Level | Description |
| ------------ | -------------------------------------------- |
| immediate | Speculate as soon as possible |eager
| | Speculate on any small positive signal |moderate
| | Speculate on hover for 200ms |conservative`| Speculate only on pointer/touch down |
|
The Speculation Rules API is supported in Chromium-based browsers (Chrome 109+, Edge 109+). The library gracefully handles unsupported browsers.
MIT