Skip to content

Preload fonts in use as per CSS definitions #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/Elastic.Markdown/Helpers/Preloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Licensed to Elasticsearch B.V under one or more agreements.
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information

using System.Collections.Immutable;
using System.Diagnostics;
using System.Reflection;
using System.Text.RegularExpressions;

namespace Elastic.Markdown.Helpers;

public static partial class FontPreloader
{
private static IReadOnlyCollection<string>? FontUriCache = null!;

public static async Task<IReadOnlyCollection<string>> GetFontUrisAsync() => FontUriCache ??= await LoadFontUrisAsync();
private static async Task<IReadOnlyCollection<string>> LoadFontUrisAsync()
{
var cachedFontUris = new List<string>();
var assembly = Assembly.GetExecutingAssembly();
var stylesResourceName = assembly.GetManifestResourceNames().First(n => n.EndsWith("styles.css"));

using var cssFileStream = new StreamReader(assembly.GetManifestResourceStream(stylesResourceName)!);

var cssFile = await cssFileStream.ReadToEndAsync();
var matches = FontUriRegex().Matches(cssFile);

foreach (Match match in matches)
{
if (match.Success)
cachedFontUris.Add($"/_static/{match.Groups[1].Value}");
}
FontUriCache = cachedFontUris;
return FontUriCache;
}

[GeneratedRegex(@"url\([""']?([^""'\)]+?\.(woff2|ttf|otf))[""']?\)", RegexOptions.Multiline | RegexOptions.Compiled)]
private static partial Regex FontUriRegex();
}
7 changes: 6 additions & 1 deletion src/Elastic.Markdown/Slices/Layout/_Head.cshtml
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
@inherits RazorSlice<LayoutViewModel>
@using Elastic.Markdown.Helpers
<head>
<title>@Model.Title</title>
<link rel="stylesheet" type="text/css" href="@Model.Static("styles.css")"/>
@foreach (var fontFile in await FontPreloader.GetFontUrisAsync())
{
<link rel="preload" href="@fontFile" as="font" type="font/woff2" crossorigin>
}
<link rel="stylesheet preload" as="style" type="text/css" href="@Model.Static("styles.css")" crossorigin/>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
@await RenderPartialAsync(_Favicon.Create())
Expand Down
Loading