Skip to content

Commit

Permalink
Don't use slug for canoncial url
Browse files Browse the repository at this point in the history
  • Loading branch information
linkdotnet committed Jan 1, 2024
1 parent 9fa1823 commit 69ad372
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
17 changes: 13 additions & 4 deletions src/LinkDotNet.Blog.Web/Features/Components/OgData.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@inject NavigationManager NavigationManager
@using System.IO
@inject NavigationManager NavigationManager

<HeadContent>
<meta name="title" property="og:title" content="@Title" />
Expand All @@ -17,12 +18,12 @@
{
<meta name="description" property="og:description" content="@Description" />
}

@if (ChildContent is not null)
{
@ChildContent
}

</HeadContent>
@code {

Expand All @@ -38,12 +39,20 @@
[Parameter]
public string Keywords { get; set; }

[Parameter]
public string CanonicalRelativeUrl { get; set; }

[Parameter]
public RenderFragment ChildContent { get; set; }

private string GetCanoncialUri()
{
if (!string.IsNullOrEmpty(CanonicalRelativeUrl))
{
return Path.Combine(NavigationManager.BaseUri, CanonicalRelativeUrl);
}

var uri = new Uri(NavigationManager.Uri);
return uri.GetLeftPart(UriPartial.Path);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@inject IOptions<ApplicationConfiguration> AppConfiguration
@inject IOptions<ProfileInformation> ProfileInformation

@if (BlogPost == null)
@if (BlogPost is null)
{
<Loading></Loading>
}
Expand All @@ -20,7 +20,8 @@ else
<OgData Title="@BlogPost.Title"
AbsolutePreviewImageUrl="@OgDataImage"
Description="@(Markdown.ToPlainText(BlogPost.ShortDescription))"
Keywords="@BlogPost.TagsAsString">
Keywords="@BlogPost.TagsAsString"
CanonicalRelativeUrl="@BlogPostCanoncialUrl">
<StructuredData Headline="@BlogPost.Title"
PreviewImage="@BlogPost.PreviewImageUrl"
PreviewFallbackImage="@BlogPost.PreviewImageUrlFallback"
Expand Down Expand Up @@ -77,6 +78,7 @@ else
public string Slug { get; set; }

private string OgDataImage => BlogPost.PreviewImageUrlFallback ?? BlogPost.PreviewImageUrl;
private string BlogPostCanoncialUrl => $"blogPost/{BlogPost.Id}";

private BlogPost BlogPost { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion tests/LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void GivenBlogPostWithTags_WhenCreatingStringFromTags_ThenTagsAreSeparate

var tags = bp.TagsAsString;

tags.Should().Be("tag 1, tag 2");
tags.Should().Be("tag 1,tag 2");
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public void ShouldShowLoadingAnimation()
[Fact]
public void ShouldSetTitleToTag()
{
JSInterop.Mode = JSRuntimeMode.Loose;
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
var blogPost = new BlogPostBuilder().WithTitle("Title").Build();
repositoryMock.GetByIdAsync("1").Returns(blogPost);
Expand All @@ -61,7 +60,6 @@ public void ShouldSetTitleToTag()
[InlineData("url1", "url2", "url2")]
public void ShouldUseFallbackAsOgDataIfAvailable(string preview, string fallback, string expected)
{
JSInterop.Mode = JSRuntimeMode.Loose;
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
var blogPost = new BlogPostBuilder()
.WithPreviewImageUrl(preview)
Expand Down Expand Up @@ -135,6 +133,24 @@ public void ShowReadingIndicatorWhenEnabled(bool isEnabled)
cut.HasComponent<ReadingIndicator>().Should().Be(isEnabled);
}

[Fact]
public void ShouldSetCanoncialUrlOfOgDataWithoutSlug()
{
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
var blogPost = new BlogPostBuilder()
.WithTitle("sample")
.Build();
blogPost.Id = "1";
repositoryMock.GetByIdAsync("1").Returns(blogPost);
Services.AddScoped(_ => repositoryMock);
SetupMocks();

var cut = RenderComponent<ShowBlogPostPage>(
p => p.Add(s => s.BlogPostId, "1"));

cut.FindComponent<OgData>().Instance.CanonicalRelativeUrl.Should().Be("blogPost/1");
}

private void SetupMocks()
{
JSInterop.Mode = JSRuntimeMode.Loose;
Expand Down

0 comments on commit 69ad372

Please sign in to comment.