-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Blazor preserve state during prerendering #22732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
aspnetcore/mvc/views/tag-helpers/built-in/preserve-component-state.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
--- | ||
title: Preserve Component State Tag Helper in ASP.NET Core | ||
author: guardrex | ||
ms.author: riande | ||
description: Learn how to use the ASP.NET Core Preserve Component State Tag Helper to preserve state when prerendering components. | ||
monikerRange: '>= aspnetcore-6.0' | ||
ms.custom: mvc | ||
ms.date: 07/14/2021 | ||
no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR] | ||
uid: mvc/views/tag-helpers/builtin-th/preserve-component-state-tag-helper | ||
zone_pivot_groups: blazor-hosting-models | ||
--- | ||
# Preserve Component State Tag Helper in ASP.NET Core | ||
|
||
## Prerequisites | ||
|
||
Follow the guidance in the *Configuration* section for either: | ||
|
||
* [Blazor WebAssembly](xref:blazor/components/prerendering-and-integration?pivots=webassembly) | ||
* [Blazor Server](xref:blazor/components/prerendering-and-integration?pivots=server) | ||
|
||
## Preserve Component State Tag Helper | ||
|
||
To preserve state into prerendered components, use the Preserve Component State Tag Helper (). Add the `<preserve-component-state>` tag inside the closing `</body>` tag of the `_Host` page in an app that prerenders components: | ||
|
||
::: zone pivot="webassembly" | ||
|
||
`Pages/_Host.cshtml`: | ||
|
||
```cshtml | ||
<body> | ||
<component type="typeof(App)" render-mode="WebAssemblyPrerendered" /> | ||
|
||
... | ||
|
||
<persist-component-state /> | ||
</body> | ||
``` | ||
|
||
::: zone-end | ||
|
||
::: zone pivot="server" | ||
|
||
`Pages/_Host.cshtml`: | ||
|
||
```cshtml | ||
<body> | ||
<component type="typeof(App)" render-mode="ServerPrerendered" /> | ||
|
||
... | ||
|
||
<persist-component-state /> | ||
</body> | ||
``` | ||
|
||
::: zone-end | ||
|
||
Decide what state to persist using the `ComponentApplicationState` service. The `ComponentApplicationState.OnPersisting` event is fired just before the state is persisted into the prerendered page, which allows you to retrieve any persisted state when initializing a component. | ||
|
||
In the following example: | ||
|
||
* The `{TYPE}` placeholder represents the type of data to persist (for example, `WeatherForecast[]`). | ||
* The `{TOKEN}` placeholder is a state identifier string (for example, `fetchdata`). | ||
|
||
```razor | ||
@implements IDisposable | ||
@inject ComponentApplicationState ApplicationState | ||
|
||
... | ||
|
||
@code { | ||
protected override async Task OnInitializedAsync() | ||
{ | ||
ApplicationState.OnPersisting += PersistData; | ||
|
||
if (!ApplicationState | ||
.TryRedeemFromJson<{TYPE}>("{TOKEN}", out var data)) | ||
{ | ||
data = ...; | ||
} | ||
} | ||
|
||
private Task PersistData() | ||
{ | ||
ApplicationState.PersistAsJson("{TOKEN}", data); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
void IDisposable.Dispose() | ||
{ | ||
ApplicationState.OnPersisting -= PersistData; | ||
} | ||
} | ||
``` | ||
|
||
For more information and a complete example, see <xref:blazor/components/prerendering-and-integration#preserve-prerendered-state>. | ||
|
||
## Additional resources | ||
|
||
* <xref:Microsoft.AspNetCore.Mvc.TagHelpers.ComponentTagHelper> | ||
* <xref:mvc/views/tag-helpers/intro> | ||
* <xref:blazor/components/index> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.