Make the prefetch supports typescript and opengraph.

 Use prefetch in Solarpass pfp
This commit is contained in:
2025-08-02 22:15:06 +08:00
parent be7d7536fc
commit 665538bdd3
11 changed files with 90 additions and 11 deletions

View File

@@ -3,6 +3,9 @@ using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using NodaTime.Serialization.SystemTextJson;
using OpenGraphNet;
namespace DysonNetwork.Shared.PageData;
@@ -24,6 +27,13 @@ public static class PageStartup
/// <returns></returns>
public static WebApplication MapPages(this WebApplication app, string defaultFile)
{
var jsonOpts = new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower,
DictionaryKeyPolicy = JsonNamingPolicy.SnakeCaseLower,
PropertyNameCaseInsensitive = true,
}.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
#pragma warning disable ASP0016
app.MapFallback(async context =>
{
@@ -33,7 +43,7 @@ public static class PageStartup
await context.Response.WriteAsync("Not found");
return;
}
var html = await File.ReadAllTextAsync(defaultFile);
using var scope = app.Services.CreateScope();
@@ -50,8 +60,15 @@ public static class PageStartup
foreach (var (key, value) in result)
appData[key] = value;
var json = JsonSerializer.Serialize(appData);
html = html.Replace("<app-data />", $"<script>window.__APP_DATA__ = {json};</script>");
OpenGraph? og = null;
if (appData.TryGetValue("OpenGraph", out var openGraph) && openGraph is OpenGraph gog)
og = gog;
var json = JsonSerializer.Serialize(appData, jsonOpts);
html = html.Replace("<app-data />", $"<script>window.DyPrefetch = {json};</script>");
if (og is not null)
html = html.Replace("<og-data />", og.ToString());
context.Response.ContentType = "text/html";
await context.Response.WriteAsync(html);