Skip to content

Commit 6fb6465

Browse files
authored
[Blazor] Cache LayoutAttribute lookup (#48439)
* Cache the lookup for the layout attribute in the routeview.
1 parent be14dda commit 6fb6465

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/Components/Components/src/RouteView.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33

44
#nullable disable warnings
55

6+
using System.Collections.Concurrent;
67
using System.Diagnostics.CodeAnalysis;
78
using System.Reflection;
9+
using Microsoft.AspNetCore.Components.HotReload;
810
using Microsoft.AspNetCore.Components.Rendering;
911
using Microsoft.AspNetCore.Components.Routing;
1012

@@ -17,6 +19,15 @@ namespace Microsoft.AspNetCore.Components;
1719
public class RouteView : IComponent
1820
{
1921
private RenderHandle _renderHandle;
22+
private static readonly ConcurrentDictionary<Type, Type?> _layoutAttributeCache = new();
23+
24+
static RouteView()
25+
{
26+
if (HotReloadManager.Default.MetadataUpdateSupported)
27+
{
28+
HotReloadManager.Default.OnDeltaApplied += _layoutAttributeCache.Clear;
29+
}
30+
}
2031

2132
[Inject]
2233
private NavigationManager NavigationManager { get; set; }
@@ -65,7 +76,8 @@ public Task SetParametersAsync(ParameterView parameters)
6576
[UnconditionalSuppressMessage("Trimming", "IL2118", Justification = "Layout components are preserved because the LayoutAttribute constructor parameter is correctly annotated.")]
6677
protected virtual void Render(RenderTreeBuilder builder)
6778
{
68-
var pageLayoutType = RouteData.PageType.GetCustomAttribute<LayoutAttribute>()?.LayoutType
79+
var pageLayoutType = _layoutAttributeCache
80+
.GetOrAdd(RouteData.PageType, static type => type.GetCustomAttribute<LayoutAttribute>()?.LayoutType)
6981
?? DefaultLayout;
7082

7183
builder.OpenComponent<LayoutView>(0);

0 commit comments

Comments
 (0)