ASP.NET Core 的“asp-fallback-*"是如何实现的CDN 标签助手有用吗? [英] How do ASP.NET Core's "asp-fallback-*" CDN tag helpers work?
问题描述
我了解 asp-fallback-*
标签助手的作用.我不明白的是如何.
I understand what the asp-fallback-*
tag helpers do. What I don't understand is how.
例如:
<link rel="stylesheet"
href="//ajax.aspnetcdn.com/ajax/bootstrap/3.3.5/css/bootstrap.min.css"
asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css"
asp-fallback-test-class="sr-only"
asp-fallback-test-property="position"
asp-fallback-test-value="absolute" />
这会从 CDN 加载引导程序,并在 CDN 关闭时加载本地副本.
This loads bootstrap from the CDN, and loads the local copy if the CDN is down.
但是它是如何决定这样做的呢?我假设它检查 asp-fallback-test-class
、asp-fallback-test-property
和 asp-fallback-test-value
.但这些属性是什么意思?
But how does it decide to do that? I assume it checks asp-fallback-test-class
, asp-fallback-test-property
, and asp-fallback-test-value
. But what do those attributes mean?
如果我想从 CDN 连接其他一些库,我需要为它们提供一些东西,但我不知道该放什么.
If I want to hook up some other library off a CDN, I'll need to supply something for those, but I'm not sure what to put there.
有很多这样的例子,但我找不到关于它是如何工作的解释.
There are lots of examples of this in action, but I can't find explanations about how this works.
更新
我并不是真的想了解标签助手是如何工作的——它们是如何呈现的,等等.我试图了解如何为这些属性选择值.例如,jQuery 回退脚本通常具有 asp-fallback-test="window.jQuery"
这很有意义 - 这是一个测试,看看 jQuery 是否已加载.但是我上面展示的那些是完全不同的.人们如何选择它们?如果我想使用其他一些 CDN 交付的库,我需要为这些属性指定值……我会使用什么?为什么选择这些作为引导程序?
UPDATE
I'm not really trying to understand how the tag helpers work - how they render, and so on. I'm trying to understand how to choose values for those attributes. For example, the jQuery fallback script usually has asp-fallback-test="window.jQuery"
which makes sense - it's a test to see if jQuery has loaded. But the ones I've shown above are quite different. How does one choose them? If I want to use some other CDN delivered library, I'll need to specify values for those attributes... what would I use? Why were those ones chosen for bootstrap?
更新 2
要了解回退过程本身的工作原理以及这些标签的编写方式,请参阅@KirkLarkin 的回答.要了解为什么使用这些测试值,请参阅我的回答.
UPDATE 2
To understand how the fallback process itself works, and how those tags are written, see @KirkLarkin's answer. To understand why those test values were used, see my answer.
推荐答案
TL;DR:
- 一个
标签被添加到具有
sr-only
的 CSS 类的 DOM 中. - 额外的 JavaScript 被写入 DOM,其中:
- A
<meta>
tag is added to the DOM that has a CSS class ofsr-only
. - Additional JavaScript is written to the DOM, which:
- 定位所述
元素.
- 检查所述元素是否具有设置为
absolute
的 CSS 属性position
. - 如果没有设置这样的属性值,一个额外的
元素被写入到 DOM 中,
href
为~/lib/bootstrap/dist/css/bootstrap.min.css
.
- Locates said
<meta>
element. - Checks whether said element has a CSS property
position
that is set toabsolute
. - If no such property value is set, an additional
<link>
element is written to the DOM with ahref
of~/lib/bootstrap/dist/css/bootstrap.min.css
.
针对您的
类在给定 CSS 类的输出 HTML 中插入一个 元素运行的 LinkTagHelper
元素
sr-only
.元素最终看起来像这样:
The LinkTagHelper
class that runs against your <link>
elements inserts a <meta>
element in the output HTML that is given a CSS class of sr-only
. The element ends up looking like this:
<meta name="x-stylesheet-fallback-test" content="" class="sr-only" />
生成元素的代码如下(来源):
The code that generates the element looks like this (source):
builder
.AppendHtml("<meta name="x-stylesheet-fallback-test" content="" class="")
.Append(FallbackTestClass)
.AppendHtml("" />");
不出所料,FallbackTestClass
的值是从 的
asp-fallback-test-class
属性获得的.
Unsurprisingly, the value for FallbackTestClass
is obtained from the <link>
's asp-fallback-test-class
attribute.
在插入这个元素之后,相应的 块也被插入(source).代码是这样开始的:
Right after this element is inserted, a corresponding <script>
block is also inserted (source). The code for that starts off like this:
// Build the <script /> tag that checks the effective style of <meta /> tag above and renders the extra
// <link /> tag to load the fallback stylesheet if the test CSS property value is found to be false,
// indicating that the primary stylesheet failed to load.
// GetEmbeddedJavaScript returns JavaScript to which we add '"{0}","{1}",{2});'
builder
.AppendHtml("<script>")
.AppendHtml(JavaScriptResources.GetEmbeddedJavaScript(FallbackJavaScriptResourceName))
.AppendHtml(""")
.AppendHtml(JavaScriptEncoder.Encode(FallbackTestProperty))
.AppendHtml("","")
.AppendHtml(JavaScriptEncoder.Encode(FallbackTestValue))
.AppendHtml("",");
这里有一些有趣的事情:
There are a few things of interest here:
- 注释的最后一行,指的是占位符
{0}
、{1}
和{2}
. FallbackJavaScriptResourceName
,表示输出到 HTML 中的 JavaScript 资源.FallbackTestProperty
和FallbackTestValue
,从属性asp-fallback-test-property
和asp-fallback-test 中获得-value
分别.
- The last line of the comment, which refers to placeholders
{0}
,{1}
and{2}
. FallbackJavaScriptResourceName
, which represents a JavaScript resource that is output into the HTML.FallbackTestProperty
andFallbackTestValue
, which are obtained from the attributesasp-fallback-test-property
andasp-fallback-test-value
respectively.
那么,让我们看看那个 JavaScript 资源(source),归结为具有以下签名的函数:
So, let's have a look at that JavaScript resource (source), which boils down to a function with the following signature:
function loadFallbackStylesheet(cssTestPropertyName, cssTestPropertyValue, fallbackHrefs, extraAttributes)
结合我之前提到的最后一行注释以及 asp-fallback-test-property
和 asp-fallback-test-value
的值,我们可以推断这是这样调用的:
Combining this with the last line of the comment I called out earlier and the values of asp-fallback-test-property
and asp-fallback-test-value
, we can reason that this is invoked like so:
loadFallbackStylesheet('position', 'absolute', ...)
我不会深入研究 fallbackHrefs
和 extraAttributes
参数,因为这些参数应该很明显并且易于您自己探索.
I won't dig into the fallbackHrefs
and extraAttributes
parameters as that should be somewhat obvious and easy to explore on your own.
loadFallbackStylesheet
的实现并没有多大作用 - 我鼓励您自己探索完整的实现.这是来自源头的实际检查:
The implementation of loadFallbackStylesheet
does not do a great deal - I encourage you to explore the full implementation on your own. Here's the actual check from the source:
if (metaStyle && metaStyle[cssTestPropertyName] !== cssTestPropertyValue) {
for (i = 0; i < fallbackHrefs.length; i++) {
doc.write('<link href="' + fallbackHrefs[i] + '" ' + extraAttributes + '/>');
}
}
脚本获取相关的 元素(假设它直接位于
本身之上)并简单地检查它是否具有以下属性
position
设置为 absolute
.如果没有,额外的 元素会被写入到每个回退 URL 的输出中.
The script obtains the relevant <meta>
element (it's assumed to be directly above the <script>
itself) and simply checks that it has a property of position
that is set to absolute
. If it does not, additional <link>
elements are written to the output for each fallback URL.
这篇关于ASP.NET Core 的“asp-fallback-*"是如何实现的CDN 标签助手有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!