Skip to content

Commit 8549c9e

Browse files
committed
Don't use query parameters for canoncial url
1 parent ab1f05f commit 8549c9e

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<HeadContent>
44
<meta name="title" property="og:title" content="@Title" />
5-
<link rel="canonical" href="@NavigationManager.Uri" />
5+
<link rel="canonical" href="@GetCanoncialUri()" />
66
@if (AbsolutePreviewImageUrl != null)
77
{
88
<meta name="image" property="og:image" content="@AbsolutePreviewImageUrl"/>
@@ -25,10 +25,17 @@
2525

2626
[Parameter]
2727
public string AbsolutePreviewImageUrl { get; set; }
28-
28+
2929
[Parameter]
3030
public string Description { get; set; }
31-
31+
3232
[Parameter]
3333
public string Keywords { get; set; }
34+
35+
private string GetCanoncialUri()
36+
{
37+
var uri = new Uri(NavigationManager.Uri);
38+
return uri.GetLeftPart(UriPartial.Path);
39+
}
40+
3441
}

tests/LinkDotNet.Blog.UnitTests/Web/Features/Components/OgDataTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,31 @@ public void ShouldNotSetMetaInformationWhenNotProvided()
4242
[Fact]
4343
public void ShouldSetCanoncialLink()
4444
{
45+
const string expectedUri = "https://steven.com/test";
46+
Services.GetRequiredService<NavigationManager>().NavigateTo(expectedUri);
4547
ComponentFactories.AddStub<HeadContent>(ps => ps.Get(p => p.ChildContent));
4648

4749
var cut = RenderComponent<OgData>(p => p
4850
.Add(s => s.Title, "Title"));
4951

5052
var link = cut.FindAll("link").FirstOrDefault(l => l.Attributes.Any(a => a.Name == "rel" && a.Value == "canonical")) as IHtmlLinkElement;
51-
var expectedUri = Services.GetRequiredService<NavigationManager>().Uri;
53+
5254
link.Href.Should().Be(expectedUri);
5355
}
5456

57+
[Fact]
58+
public void ShouldSetCanoncialLinkWithoutQueryParameter()
59+
{
60+
ComponentFactories.AddStub<HeadContent>(ps => ps.Get(p => p.ChildContent));
61+
Services.GetRequiredService<NavigationManager>().NavigateTo("https://localhost.com/site?query=2");
62+
63+
var cut = RenderComponent<OgData>(p => p
64+
.Add(s => s.Title, "Title"));
65+
66+
var link = cut.FindAll("link").FirstOrDefault(l => l.Attributes.Any(a => a.Name == "rel" && a.Value == "canonical")) as IHtmlLinkElement;
67+
link.Href.Should().Be("https://localhost.com/site");
68+
}
69+
5570
private static void AssertMetaTagExistsWithValue(
5671
IRenderedFragment cut,
5772
string metaTag,

0 commit comments

Comments
 (0)