How to add rel=“preconnect” to tags other than link?

≡放荡痞女 提交于 2019-12-12 11:48:42

问题


I am using Chrome Dev Tools to audit my site's homepage. And it gives one of the opportunities "Preconnect to required origins" for the facebook, twitter and linkedin share button in my homepage.

I read the google article about preconnect and dns-prefetch at https://developers.google.com/web/fundamentals/performance/resource-prioritization , but find the syntax is only for link tag, as below:

But in my home page, the share buttons that will connect to the social sites are looks like this:

<div class="fb-like" data-href="https://www.facebook.com/xxxx" data- 
send="false" data-layout="button_count" data-width="90" data-show- 
faces="true"></div>

or this:

<a href="https://twitter.com/share" class="twitter-share-button" data- 
 count="none">Tweet</a>

or this:

<script src="https://platform.linkedin.com/in.js" type="text/javascript"> 
</script>
                        <script type="IN/Share"></script>

In such cases, how to add the preconnect or dns-prefetch hints to the html code? It seems that these hints are only valid for link tag?

Thanks


回答1:


I'm assuming that you're auditing your page with the Audits panel and you're looking at the Preload key requests audit.

If you click the Preload key requests audit, you should see an expanded table with the exact URLs that it is suggesting you preload.

Take these URLs and add <link> tags to the <head> of your HTML that preload these requests.

<!doctype html>
<html>
  <head>
    <link rel="preload" as="script" href="facebook.js">
    <link rel="preload" as="script" href="linkedin.js">

You need to pay attention to the file types of each resource and update the as property accordingly. If the file is a stylesheet, you need to set as="style". If it's a script you need to set as="script".



来源:https://stackoverflow.com/questions/54900054/how-to-add-rel-preconnect-to-tags-other-than-link

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!