Skip to content

Commit afab1f6

Browse files
authored
Static SSR in interactive apps improvements (#34631)
1 parent 27c00c8 commit afab1f6

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

aspnetcore/blazor/components/render-modes.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ For more information, see <xref:blazor/tooling>.
177177

178178
Properties and fields can assign a render mode.
179179

180-
The second approach described in this section, setting the render mode by component instance, is especially useful when your app specification calls for one or more components to adopt static SSR in a globally-interactive app. This scenario is covered in the [Static SSR pages in a globally-interactive app](#static-ssr-pages-in-a-globally-interactive-app) section later in this article.
180+
The second approach described in this section, setting the render mode by component instance, is especially useful when your app specification calls for one or more components to adopt static SSR in an otherwise interactive app. This scenario is covered in the [Static SSR pages in an interactive app](#static-ssr-pages-in-an-interactive-app) section later in this article.
181181

182182
### Set the render mode by component definition
183183

@@ -207,7 +207,7 @@ The following example applies interactive server-side rendering (interactive SSR
207207
}
208208
```
209209

210-
Additional information on render mode propagation is provided in the [Render mode propagation](#render-mode-propagation) section later in this article. The [Static SSR pages in a globally-interactive app](#static-ssr-pages-in-a-globally-interactive-app) section shows how to use the preceding approach to adopt static SSR in a globally-interactive app.
210+
Additional information on render mode propagation is provided in the [Render mode propagation](#render-mode-propagation) section later in this article. The [Static SSR pages in an interactive app](#static-ssr-pages-in-an-interactive-app) section shows how to use the preceding approach to adopt static SSR in an otherwise interactive app.
211211

212212
:::moniker range=">= aspnetcore-9.0"
213213

@@ -221,10 +221,11 @@ The <xref:Microsoft.AspNetCore.Components.ComponentBase.RendererInfo?displayProp
221221
* `WebAssembly`: On the client (CSR) and capable of interactivity after prerendering.
222222
* `WebView`: On the native device and capable of interactivity after prerendering.
223223
* <xref:Microsoft.AspNetCore.Components.RendererInfo.IsInteractive?displayProperty=nameWithType> indicates if the component supports interactivity at the time of rendering. The value is `true` when rendering interactively or `false` when prerendering or for static SSR (<xref:Microsoft.AspNetCore.Components.RendererInfo.Name?displayProperty=nameWithType> of `Static`).
224-
* <xref:Microsoft.AspNetCore.Components.ComponentBase.AssignedRenderMode?displayProperty=nameWithType> exposes the component's assigned render mode:
225-
* `InteractiveServer` for Interactive Server.
226-
* `InteractiveAuto` for Interactive Auto.
227-
* `InteractiveWebAssembly` for Interactive WebAssembly.
224+
* <xref:Microsoft.AspNetCore.Components.ComponentBase.AssignedRenderMode> exposes the component's assigned render mode:
225+
* `InteractiveServerRenderMode` for Interactive Server.
226+
* `InteractiveAutoRenderMode` for Interactive Auto.
227+
* `InteractiveWebAssemblyRenderMode` for Interactive WebAssembly.
228+
* `null` if a render mode isn't assigned.
228229

229230
Components use these properties to render content depending on their location or interactivity status. The following examples demonstrate typical use cases.
230231

@@ -617,11 +618,11 @@ The following component results in a runtime error when the component is rendere
617618

618619
> :::no-loc text="Cannot create a component of type 'BlazorSample.Components.SharedMessage' because its render mode 'Microsoft.AspNetCore.Components.Web.InteractiveWebAssemblyRenderMode' is not supported by Interactive Server rendering.":::
619620
620-
## Static SSR pages in a globally-interactive app
621+
## Static SSR pages in an interactive app
621622

622623
There are cases where the app's specification calls for components to adopt static server-side rendering (static SSR) and only run on the server, while the rest of the app uses an interactive render mode.
623624

624-
This approach is only useful when the app has specific pages that can't work with interactive Server or WebAssembly rendering. For example, adopt this approach for pages that depend on reading/writing HTTP cookies and can only work in a request/response cycle instead of interactive rendering. For pages that work with interactive rendering, you shouldn't force them to use static SSR rendering, as it's less efficient and less responsive for the end user.
625+
This approach is only useful when the app has specific pages that can't work with interactive Server or WebAssembly rendering. For example, adopt this approach for pages that depend on reading/writing HTTP cookies and can only work in a request/response cycle instead of interactive rendering. For pages that work with interactive rendering, you shouldn't force them to use static SSR, as it's less efficient and less responsive for the end user.
625626

626627
:::moniker range=">= aspnetcore-9.0"
627628

@@ -680,18 +681,22 @@ The following examples use the <xref:Microsoft.AspNetCore.Http.HttpContext> casc
680681

681682
### Area (folder) of static SSR components
682683

683-
The approach described in this subsection is used by the Blazor Web App project template with individual authentication and global interactivity.
684+
*For an example of the approach in this section, see the [`BlazorWebAppAreaOfStaticSsrComponents` sample app](https://github.com/dotnet/blazor-samples/tree/main/9.0/BlazorWebAppAreaOfStaticSsrComponents). The technique described in this section is most appropriate for 8.0 Blazor Web Apps, but the sample is implemented in 9.0 using Blazor features that simplify demonstrating how the approach works.*
684685

685-
An area (folder) of the app contains the components that must adopt static SSR and only run on the server. The components in the folder share the same route path prefix. For example, the Identity Razor components of the Blazor Web App project template are in the `Components/Account/Pages` folder and share the root path prefix `/Account`.
686+
The approach described in this subsection is used by the Blazor Web App project template with global interactivity.
686687

687-
The folder also contains an `_Imports.razor` file, which applies a custom account layout to the components in the folder:
688+
An area (folder) of the app contains the components that must adopt static SSR and only run on the server. The components in the folder share the same route path prefix. For example, the Identity Razor components of the Blazor Web App project template are in the `Components/Account/Pages` folder and share the root path prefix `/account`.
689+
690+
The app also contains an `_Imports.razor` file automatically applied to static SSR components in the `Components` folder, which applies a custom layout.
691+
692+
`Components/Account/_Imports.razor`:
688693

689694
```razor
690695
@using BlazorSample.Components.Account.Shared
691696
@layout AccountLayout
692697
```
693698

694-
The `Shared` folder maintains the `AccountLayout` layout component. The component makes use of <xref:Microsoft.AspNetCore.Http.HttpContext> to determine if the component has adopted static SSR. Identity components must render on the server with static SSR because they set Identity cookies. If the value of <xref:Microsoft.AspNetCore.Http.HttpContext> is `null`, the component is rendering interactively, and a full-page reload is performed by calling <xref:Microsoft.AspNetCore.Components.NavigationManager.Refresh%2A?displayProperty=nameWithType> with `forceLoad` set to `true`. This forces a full rerender of the page using static SSR.
699+
The `Shared` folder maintains the `AccountLayout` layout component. The component makes use of <xref:Microsoft.AspNetCore.Http.HttpContext> to determine if the component has adopted static SSR. For example, Identity components must render on the server with static SSR because they set Identity cookies. If the value of <xref:Microsoft.AspNetCore.Http.HttpContext> is `null`, the component is rendering interactively, and a full-page reload is performed by calling <xref:Microsoft.AspNetCore.Components.NavigationManager.Refresh%2A?displayProperty=nameWithType> with `forceLoad` set to `true`. This forces a full rerender of the page using static SSR.
695700

696701
`Components/Account/Shared/AccountLayout.razor`:
697702

@@ -724,7 +729,7 @@ else
724729
```
725730

726731
> [!NOTE]
727-
> In the Blazor Web App project template, there's a second layout file (`ManageLayout.razor` in the `Components/Account/Shared` folder) for Identity components in the `Components/Account/Pages/Manage` folder. The `Manage` folder has its own `_Imports.razor` file to apply to the `ManageLayout` to components in the folder. In your own apps, using nested `_Imports.razor` files is a useful approach for applying custom layouts to groups of pages.
732+
> In the Blazor Web App project template for authentication scenarios, there's a second layout file (`ManageLayout.razor` in the `Components/Account/Shared` folder) for Identity components in the `Components/Account/Pages/Manage` folder. The `Manage` folder has its own `_Imports.razor` file to apply to the `ManageLayout` to components in the folder. In your own apps, using nested `_Imports.razor` files is a useful approach for applying custom layouts to groups of pages.
728733
729734
In the `App` component, any request for a component in the `Account` folder applies a `null` render mode, which enforces static SSR. Other component requests receive a global application of the interactive SSR render mode (`InteractiveServer`).
730735

@@ -745,7 +750,7 @@ In the `App` component, any request for a component in the `Account` folder appl
745750
private HttpContext HttpContext { get; set; } = default!;
746751
747752
private IComponentRenderMode? RenderModeForPage =>
748-
HttpContext.Request.Path.StartsWithSegments("/Account")
753+
HttpContext.Request.Path.StartsWithSegments("/account")
749754
? null
750755
: {INTERACTIVE RENDER MODE};
751756
}
@@ -757,6 +762,8 @@ The components that must adopt static SSR in the `Account` folder aren't require
757762

758763
### Static SSR components spread out across the app
759764

765+
*For an example of the approach in this section, see the [`BlazorWebAppSpreadOutStaticSsrComponents` sample app](https://github.com/dotnet/blazor-samples/tree/main/9.0/BlazorWebAppSpreadOutStaticSsrComponents). The technique described in this section is most appropriate for 8.0 Blazor Web Apps, but the sample is implemented in 9.0 using Blazor features that simplify demonstrating how the approach works.*
766+
760767
In the [preceding subsection](#area-folder-of-static-ssr-components), the app controls the render mode of the components by setting the render mode globally in the `App` component. Alternatively, the `App` component can also adopt ***per-component*** render modes for setting the render mode, which permits components spread around the app to enforce adoption of static SSR. This subsection describes the approach.
761768

762769
The app has a custom layout that can be applied to components around the app. Usually, a shared component for the app is placed in the `Components/Layout` folder. The component makes use of <xref:Microsoft.AspNetCore.Http.HttpContext> to determine if the component has adopted static SSR. If the value of <xref:Microsoft.AspNetCore.Http.HttpContext> is `null`, the component is rendering interactively, and a full-page reload is performed by calling <xref:Microsoft.AspNetCore.Components.NavigationManager.Refresh%2A?displayProperty=nameWithType> with `forceLoad` set to `true`. This triggers a request to the server for the component.

aspnetcore/blazor/security/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ To avoid showing unauthorized content, for example content in an [`AuthorizeView
507507
<HeadOutlet @rendermode="new InteractiveServerRenderMode(prerender: false)" />
508508
```
509509

510-
You can also selectively control the render mode applied to the `Routes` component instance. For example, see <xref:blazor/components/render-modes#static-ssr-pages-in-a-globally-interactive-app>.
510+
You can also selectively control the render mode applied to the `Routes` component instance. For example, see <xref:blazor/components/render-modes#static-ssr-pages-in-an-interactive-app>.
511511

512512
:::moniker-end
513513

0 commit comments

Comments
 (0)