<link rel="preload">
动态添加navigator.connection.effectiveType
标签来加载字体文件,例如如果有效类型为'4g',则在<link as="font" type="font/woff2" rel="preload" crossorigin="anonymous" href="inter-var.woff2">
标签中插入head
,然后再插入其他资源,如果连接为'slow-2g'/'2g'/'3g',则不插入link
。 我也在使用preload
加载其他资源,但是它们不如字体重要,因此会在字体文件之后注入。
<head>
// some other code
<script id="connection-type-checker">
(() => {
const inter = document.createElement('link')
const interItalic = document.createElement('link')
const firaCode = document.createElement('link')
inter.as = 'font'
inter.type = 'font/woff2'
inter.rel = 'preload'
inter.crossOrigin = 'anonymous'
inter.href = 'inter-var.woff2'
interItalic.as = 'font'
interItalic.type = 'font/woff2'
interItalic.rel = 'preload'
interItalic.crossOrigin = 'anonymous'
interItalic.href = 'inter-var-italic.woff2'
firaCode.as = 'font'
firaCode.type = 'font/woff2'
firaCode.rel = 'preload'
firaCode.crossOrigin = 'anonymous'
firaCode.href = 'fira-code.woff2'
const insertAfter = (newNode, referenceNode) => referenceNode
.parentNode.insertBefore(newNode, referenceNode.nextSibling)
const target = document.getElementById('connection-type-checker')
insertAfter(inter, target)
insertAfter(interItalic, target)
insertAfter(firaCode, target)
})()
</script>
// **This is where <link>s get injected**
// some other code...
<link as="script" rel="preload" href="script.js" crossorigin="anonymous">
</head>
我面临的问题是,如果link
元素是使用JavaScript创建的,则Chrome不会保留通过链接预加载功能加载的资源的原始顺序(如果链接元素在head
标签中以HTML内联的形式显示,则一切正常如预期的那样。
我试图了解为什么原始顺序在Chrome中中断,并且可以解决吗?
我正在尝试动态添加
标签,以根据用户连接navigator.connection.effectiveType加载字体文件,例如如果有效类型为'4g',则注入&...