Overview
The Flash Prevention plugin:- Prevents flash of unstyled content (FOUC)
- Creates a duplicate app root element
- Shows static content until Angular loads
- Smoothly transitions to the live app
- Preserves scroll position during transition
- Automatically removes the duplicate after hydration
The Problem
When Angular hydrates a pre-rendered page:- Browser loads and displays static HTML
- Angular starts bootstrapping
- Brief moment where Angular hasn’t rendered yet
- Flash: Content disappears or appears unstyled
- Angular finishes loading and displays properly
The Solution
The Flash Prevention plugin solves this by:- Creating a mock
<app-root-scully>with the static content - Hiding the real
<app-root>until Angular loads - Showing the mock root while Angular bootstraps
- Swapping visibility when Angular fires the
AngularReadyevent - Removing the mock element after transition
Installation
The Flash Prevention plugin is built into Scully and requires no separate installation.Configuration
Import and configure the plugin in yourscully.config.ts:
Configuration Options
Customize the plugin behavior with these options:The selector for your Angular app root element. Change this if you’ve customized your app’s root selector.
The CSS class added to
<body> when Angular finishes loading.The CSS display value for the app root elements.
HTML attributes to remove from the real app root when copying to the mock. By default, Angular-specific attributes like
_nghost and ng-version are removed.HTML attributes to remove from the mock app root element.
Usage Examples
Basic Setup
Minimal configuration with defaults:Custom App Root Selector
If your Angular app uses a custom selector:Custom Loaded Class
Use a custom class name for styling:Block Display
Force block display for app roots:Custom Attribute Filtering
Remove additional attributes from the app root:How It Works
HTML Transformation
The plugin transforms your HTML from:CSS Injection
The plugin adds CSS to control visibility:JavaScript Injection
A script handles the transition:- Scroll Preservation: Captures scroll position on scroll events
- Event Listening: Waits for the
AngularReadyevent - Class Addition: Adds the
loadedclass to<body> - Scroll Restoration: Restores the user’s scroll position
- Cleanup: Removes the mock element and event listeners
- Event Dispatch: Fires
FlashPreventionSwitchDoneevent
Angular Integration
Fire theAngularReady event in your Angular app:
Events
The plugin works with these custom events:AngularReady
Dispatch from Angular when your app is fully loaded:FlashPreventionSwitchDone
Listen for this event to know when the transition completes:When to Use
Use the Flash Prevention plugin when:- You notice a flash when Angular hydrates
- Content briefly disappears during page load
- You want seamless transitions between static and dynamic content
- User experience during hydration matters
- You’re using Scully for pre-rendering
Best Practices
Dispatch AngularReady correctly
Make sure to dispatch the
AngularReady event after your Angular app is stable, not just after bootstrap.Test on slow connections
The flash is most noticeable on slower connections. Test with network throttling.
Styling with Loaded Class
Use the loaded class for conditional styling:Troubleshooting
Still seeing a flash
Still seeing a flash
- Verify you’re dispatching the
AngularReadyevent - Check that the plugin is in
defaultPostRenderers - Ensure Angular app root selector matches configuration
Content appears twice
Content appears twice
- Check that
AngularReadyevent is being fired - Verify the event is dispatched after app is stable
- Look for JavaScript errors preventing the transition
Scroll position not maintained
Scroll position not maintained
- Ensure scroll events are not being prevented
- Check browser console for JavaScript errors
- Verify the scroll script is running correctly
Custom attributes appearing in mock
Custom attributes appearing in mock
- Add them to
appRootAttributesBlacklist - Or add to
mockAttributesBlacklistas appropriate
Performance Impact
The Flash Prevention plugin has minimal impact:- HTML size: Increases by ~2-3KB (compressed)
- Render time: No measurable impact
- User experience: Significantly improved
The small size increase is worth it for the dramatically improved user experience.
Related
Critical CSS Plugin
Inline critical CSS for faster rendering
Post Processors
Learn about other post-processing plugins

