SEO optimization on the hosted pages

This commit is contained in:
2025-11-22 16:45:44 +08:00
parent c62ed191f3
commit 3800dae8b7
5 changed files with 164 additions and 3 deletions

View File

@@ -19,11 +19,13 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference> </PackageReference>
<PackageReference Include="NodaTime.Serialization.Protobuf" Version="2.0.2" /> <PackageReference Include="NodaTime.Serialization.Protobuf" Version="2.0.2" />
<PackageReference Include="NodaTime" Version="3.2.2"/> <PackageReference Include="NodaTime" Version="3.2.2" />
<PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.3.0"/> <PackageReference Include="NodaTime.Serialization.SystemTextJson" Version="1.3.0" />
<PackageReference Include="Grpc.AspNetCore.Server" Version="2.71.0"/> <PackageReference Include="Grpc.AspNetCore.Server" Version="2.71.0" />
<PackageReference Include="Quartz" Version="3.15.1" /> <PackageReference Include="Quartz" Version="3.15.1" />
<PackageReference Include="Quartz.AspNetCore" Version="3.15.1" /> <PackageReference Include="Quartz.AspNetCore" Version="3.15.1" />
<PackageReference Include="SimpleMvcSitemap" Version="4.0.1" />
<PackageReference Include="System.ServiceModel.Syndication" Version="10.0.0" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>

View File

@@ -16,6 +16,8 @@
<link rel="icon" type="image/png" href="~/favicon.png" /> <link rel="icon" type="image/png" href="~/favicon.png" />
<link rel="alternate" type="application/rss+xml" title="Solar Network Posts" href="~/rss.xml" />
@await RenderSectionAsync("Head", required: false) @await RenderSectionAsync("Head", required: false)
</head> </head>
<body> <body>

View File

@@ -0,0 +1,71 @@
using System.ServiceModel.Syndication;
using System.Xml;
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Markdig;
using Microsoft.AspNetCore.Mvc;
namespace DysonNetwork.Zone.SEO;
[ApiController]
public class RssController(PostService.PostServiceClient postClient) : ControllerBase
{
[HttpGet("rss")]
[Produces("application/rss+xml")]
public async Task<IActionResult> Rss()
{
var feed = new SyndicationFeed(
"Solar Network Posts",
"Latest posts from Solar Network",
new Uri($"{Request.Scheme}://{Request.Host}/")
);
var items = new List<SyndicationItem>();
// Fetch posts - similar to SitemapController, but for RSS we usually only want recent ones
// For simplicity, let's fetch the first page of posts
var request = new ListPostsRequest
{
OrderBy = "date",
OrderDesc = true,
PageSize = 20 // Get top 20 recent posts
};
var response = await postClient.ListPostsAsync(request);
if (response?.Posts != null)
{
foreach (var protoPost in response.Posts)
{
var post = SnPost.FromProtoValue(protoPost);
var postUrl = post.AsUrl(Request.Host.ToString(), Request.Scheme); // Using the extension method
var item = new SyndicationItem(
post.Title,
post.Content is not null ? Markdown.ToHtml(post.Content!) : "No content", // Convert Markdown to HTML
new Uri(postUrl),
post.Id.ToString(),
post.EditedAt?.ToDateTimeOffset() ??
post.PublishedAt?.ToDateTimeOffset() ?? post.CreatedAt.ToDateTimeOffset()
)
{
PublishDate = post.PublishedAt?.ToDateTimeOffset() ??
post.CreatedAt.ToDateTimeOffset() // Use CreatedAt for publish date
};
items.Add(item);
}
}
feed.Items = items;
await using var sw = new StringWriter();
await using var reader = XmlWriter.Create(sw, new XmlWriterSettings { Indent = true, Async = true });
var formatter = new Rss20FeedFormatter(feed);
formatter.WriteTo(reader);
await reader.FlushAsync();
return Content(sw.ToString(), "application/rss+xml");
}
}

View File

@@ -0,0 +1,74 @@
using DysonNetwork.Shared.Models;
using DysonNetwork.Shared.Proto;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using SimpleMvcSitemap;
namespace DysonNetwork.Zone.SEO;
[ApiController]
public class SitemapController(
AppDatabase db,
PostService.PostServiceClient postClient
)
: ControllerBase
{
[HttpGet("sitemap.xml")]
public async Task<IActionResult> Sitemap()
{
var nodes = new List<SitemapNode>
{
// Add static pages
new("/")
{ ChangeFrequency = ChangeFrequency.Weekly, Priority = 1.0m },
new("/about")
{ ChangeFrequency = ChangeFrequency.Monthly, Priority = 0.8m },
new("/posts")
{ ChangeFrequency = ChangeFrequency.Daily, Priority = 0.9m }
};
// Add dynamic posts
var allPosts = await GetAllPosts();
nodes.AddRange(allPosts.Select(post =>
{
var uri = post.AsUrl(Request.Host.ToString(), Request.Scheme);
return new SitemapNode(uri)
{
LastModificationDate = post.EditedAt?.ToDateTimeUtc() ?? post.CreatedAt.ToDateTimeUtc(),
ChangeFrequency = ChangeFrequency.Monthly, Priority = 0.7m
};
}));
return new SitemapProvider().CreateSitemap(new SitemapModel(nodes));
}
private async Task<List<SnPost>> GetAllPosts()
{
var allPosts = new List<SnPost>();
string? pageToken = null;
const int pageSize = 100; // Fetch in batches
while (true)
{
var request = new ListPostsRequest
{
OrderBy = "date",
OrderDesc = true,
PageSize = pageSize,
PageToken = pageToken ?? string.Empty
};
var response = await postClient.ListPostsAsync(request);
if (response?.Posts != null)
allPosts.AddRange(response.Posts.Select(SnPost.FromProtoValue));
if (string.IsNullOrEmpty(response?.NextPageToken))
break;
pageToken = response.NextPageToken;
}
return allPosts;
}
}

View File

@@ -0,0 +1,12 @@
using DysonNetwork.Shared.Models;
using Microsoft.AspNetCore.Mvc;
namespace DysonNetwork.Zone.SEO;
public static class SnPostExtensions
{
public static string AsUrl(this SnPost post, string host, string scheme)
{
return $"{scheme}://{host}/p/{post.Slug ?? post.Id.ToString()}";
}
}