Skip to content

Commit 69ad372

Browse files
committed
Don't use slug for canoncial url
1 parent 9fa1823 commit 69ad372

File tree

4 files changed

+36
-9
lines changed

4 files changed

+36
-9
lines changed

src/LinkDotNet.Blog.Web/Features/Components/OgData.razor

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
@inject NavigationManager NavigationManager
1+
@using System.IO
2+
@inject NavigationManager NavigationManager
23

34
<HeadContent>
45
<meta name="title" property="og:title" content="@Title" />
@@ -17,12 +18,12 @@
1718
{
1819
<meta name="description" property="og:description" content="@Description" />
1920
}
20-
21+
2122
@if (ChildContent is not null)
2223
{
2324
@ChildContent
2425
}
25-
26+
2627
</HeadContent>
2728
@code {
2829

@@ -38,12 +39,20 @@
3839
[Parameter]
3940
public string Keywords { get; set; }
4041

42+
[Parameter]
43+
public string CanonicalRelativeUrl { get; set; }
44+
4145
[Parameter]
4246
public RenderFragment ChildContent { get; set; }
4347

4448
private string GetCanoncialUri()
4549
{
50+
if (!string.IsNullOrEmpty(CanonicalRelativeUrl))
51+
{
52+
return Path.Combine(NavigationManager.BaseUri, CanonicalRelativeUrl);
53+
}
54+
4655
var uri = new Uri(NavigationManager.Uri);
4756
return uri.GetLeftPart(UriPartial.Path);
4857
}
49-
}
58+
}

src/LinkDotNet.Blog.Web/Features/ShowBlogPost/ShowBlogPostPage.razor

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
@inject IOptions<ApplicationConfiguration> AppConfiguration
1111
@inject IOptions<ProfileInformation> ProfileInformation
1212

13-
@if (BlogPost == null)
13+
@if (BlogPost is null)
1414
{
1515
<Loading></Loading>
1616
}
@@ -20,7 +20,8 @@ else
2020
<OgData Title="@BlogPost.Title"
2121
AbsolutePreviewImageUrl="@OgDataImage"
2222
Description="@(Markdown.ToPlainText(BlogPost.ShortDescription))"
23-
Keywords="@BlogPost.TagsAsString">
23+
Keywords="@BlogPost.TagsAsString"
24+
CanonicalRelativeUrl="@BlogPostCanoncialUrl">
2425
<StructuredData Headline="@BlogPost.Title"
2526
PreviewImage="@BlogPost.PreviewImageUrl"
2627
PreviewFallbackImage="@BlogPost.PreviewImageUrlFallback"
@@ -77,6 +78,7 @@ else
7778
public string Slug { get; set; }
7879

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

8183
private BlogPost BlogPost { get; set; }
8284

tests/LinkDotNet.Blog.UnitTests/Domain/BlogPostTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void GivenBlogPostWithTags_WhenCreatingStringFromTags_ThenTagsAreSeparate
151151

152152
var tags = bp.TagsAsString;
153153

154-
tags.Should().Be("tag 1, tag 2");
154+
tags.Should().Be("tag 1,tag 2");
155155
}
156156

157157
[Fact]

tests/LinkDotNet.Blog.UnitTests/Web/Features/ShowBlogPost/ShowBlogPostPageTests.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ public void ShouldShowLoadingAnimation()
4141
[Fact]
4242
public void ShouldSetTitleToTag()
4343
{
44-
JSInterop.Mode = JSRuntimeMode.Loose;
4544
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
4645
var blogPost = new BlogPostBuilder().WithTitle("Title").Build();
4746
repositoryMock.GetByIdAsync("1").Returns(blogPost);
@@ -61,7 +60,6 @@ public void ShouldSetTitleToTag()
6160
[InlineData("url1", "url2", "url2")]
6261
public void ShouldUseFallbackAsOgDataIfAvailable(string preview, string fallback, string expected)
6362
{
64-
JSInterop.Mode = JSRuntimeMode.Loose;
6563
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
6664
var blogPost = new BlogPostBuilder()
6765
.WithPreviewImageUrl(preview)
@@ -135,6 +133,24 @@ public void ShowReadingIndicatorWhenEnabled(bool isEnabled)
135133
cut.HasComponent<ReadingIndicator>().Should().Be(isEnabled);
136134
}
137135

136+
[Fact]
137+
public void ShouldSetCanoncialUrlOfOgDataWithoutSlug()
138+
{
139+
var repositoryMock = Substitute.For<IRepository<BlogPost>>();
140+
var blogPost = new BlogPostBuilder()
141+
.WithTitle("sample")
142+
.Build();
143+
blogPost.Id = "1";
144+
repositoryMock.GetByIdAsync("1").Returns(blogPost);
145+
Services.AddScoped(_ => repositoryMock);
146+
SetupMocks();
147+
148+
var cut = RenderComponent<ShowBlogPostPage>(
149+
p => p.Add(s => s.BlogPostId, "1"));
150+
151+
cut.FindComponent<OgData>().Instance.CanonicalRelativeUrl.Should().Be("blogPost/1");
152+
}
153+
138154
private void SetupMocks()
139155
{
140156
JSInterop.Mode = JSRuntimeMode.Loose;

0 commit comments

Comments
 (0)