-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Influence HTML head from Blazor #23833
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
Show all changes
21 commits
Select commit
Hold shift + click to select a range
79aa2de
Started on head manager
MackinnonBuck 5eb0bf3
Working prototype.
MackinnonBuck 1166641
Started on improved meta tag support.
MackinnonBuck 4de103a
Improved meta support and better thread safety.
MackinnonBuck 75183d9
Improved synchronization.
MackinnonBuck 2e74f3e
Added support for meta "property" attribute.
MackinnonBuck 0b97601
Updated exception message.
MackinnonBuck 687b3f8
Added link element support.
MackinnonBuck 34f9733
Started on functional tests.
MackinnonBuck 0ad5577
Added more functional tests.
MackinnonBuck 51ca920
Added E2E tests.
MackinnonBuck 9577da8
Simplified implementation.
MackinnonBuck 01ae712
Prerendering support.
MackinnonBuck 74ef65a
Small documentation updates.
MackinnonBuck 0367f9c
Made TagElement and TitleElement readonly.
MackinnonBuck 307d582
Removed M.A.Components.Server dependency.
MackinnonBuck 23fdb48
Minor fixes and updates.
MackinnonBuck ef05f60
Update PrerenderedHeadComponent.razor
MackinnonBuck 29d5a71
Removed AddWebExtensions.
MackinnonBuck 68d3b53
Merge branch 'release/5.0-preview8' of https://github.com/dotnet/aspn…
MackinnonBuck 2c0eb50
Applied CR feedback
MackinnonBuck 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
28 changes: 28 additions & 0 deletions
28
src/Components/Web.Extensions/src/HeadManagement/HeadManagementJSRuntimeExtensions.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,28 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Threading.Tasks; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
internal static class HeadManagementJSRuntimeExtensions | ||
{ | ||
private const string JsFunctionsPrefix = "_blazorHeadManager"; | ||
|
||
public static ValueTask SetTitleAsync(this IJSRuntime jsRuntime, string title) | ||
{ | ||
return jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.setTitle", title); | ||
} | ||
|
||
public static ValueTask AddOrUpdateHeadTagAsync(this IJSRuntime jsRuntime, TagElement tag, string id) | ||
{ | ||
return jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.addOrUpdateHeadTag", tag, id); | ||
} | ||
|
||
public static ValueTask RemoveHeadTagAsync(this IJSRuntime jsRuntime, string id) | ||
{ | ||
return jsRuntime.InvokeVoidAsync($"{JsFunctionsPrefix}.removeHeadTag", id); | ||
} | ||
MackinnonBuck marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Components/Web.Extensions/src/HeadManagement/HeadTagBase.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,67 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
/// <summary> | ||
/// Serves as a base for components that represent tags in the HTML head. | ||
/// </summary> | ||
public abstract class HeadTagBase : ComponentBase, IDisposable | ||
{ | ||
private readonly string _id = Guid.NewGuid().ToString("N"); | ||
|
||
private TagElement _tagElement; | ||
|
||
private bool _hasRendered; | ||
|
||
[Inject] | ||
private IJSRuntime JSRuntime { get; set; } = default!; | ||
|
||
/// <summary> | ||
/// Gets or sets a collection of additional attributes that will be applied to the meta element. | ||
/// </summary> | ||
[Parameter(CaptureUnmatchedValues = true)] | ||
public IReadOnlyDictionary<string, object>? Attributes { get; set; } | ||
|
||
/// <summary> | ||
/// Gets the name of the tag being represented. | ||
/// </summary> | ||
protected abstract string TagName { get; } | ||
|
||
/// <inheritdoc /> | ||
protected override void OnParametersSet() | ||
{ | ||
_tagElement = new TagElement(TagName, Attributes); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
_hasRendered = true; | ||
|
||
await JSRuntime.AddOrUpdateHeadTagAsync(_tagElement, _id); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
builder.AddMarkupContent(0, $"<!--Head:{JsonSerializer.Serialize(_tagElement, JsonSerializerOptionsProvider.Options)}-->"); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Dispose() | ||
{ | ||
if (_hasRendered) | ||
{ | ||
_ = JSRuntime.RemoveHeadTagAsync(_id); | ||
} | ||
} | ||
} | ||
} |
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,14 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
/// <summary> | ||
/// A component that adds a link tag to the HTML head. | ||
/// </summary> | ||
public sealed class Link : HeadTagBase | ||
{ | ||
/// <inheritdoc /> | ||
protected override string TagName => "link"; | ||
} | ||
} |
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,14 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
/// <summary> | ||
/// A component that adds a meta tag to the HTML head. | ||
/// </summary> | ||
public sealed class Meta : HeadTagBase | ||
{ | ||
/// <inheritdoc /> | ||
protected override string TagName => "meta"; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
src/Components/Web.Extensions/src/HeadManagement/TagElement.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,22 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
internal readonly struct TagElement | ||
{ | ||
public string Type => "tag"; | ||
|
||
public string TagName { get; } | ||
|
||
public IReadOnlyDictionary<string, object>? Attributes { get; } | ||
|
||
public TagElement(string tagName, IReadOnlyDictionary<string, object>? attributes) | ||
{ | ||
TagName = tagName; | ||
Attributes = attributes; | ||
} | ||
} | ||
} |
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,37 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Components.Rendering; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
/// <summary> | ||
/// A component that changes the title of the document. | ||
/// </summary> | ||
public sealed class Title : ComponentBase | ||
{ | ||
[Inject] | ||
private IJSRuntime JSRuntime { get; set; } = default!; | ||
|
||
/// <summary> | ||
/// Gets or sets the value to use as the document's title. | ||
/// </summary> | ||
[Parameter] | ||
public string Value { get; set; } = string.Empty; | ||
|
||
/// <inheritdoc /> | ||
protected override async Task OnAfterRenderAsync(bool firstRender) | ||
{ | ||
await JSRuntime.SetTitleAsync(Value); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override void BuildRenderTree(RenderTreeBuilder builder) | ||
{ | ||
builder.AddMarkupContent(0, $"<!--Head:{JsonSerializer.Serialize(new TitleElement(Value), JsonSerializerOptionsProvider.Options)}-->"); | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Components/Web.Extensions/src/HeadManagement/TitleElement.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,17 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
namespace Microsoft.AspNetCore.Components.Web.Extensions.Head | ||
{ | ||
internal readonly struct TitleElement | ||
{ | ||
public string Type => "title"; | ||
|
||
public string Title { get; } | ||
|
||
public TitleElement(string title) | ||
{ | ||
Title = title; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
using System.Runtime.CompilerServices; | ||
|
||
[assembly: InternalsVisibleTo("Microsoft.AspNetCore.Components.Web.Extensions.Tests, PublicKey=0024000004800000940000000602000000240000525341310004000001000100f33a29044fa9d740c9b3213a93e57c84b472c84e0b8a0e1ae48e67a9f8f6de9d5f7f3d52ac23e48ac51801f1dc950abe901da34d2a9e3baadb141a17c77ef3c565dd5ee5054b91cf63bb3c6ab83f72ab3aafe93d0fc3c2348b764fafb0b1c0733de51459aeab46580384bf9d74c4e28164b7cde247f891ba07891c9d872ad2bb")] |
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.