Skip to content
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

Preload fonts in use as per CSS definitions #765

Merged
merged 4 commits into from
Mar 18, 2025
Merged

Conversation

cotti
Copy link
Contributor

@cotti cotti commented Mar 18, 2025

In order to preload fonts, a hint must be given to the browser in the form of a tag:

<link rel="preload" href="/path/to/font.woff2" as="font" type="font/woff2" crossorigin>

This PR includes a cached repository for the font definitions in .css files so we don't need to worry about adding these if fonts change.

Handles #704

@cotti cotti requested a review from a team as a code owner March 18, 2025 05:59
@cotti cotti added the feature label Mar 18, 2025
@cotti cotti self-assigned this Mar 18, 2025
@cotti cotti linked an issue Mar 18, 2025 that may be closed by this pull request
2 tasks
Copy link
Member

@Mpdreamz Mpdreamz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpicks, other than that lgtm.


public static partial class FontPreloader
{
private static List<string>? FontUriCache = null!;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

readonly field or getter only property.

Also IReadOnlyCollection instead of List.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this need to be thread safe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. Nevermind. If I understood correctly, the whole logic is only executed once.

private static List<string>? FontUriCache = null!;

public static async Task<IEnumerable<string>> GetFontUrisAsync() => FontUriCache ??= await LoadFontUrisAsync();
public static async Task<List<string>> LoadFontUrisAsync()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be made private

return FontUriCache;
}

[GeneratedRegex(@"url\([""']?([^""'\)]+)[""']?\)", RegexOptions.Multiline | RegexOptions.Compiled)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe stricter including src: key?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding also the file extension here.

the url function could in theory load any kind of file.

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to add the url path prefix here.

image

But nice! I can see it's the first being loaded.

@cotti cotti changed the title Draft: Preload fonts in use as per CSS definitions Preload fonts in use as per CSS definitions Mar 18, 2025
@cotti cotti requested a review from Mpdreamz March 18, 2025 12:56
@cotti cotti merged commit 99b12f8 into main Mar 18, 2025
10 checks passed
@cotti cotti deleted the feature/font_preload branch March 18, 2025 14:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature Request]: Preload fonts
3 participants