TransferStateService
TheTransferStateService allows you to transfer your Angular application’s state into the static site rendered by Scully. This enables Scully to use cached data instead of making requests to the original data source (typically an external API).
Overview
When Scully renders your application, it captures the state of your data and embeds it in the static HTML. When users visit the static site, this cached state is loaded instantly, avoiding unnecessary API calls and improving performance. Additionally, the service loads state for subsequent route changes, allowing the next route’s data to be fetched from the server without requiring a full page reload.Usage
The service is automatically provided when you importScullyLibModule:
Methods
useScullyTransferState()
The recommended method for automatically including your observable data sources in Scully’s TransferState.name- Unique identifier for this stateoriginalState- Your original observable data source
getState()
Returns an observable that fires once and completes after the page’s navigation has finished.name- The key name of the state to retrieve
setState()
Sets a value in the transfer state. This is typically used during Scully’s rendering process.name- The key name for the stateval- The value to store
stateHasKey()
Checks if the current state has a value for the given key name.name- The key name to check
true if the key exists (even if the value is undefined)
stateKeyHasValue()
Checks if the current state has a non-null value for the given key name.name- The key name to check
true if the key exists and has a non-null value
Example:
How It Works
During Scully Rendering
- Your Angular app runs in Puppeteer
- When you call
setState()or useuseScullyTransferState(), the data is captured - Scully embeds this data in the static HTML as a script tag
- The page is saved with the embedded state
On the Client
- When a user visits the static page, the embedded state is immediately available
getState()returns the cached data synchronously on initial load- No API calls are made for data that’s already in the state
- On route changes, new state is fetched from the next route’s data file or inline HTML
Configuration Options
The TransferState service behavior can be configured inscully.config.ts:
Best Practices
Use unique state keys
Use unique state keys
Always use unique keys for different data to avoid conflicts. Include identifiers like IDs in your keys:
Prefer useScullyTransferState()
Prefer useScullyTransferState()
Use
useScullyTransferState() instead of manually calling getState() and setState(). It handles both development and production scenarios automatically.Handle loading states
Handle loading states
Even with TransferState, handle loading states in your templates:
Don't store sensitive data
Don't store sensitive data
Remember that state is embedded in the static HTML and visible to all users. Never store sensitive information like API keys or personal data.
Advanced Example
Here’s a complete example showing TransferState with a list and detail view:Source Code
View the complete source code:Related
Scully Library
Learn about the Angular library overview
Idle Monitor
Understand when Scully renders your pages

