我正在尝试根据用户连接的
我还使用
The problem I'm facing is that Chrome doesn't keep the original order for resources loaded with link preload if the
navigator.connection.effectiveType
动态添加<link rel="preload">
标签来加载字体文件。例如,如果有效类型为“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>
The problem I'm facing is that Chrome doesn't keep the original order for resources loaded with link preload if the
link
element was created with JavaScript (if link elements are inlined as HTML in the head
tag everything works as expected).
Screenshot:
- Safari(遵守原始顺序)
- Chrome(不遵守原始顺序)
- Firefox(不支持链接预加载)
我正在尝试理解为什么原始顺序在Chrome中被打乱了,以及是否可以修复?