I took Adam Tal's suggestion a little further.
I'm probably over-coding it, but for readability, I created a static class to kind of mimic the Styles.Render
format.
public static class StyleExtensions
{
public enum Format
{
Async,
Preload,
}
public static IHtmlString Render(string contentPath, Format format)
{
switch (format)
{
case Format.Async:
return contentPath.ToAsyncFormat();
case Format.Preload:
return contentPath.ToPreloadFormat();
default:
return new HtmlString(string.Empty);
}
}
public static IHtmlString RenderAsync(string contentPath)
{
return contentPath.ToAsyncFormat();
}
public static IHtmlString RenderPreload(string contentPath)
{
return contentPath.ToPreloadFormat();
}
public static IHtmlString ToAsyncFormat(this string contentPath)
{
return Styles.RenderFormat("<link rel=\"stylesheet\" type=\"text/css\" href=\"{0}\" media=\"print\" onload=\"this.media='all';this.onload=null;\">", contentPath);
}
public static IHtmlString ToPreloadFormat(this string contentPath)
{
return Styles.RenderFormat("<link rel=\"preload\" href=\"{0}\" as=\"style\" onload=\"this.rel='stylesheet';this.onload=null;\">", contentPath);
}
}
I would probably erase either the direct constructor or the enum constructor, and you could really put the string extension inside the method too, depending on whatever makes more sense to you, but you'd call it either of these ways accordingly:
@StyleExtensions.Render("~/Content/bundle-bootstrap", StyleExtensions.Format.Preload)
@StyleExtensions.Render("https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;700&display=swap", StyleExtensions.Format.Preload)
@StyleExtensions.Render(Url.Content("~/Content/Styles/Primary.min.css"), StyleExtensions.Format.Async)
or
@StyleExtensions.RenderPreload("~/Content/bundle-bootstrap")
@StyleExtensions.RenderPreload("https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;700&display=swap")
@StyleExtensions.RenderAsync(Url.Content("~/Content/Styles/Primary.min.css"))