generated from NetCoreTemplates/razor-ssg
/
Configure.Ssg.cs
168 lines (145 loc) · 6.74 KB
/
Configure.Ssg.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Diagnostics;
using System.Globalization;
using System.Text;
using ServiceStack.Host;
using ServiceStack.IO;
using ServiceStack.Logging;
using ServiceStack.Text;
[assembly: HostingStartup(typeof(MyApp.ConfigureSsg))]
namespace MyApp;
public class ConfigureSsg : IHostingStartup
{
public void Configure(IWebHostBuilder builder) => builder
.ConfigureServices((context,services) =>
{
services.AddSingleton<RazorPagesEngine>();
services.AddSingleton<MarkdownPages>();
services.AddSingleton<MarkdownBlog>();
services.AddSingleton<IServiceGateway>(implementationFactory:
provider => new JsonApiClient(AppConfig.Instance.ApiBaseUrl!));
services.AddSingleton<UserState>();
})
.ConfigureAppHost(
appHost => appHost.Plugins.Add(new CleanUrlsFeature()),
afterPluginsLoaded: appHost =>
{
var pages = appHost.Resolve<MarkdownPages>();
var blogPosts = appHost.Resolve<MarkdownBlog>();
var markdownFeatures = new IMarkdownPages[] { pages, blogPosts };
markdownFeatures.Each(x => x.VirtualFiles = appHost.VirtualFiles);
blogPosts.Authors = Authors;
pages.LoadFrom("_pages");
blogPosts.LoadFrom("_posts");
var userState = appHost.Resolve<UserState>();
userState.LoadAsync().GetAwaiter().GetResult();
},
afterAppHostInit: appHost =>
{
// prerender with: `$ npm run prerender`
AppTasks.Register("prerender", args =>
{
var distDir = appHost.ContentRootDirectory.RealPath.CombineWith("dist");
if (Directory.Exists(distDir))
FileSystemVirtualFiles.DeleteDirectory(distDir);
FileSystemVirtualFiles.CopyAll(
new DirectoryInfo(appHost.ContentRootDirectory.RealPath.CombineWith("wwwroot")),
new DirectoryInfo(distDir));
var razorFiles = appHost.VirtualFiles.GetAllMatchingFiles("*.cshtml");
RazorSsg.PrerenderAsync(appHost, razorFiles, distDir).GetAwaiter().GetResult();
var appConfig = AppConfig.Instance;
PrerenderSitemapAsync(appHost, distDir, new JsonApiClient(appConfig.ApiBaseUrl!), appConfig.BaseUrl!).GetAwaiter().GetResult();
});
});
public List<AuthorInfo> Authors { get; } = new()
{
new AuthorInfo("Lucy Bates", "img/authors/author1.svg")
{
TwitterUrl = "https://twitter.com/lucy",
GitHubUrl = "https://github.com/lucy",
},
new AuthorInfo("Gayle Smith", "img/authors/author2.svg")
{
TwitterUrl = "https://twitter.com/gayle",
},
new AuthorInfo("Brandon Foley", "img/authors/author3.svg")
{
GitHubUrl = "https://github.com/brandon",
},
};
async Task PrerenderSitemapAsync(ServiceStackHost appHost, string distDir, JsonApiClient client, string baseUrl)
{
var log = appHost.Resolve<ILogger<SitemapFeature>>();
log.LogInformation("Prerendering Sitemap...");
var sw = Stopwatch.StartNew();
var feature = await CreateSitemapAsync(log, client, baseUrl);
await feature.RenderToAsync(distDir);
log.LogInformation("Sitemap took {Elapsed} to prerender", sw.Elapsed.Humanize());
}
async Task<SitemapFeature> CreateSitemapAsync(ILogger log, JsonApiClient client, string baseUrl)
{
var albums = (await client.ApiAsync(new QueryAlbums())).Response!.Results;
DateTime ValidDate(DateTime date) =>
date.Year < 2000 ? new DateTime(DateTime.UtcNow.Year, date.Month, date.Day) : date;
var to = new SitemapFeature {
SitemapIndex =
{
new Sitemap
{
Location = baseUrl.CombineWith("/sitemaps/sitemap-albums.xml"),
AtPath = "/sitemaps/sitemap-albums.xml",
LastModified = ValidDate(albums.Max(x => x.ModifiedDate)),
UrlSet = albums.Map(album => new SitemapUrl {
Location = baseUrl.CombineWith($"/albums/{album.Slug}"),
LastModified = ValidDate(album.Artifacts.Max(x => x.ModifiedDate)),
ChangeFrequency = SitemapFrequency.Daily,
})
},
}
};
log.LogInformation("Albums Loaded: {Count}", albums.Count);
var api = await client.ApiAsync(new QueryArtifacts { Include = "total", Take = 1 });
var totalArtifacts = api.Response!.Total;
var now = DateTime.UtcNow.Date;
int latestPages = (int)Math.Ceiling(totalArtifacts / (double)UserState.StaticTake);
to.SitemapIndex.Add(new Sitemap
{
Location = baseUrl.CombineWith("/sitemaps/sitemap-latest.xml"),
AtPath = "/sitemaps/sitemap-latest.xml",
LastModified = ValidDate(albums.Max(x => x.ModifiedDate)),
UrlSet = latestPages.Times(i => new SitemapUrl {
Location = baseUrl.CombineWith("latest" + (i == 0 ? "" : "_" + (i + 1))),
LastModified = now,
ChangeFrequency = SitemapFrequency.Daily,
})
});
foreach (var album in albums)
{
var total = album.Artifacts.Count;
var pages = (int)Math.Ceiling(total / (double)UserState.StaticPagedTake);
var albumArtifacts = album.Artifacts;
for (var i = 0; i < pages; i++)
{
var pageNo = i + 1;
var suffix = pages == 1 ? "" : "_" + pageNo;
var path = $"/sitemaps/albums/sitemap-{album.Slug}{suffix}.xml";
var pageArtifacts = albumArtifacts.Skip(i * UserState.StaticPagedTake).Take(UserState.StaticPagedTake).ToList();
to.SitemapIndex.Add(new Sitemap
{
Location = baseUrl.CombineWith(path),
AtPath = path,
LastModified = ValidDate(pageArtifacts.Max(x => x.ModifiedDate)),
UrlSet = pageArtifacts.Map(x => new SitemapUrl {
Location = baseUrl.AddQueryParam("view", x.ArtifactId),
LastModified = ValidDate(x.ModifiedDate),
ChangeFrequency = SitemapFrequency.Monthly,
})
});
}
}
return to;
}
}
// Add additional frontmatter info to include
public class MarkdownFileInfo : MarkdownFileBase
{
}