-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Blazor] Specialize pipeline for MapRazorComponents #47649
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
78 changes: 78 additions & 0 deletions
78
src/Components/Endpoints/src/RazorComponentEndpointInvoker.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Buffers; | ||
| using System.Text; | ||
| using System.Text.Encodings.Web; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.WebUtilities; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
|
||
| internal class RazorComponentEndpointInvoker | ||
| { | ||
| private readonly HttpContext _context; | ||
| private readonly EndpointHtmlRenderer _renderer; | ||
| private readonly Type _componentType; | ||
|
|
||
| public RazorComponentEndpointInvoker(HttpContext context, Type componentType) | ||
| { | ||
| _context = context; | ||
| _renderer = _context.RequestServices.GetRequiredService<EndpointHtmlRenderer>(); | ||
| _componentType = componentType; | ||
| } | ||
|
|
||
| public Task RenderComponent() | ||
| { | ||
| return _renderer.Dispatcher.InvokeAsync(RenderComponentCore); | ||
| } | ||
|
|
||
| private async Task RenderComponentCore() | ||
| { | ||
| _context.Response.ContentType = RazorComponentResultExecutor.DefaultContentType; | ||
|
|
||
| // We could pool these dictionary instances if we wanted, and possibly even the ParameterView | ||
| // backing buffers could come from a pool like they do during rendering. | ||
| var hostParameters = ParameterView.FromDictionary(new Dictionary<string, object?> | ||
| { | ||
| { nameof(RazorComponentEndpointHost.RenderMode), RenderMode.Static }, | ||
| { nameof(RazorComponentEndpointHost.ComponentType), _componentType }, | ||
| { nameof(RazorComponentEndpointHost.ComponentParameters), null }, | ||
| }); | ||
|
|
||
| await using var writer = CreateResponseWriter(_context.Response.Body); | ||
|
|
||
| // Note that we always use Static rendering mode for the top-level output from a RazorComponentResult, | ||
| // because you never want to serialize the invocation of RazorComponentResultHost. Instead, that host | ||
| // component takes care of switching into your desired render mode when it produces its own output. | ||
| var htmlContent = await _renderer.RenderEndpointComponent( | ||
| _context, | ||
| typeof(RazorComponentEndpointHost), | ||
| hostParameters, | ||
| waitForQuiescence: false); | ||
|
|
||
| // Importantly, we must not yield this thread (which holds exclusive access to the renderer sync context) | ||
| // in between the first call to htmlContent.WriteTo and the point where we start listening for subsequent | ||
| // streaming SSR batches (inside SendStreamingUpdatesAsync). Otherwise some other code might dispatch to the | ||
| // renderer sync context and cause a batch that would get missed. | ||
| htmlContent.WriteTo(writer, HtmlEncoder.Default); // Don't use WriteToAsync, as per the comment above | ||
|
|
||
| if (!htmlContent.QuiescenceTask.IsCompleted) | ||
| { | ||
| await _renderer.SendStreamingUpdatesAsync(_context, htmlContent.QuiescenceTask, writer); | ||
| } | ||
|
|
||
| // Invoke FlushAsync to ensure any buffered content is asynchronously written to the underlying | ||
| // response asynchronously. In the absence of this line, the buffer gets synchronously written to the | ||
| // response as part of the Dispose which has a perf impact. | ||
| await writer.FlushAsync(); | ||
| } | ||
|
|
||
| private static TextWriter CreateResponseWriter(Stream bodyStream) | ||
| { | ||
| // Matches MVC's MemoryPoolHttpResponseStreamWriterFactory.DefaultBufferSize | ||
| const int DefaultBufferSize = 16 * 1024; | ||
| return new HttpResponseStreamWriter(bodyStream, Encoding.UTF8, DefaultBufferSize, ArrayPool<byte>.Shared, ArrayPool<char>.Shared); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.