为什么preload链接不适用于JSON请求? [英] Why are preload link not working for JSON requests ?
问题描述
我网站上的JavaScript加载了几个JSON来初始化自己。
The JavaScript on my website loads several JSON to initialize itself.
我想预加载它们,所以当JavaScript在它上面启动Ajax请求时,它们会将立即加载。
I would like to preload them so, when the JavaScript will launch an Ajax request on it, they will be loaded instantaneously.
一个新的 链接
标签。
A new link
tag exists for that.
我试图用它来加载类似的JSON:
I tried to use it to load a JSON like that :
<link rel="preload" href="/test.json">
但是,Chrome似乎加载了两次并在控制台中显示警告:
However, Chrome seems to load it twice and present a warning in the console :
资源test.json是使用链接预加载预加载的,但是在窗口加载事件的几秒钟内没有使用。请确保它没有预先加载。
所以看起来preload对JSON不起作用。
确实,我没有在规范<中找到对JSON的引用/ a>。
So it seems that preload doesn’t work for JSON. Indeed, I haven’t found reference to JSON in the specification.
这是正确的还是我做错了?
Is that correct or am I doing it wrong ?
推荐答案
根据 https://developer.mozilla.org/ en-US / docs / Web / HTML / Preloading_content ,您必须为JSON文件添加 as =prefetch
。
所以你的代码变成
According to https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content , you have to add as="prefetch"
for JSON files.
So your code becomes
< link rel =preloadhref =/ test.jsonas =fetch >
所有现代浏览器都支持它,如果在几秒钟内没有使用此资源,则会收到警告消息,因为它会适得其反在这种情况下预加载它(延迟,双重加载等)。
It's supported by all modern browsers and you get a warning message if this resource is not used within a few seconds because it is counterproductive to "preload" it in a such case (delay, double load etc.)
它与< link rel =prefetch不同。 ..>
这是为了预测未来的导航并且不被广泛支持。
It's different from <link rel="prefetch" ...>
which is to anticipate future navigation and not supported widely.
关于此的Chrome插图文章: https://medium.com/reloading/preload-prefetch-and-priorities-in- chrome-776165961bbf
A Chrome illustrated article about this: https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf
这篇关于为什么preload链接不适用于JSON请求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!