mirror of
https://github.com/angular/angular.git
synced 2026-09-14 13:54:52 +08:00
docs: add Event Replay section to the Hydration guide (#56770)
PR Close #56770
This commit is contained in:
committed by
Jessica Janiuk
parent
fec5b80aaf
commit
622ea44ea2
@@ -12,7 +12,15 @@ Without hydration enabled, server-side rendered Angular applications will destro
|
||||
|
||||
## How do you enable hydration in Angular
|
||||
|
||||
Before you can get started with hydration, you must have a server-side rendered (SSR) application. Follow the [Angular SSR Guide](guide/ssr) to enable server-side rendering first. Once you have SSR working with your application, you can enable hydration by visiting your main app component or module and importing `provideClientHydration` from `@angular/platform-browser`. You'll then add that provider to your app's bootstrapping providers list.
|
||||
Hydration can be enabled for server-side rendered (SSR) applications only. Follow the [Angular SSR Guide](guide/ssr) to enable server-side rendering first.
|
||||
|
||||
### Using Angular CLI
|
||||
|
||||
If you've used Angular CLI to enable SSR (either by enabling it during application creation or later via `ng add @angular/ssr`), the code that enables hydration should already be included into your application.
|
||||
|
||||
### Manual setup
|
||||
|
||||
If you have a custom setup and didn't use Angular CLI to enable SSR, you can enable hydration manually by visiting your main application component or module and importing `provideClientHydration` from `@angular/platform-browser`. You'll then add that provider to your app's bootstrapping providers list.
|
||||
|
||||
```typescript
|
||||
import {
|
||||
@@ -43,13 +51,31 @@ export class AppModule {}
|
||||
|
||||
IMPORTANT: Make sure that the `provideClientHydration()` call is also included into a set of providers that is used to bootstrap an application on the **server**. In applications with the default project structure (generated by the `ng new` command), adding a call to the root `AppModule` should be sufficient, since this module is imported by the server module. If you use a custom setup, add the `provideClientHydration()` call to the providers list in the server bootstrap configuration.
|
||||
|
||||
After you've followed these steps and have started up your server, load your application in the browser.
|
||||
### Verify that hydration is enabled
|
||||
|
||||
After you've configured hydration and have started up your server, load your application in the browser.
|
||||
|
||||
HELPFUL: You will likely need to fix instances of Direct DOM Manipulation before hydration will fully work either by switching to Angular constructs or by using `ngSkipHydration`. See [Constraints](#constraints), [Direct DOM Manipulation](#direct-dom-manipulation), and [How to skip hydration for particular components](#how-to-skip-hydration-for-particular-components) for more details.
|
||||
|
||||
While running an application in dev mode, you can confirm hydration is enabled by opening the Developer Tools in your browser and viewing the console. You should see a message that includes hydration-related stats, such as the number of components and nodes hydrated.
|
||||
While running an application in dev mode, you can confirm hydration is enabled by opening the Developer Tools in your browser and viewing the console. You should see a message that includes hydration-related stats, such as the number of components and nodes hydrated. Angular calculates the stats based on all components rendered on a page, including those that come from third-party libraries.
|
||||
|
||||
HELPFUL: Angular calculates the stats based on all components rendered on a page, including those that come from third-party libraries.
|
||||
You can also use [Angular DevTools browser extension](tools/devtools) to see hydration status of components on a page. Angular DevTools also allows to enable an overlay to indicate which parts of the page were hydrated. If there is a hydration mismatch error - DevTools would also highlight a component that caused the error.
|
||||
|
||||
## Capturing and replaying events
|
||||
|
||||
When an application is rendered on the server, it is visible in a browser as soon as produced HTML loads. Users may assume that they can interact with the page, but event listeners are not attached until hydration completes. Starting from v18, you can enable the Event Replay feature that allows to capture all events that happen before hydration and replay those events once hydration has completed. You can enable it using the `withEventReplay()` function, for example:
|
||||
|
||||
```typescript
|
||||
import {provideClientHydration, withEventReplay} from '@angular/platform-browser';
|
||||
|
||||
bootstrapApplication(App, {
|
||||
providers: [
|
||||
provideClientHydration(withEventReplay())
|
||||
]
|
||||
});
|
||||
```
|
||||
|
||||
IMPORTANT: the Event Replay feature is currently in [Developer Preview](/reference/releases#developer-preview).
|
||||
|
||||
## Constraints
|
||||
|
||||
|
||||
Reference in New Issue
Block a user