Skip to content
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

Error.razor should always be a static server rendered page #51031

Closed
danroth27 opened this issue Sep 29, 2023 · 4 comments
Closed

Error.razor should always be a static server rendered page #51031

danroth27 opened this issue Sep 29, 2023 · 4 comments
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@danroth27
Copy link
Member

If you create new Blazor Web App with interactive WebAssembly rendering enabled globally, then the Error.razor page will get included in the client project. But this doesn't really make sense. The error page uses the HttpContext, which isn't available on the client. The error page should always be a statically rendered page from the server.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Sep 29, 2023
@danroth27
Copy link
Member Author

@danroth27
Copy link
Member Author

I tried moving Error.razor to the server project and then setting the render mode of the router dynamically based on the file path:

<body>
    <Routes @rendermode="@RenderModeForPage" />
    <script src="_framework/blazor.web.js"></script>
</body>

</html>

@code {
    [CascadingParameter] HttpContext HttpContext { get; set; } = default!;

    IComponentRenderMode? RenderModeForPage => HttpContext.Request.Path.StartsWithSegments("/error")
        ? null
        : RenderMode.InteractiveWebAssembly;
}

But then I get an ArgumentNullException when trying to set the render mode to null:

System.ArgumentNullException: Value cannot be null. (Parameter 'renderMode')
   at System.ArgumentNullException.Throw(String paramName)
   at System.ArgumentNullException.ThrowIfNull(Object argument, String paramName)
   at Microsoft.AspNetCore.Components.Rendering.RenderTreeBuilder.AddComponentRenderMode(IComponentRenderMode renderMode)
   at BlazorApp7.Components.App.BuildRenderTree(RenderTreeBuilder __builder) in C:\Users\daroth\source\repos\BlazorApp7\BlazorApp7\Components\App.razor:line 16
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.AddToRenderQueue(Int32 componentId, RenderFragment renderFragment)
   at Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()
   at Microsoft.AspNetCore.Components.ComponentBase.CallOnParametersSetAsync()
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.SetDirectParameters(ParameterView parameters)
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.RenderRootComponentAsync(Int32 componentId, ParameterView initialParameters)
   at Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.BeginRenderingComponent(IComponent component, ParameterView initialParameters)
   at Microsoft.AspNetCore.Components.Endpoints.EndpointHtmlRenderer.RenderEndpointComponent(HttpContext httpContext, Type rootComponentType, ParameterView parameters, Boolean waitForQuiescence)
   at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
   at Microsoft.AspNetCore.Components.Endpoints.RazorComponentEndpointInvoker.RenderComponentCore(HttpContext context)
   at Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.<>c.<<InvokeAsync>b__10_0>d.MoveNext()
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

It looks we might need to change the runtime to provide a way to set the render mode to static server rendering.

@SteveSandersonMS
Copy link
Member

SteveSandersonMS commented Oct 2, 2023

It looks we might need to change the runtime to provide a way to set the render mode to static server rendering.

Yes, I think you're right, and this is going to affect #50920 (Auth with global interactivity) because it's exactly how we're most likely going to do it. cc @halter73

The reason it worked in my PR is probably that it didn't yet have the Razor compiler changes for @rendermode and hence was behaving as a regular attribute that gets elided if you pass null. We can change AddComponentRenderMode in the runtime to treat null as a no-op.

@mkArtakMSFT mkArtakMSFT added the bug This issue describes a behavior which is not expected - a bug. label Oct 4, 2023
@mkArtakMSFT mkArtakMSFT added this to the 8.0.0 milestone Oct 4, 2023
@halter73
Copy link
Member

This was fixed by #51134

@ghost ghost locked as resolved and limited conversation to collaborators Nov 11, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Projects
None yet
Development

No branches or pull requests

4 participants