You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/blazor/components/render-modes.md
+21-14Lines changed: 21 additions & 14 deletions
Original file line number
Diff line number
Diff line change
@@ -177,7 +177,7 @@ For more information, see <xref:blazor/tooling>.
177
177
178
178
Properties and fields can assign a render mode.
179
179
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.
181
181
182
182
### Set the render mode by component definition
183
183
@@ -207,7 +207,7 @@ The following example applies interactive server-side rendering (interactive SSR
207
207
}
208
208
```
209
209
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.
211
211
212
212
:::moniker range=">= aspnetcore-9.0"
213
213
@@ -221,10 +221,11 @@ The <xref:Microsoft.AspNetCore.Components.ComponentBase.RendererInfo?displayProp
221
221
*`WebAssembly`: On the client (CSR) and capable of interactivity after prerendering.
222
222
*`WebView`: On the native device and capable of interactivity after prerendering.
223
223
*<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.
228
229
229
230
Components use these properties to render content depending on their location or interactivity status. The following examples demonstrate typical use cases.
230
231
@@ -617,11 +618,11 @@ The following component results in a runtime error when the component is rendere
617
618
618
619
> :::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.":::
619
620
620
-
## Static SSR pages in a globally-interactive app
621
+
## Static SSR pages in an interactive app
621
622
622
623
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.
623
624
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.
625
626
626
627
:::moniker range=">= aspnetcore-9.0"
627
628
@@ -680,18 +681,22 @@ The following examples use the <xref:Microsoft.AspNetCore.Http.HttpContext> casc
680
681
681
682
### Area (folder) of static SSR components
682
683
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.*
684
685
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.
686
687
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`:
688
693
689
694
```razor
690
695
@using BlazorSample.Components.Account.Shared
691
696
@layout AccountLayout
692
697
```
693
698
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.
695
700
696
701
`Components/Account/Shared/AccountLayout.razor`:
697
702
@@ -724,7 +729,7 @@ else
724
729
```
725
730
726
731
> [!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.
728
733
729
734
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`).
730
735
@@ -745,7 +750,7 @@ In the `App` component, any request for a component in the `Account` folder appl
@@ -757,6 +762,8 @@ The components that must adopt static SSR in the `Account` folder aren't require
757
762
758
763
### Static SSR components spread out across the app
759
764
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
+
760
767
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.
761
768
762
769
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.
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>.
0 commit comments