IdleMonitorService
TheIdleMonitorService hooks into Angular’s Zone.js to detect when your application becomes idle. When all HTTP requests complete and there are no pending asynchronous tasks, Scully knows the page is ready to be rendered.
Overview
When Scully renders your Angular application in Puppeteer, it needs to know when to capture the page content. TheIdleMonitorService monitors Zone.js macro tasks and dispatches events when Angular goes idle, signaling to Scully that rendering can begin.
This ensures that:
- All HTTP requests have completed
- All async operations have finished
- The DOM is fully updated
- The page is in its final rendered state
How It Works
- AngularInitialized Event: Dispatched when the service initializes
- Zone Monitoring: The service monitors Zone.js macro tasks, specifically XMLHttpRequest tasks
- AngularReady Event: Dispatched when the application becomes idle (all async tasks complete)
- AngularTimeout Event: Dispatched if the app doesn’t become idle within 30 seconds
Configuration
The service is automatically enabled when you importScullyLibModule. Configure it through the forRoot() options:
Configuration Options
When
true, idle monitoring runs in both development and production. When false, it only runs during Scully rendering.When
true, disables automatic idle detection and requires manual triggering of the ready event.Automatic Idle Detection
By default, the service automatically detects when your app is idle:Manual Idle Control
For content loaded outside of Zone.js (e.g., third-party scripts, Web Workers), you can manually control when Scully should render.Global Manual Idle
Disable automatic detection globally:Per-Route Manual Idle
Enable manual idle for specific routes inscully.config.ts:
Methods
fireManualMyAppReadyEvent()
Manually triggers theAngularReady event to signal that Scully should start rendering.
true when the event is dispatched
Example:
init()
Returns a promise that resolves when the application first becomes idle.setPupeteerTimeoutValue()
Sets the timeout value used when Zone.js is not available.milliseconds- The timeout duration in milliseconds (default: 5000)
Observables
idle$
An observable that emits the current idle state of the application.Events
The service dispatches the following custom DOM events:AngularInitialized
Dispatched when Angular and the IdleMonitorService initialize.AngularReady
Dispatched when the application becomes idle and is ready to be rendered.AngularTimeout
Dispatched if the application doesn’t become idle within 30 seconds.Fallback Behavior
If Zone.js is not available, the service falls back to a simple timeout mechanism:Troubleshooting
Scully renders before content loads
Scully renders before content loads
If Scully captures your page before content loads, your async operations might be running outside Zone.js. Solutions:
- Enable manual idle mode and call
fireManualMyAppReadyEvent() - Ensure HTTP requests use Angular’s
HttpClient - Run async operations inside
NgZone:
Timeout errors during rendering
Timeout errors during rendering
If you see
AngularTimeout events, your app is taking more than 30 seconds to become idle. Solutions:- Check for infinite HTTP polling or intervals
- Use manual idle mode for complex pages
- Defer non-critical operations until after rendering
Content missing on static pages
Content missing on static pages
If content is missing from rendered pages:
- Verify HTTP requests complete before idle state
- Check browser console for errors
- Enable
alwaysMonitorto test in development - Use
manualIdleCheckfor specific routes
Best Practices
-
Use HttpClient: Always use Angular’s
HttpClientfor HTTP requests so they’re tracked by Zone.js - Avoid Long Polling: Don’t use intervals or long polling on pages that Scully renders
-
Defer Analytics: Load analytics and tracking scripts after the page renders:
-
Test Rendering: Enable
alwaysMonitorduring development to test idle detection: -
Per-Route Configuration: Use route-specific
manualIdleCheckfor pages with special loading requirements
Source Code
View the complete source code:Related
Scully Library
Learn about the Angular library overview
Transfer State
Cache application state during rendering

