Skip to content

[Blazor] Support serializing render fragments from SSR #52768

@javiercn

Description

@javiercn

Currently we don't support serializing RenderFragments as root component parameters.

This has obvious drawbacks like our inability to render components with a body as a root component (That's why we need to have extra components like Routes.razor) on the template or why we can't have @RenderMode applied to things like Layout components.

To support these types of scenarios, we can support serializing the contents of the render fragment statically and re-applying those contents on the interactive side.

The way it will work is as follows:

  • Everytime we render a component with a RenderMode (either on the component or at the call site), we'll look at the list of parameters and will wrap every RenderFragment component within a function that will capture the outputs produced by the render fragment.
  • We will serialize that output as JSON (it's the RenderTree as JSON)
  • On the interactive side, we will read that tree and pass in a RenderFragment delegate as parameter that just outputs that tree.

For the most part, this will work for static content and will also work for even components, provided that the parameters in the components are serializable.

This will mean that we can for example, apply @rendermode to things like CascadingValue provided that the T used is serializable, and the components inside the body are serializable too.

To avoid situations where we close over server state, we can require the compiler to make the function a static local function when it is passed to a component with a rendermode or @rendermode is used as an attribute. You could still get into weird situations if you try to access static variables, but I think that's acceptable.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions