Skip to content

Improve the PersistentComponentStateSerializer example#37184

Merged
guardrex merged 6 commits into
mainfrom
guardrex/blazor-persistentcomponentstateserializer-example
May 22, 2026
Merged

Improve the PersistentComponentStateSerializer example#37184
guardrex merged 6 commits into
mainfrom
guardrex/blazor-persistentcomponentstateserializer-example

Conversation

@guardrex
Copy link
Copy Markdown
Collaborator

@guardrex guardrex commented May 22, 2026

Fixes #37181

Thanks, @htmlsplash! 🚀

This PR improves the PersistentComponentStateSerializer example.

Tom, Wade ... Just need one review on this one, and we don't need to bother Javier. It's taken directly from his framework test at ...

https://github.com/dotnet/aspnetcore/blob/main/src/Components/test/testassets/TestContentPackage/CustomIntSerializer.cs

... with updates to make it for nullable integers (int?) and works great as a fully working, cut-'n-paste demo! 🎉

Note that Copilot suggested the addition of ...

using System;
using Microsoft.Extensions.Logging;

... which I did add. However, those are in the shared framework. In an app, these namespaces aren't required, but let's go ahead ... no harm ... and the dev can knock them out. If you think the section should remark on this, I can add a sentence or two stating that these namespaces are in the shared framework. IMO, it's probably not necessary.


Internal previews

📄 File 🔗 Preview link
aspnetcore/blazor/state-management/prerendered-state-persistence.md aspnetcore/blazor/state-management/prerendered-state-persistence

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the Blazor prerendered state persistence article to provide a more complete, copy/paste-ready example of implementing a custom PersistentComponentStateSerializer<T>.

Changes:

  • Replaces the prior generic custom-serializer description with a concrete int? custom serializer example (CustomIntSerializer) that demonstrates non-JSON serialization.
  • Updates the DI registration snippet to show registering the custom serializer for int?.
  • Adds an example logging output section intended to show how the serializer is invoked during prerendering/restore.
Comments suppressed due to low confidence (1)

aspnetcore/blazor/state-management/prerendered-state-persistence.md:377

  • Restore returns 0 when parsing fails, which silently changes data and is especially problematic for an int? serializer (it can't distinguish invalid data from a real 0). It should return null (or otherwise preserve the absence of a value) when the custom payload is null/empty or cannot be parsed.
        if (text.StartsWith("CUSTOM:", StringComparison.Ordinal) && 
            int.TryParse(text.AsSpan(7), out int value))
        {
            return value;
        }

        // Fallback to direct parsing if format is unexpected
        return int.TryParse(text, out int fallbackValue) ? fallbackValue : 0;
    }

Comment thread aspnetcore/blazor/state-management/prerendered-state-persistence.md
Comment thread aspnetcore/blazor/state-management/prerendered-state-persistence.md
Comment thread aspnetcore/blazor/state-management/prerendered-state-persistence.md Outdated
Comment thread aspnetcore/blazor/state-management/prerendered-state-persistence.md

This comment was marked as resolved.

This comment was marked as outdated.

This comment was marked as resolved.

@guardrex guardrex requested review from tdykstra and wadepickett May 22, 2026 16:08
Comment thread aspnetcore/blazor/state-management/prerendered-state-persistence.md Outdated
Copy link
Copy Markdown
Contributor

@wadepickett wadepickett left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved. Looks great. I don't see any issues in the PR additions.

I did make a suggestion which you could keep or not for the intro of the article.

…ce.md

Co-authored-by: Wade Pickett <wpickett@microsoft.com>
@guardrex guardrex removed the request for review from tdykstra May 22, 2026 17:30
@guardrex guardrex merged commit c194ff2 into main May 22, 2026
4 checks passed
@guardrex guardrex deleted the guardrex/blazor-persistentcomponentstateserializer-example branch May 22, 2026 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve example for PersistentComponentStateSerializer

3 participants