首页 > 解决方案 > Symfony 的 WebLink 组件:如何在 twig 中正确集成 dns_prefetch 和预连接

问题描述

我正在尝试通过集成 Symfony WebLink 组件来提高我的站点性能。

https://symfony.com/doc/current/web_link.html

我现在已经对我的 css ans js 文件使用了 preload() 函数,并且性能确实得到了提高,对此我感到非常高兴。

但是我在任何地方都找不到dns_prefetch()和preconnect( )函数如何工作的任何树枝示例。

在集成 WebLink 之前是这样的:

<link rel="dns-prefetch" href="https://www.googletagmanager.com" />
<link rel="dns-prefetch" href="https://www.googleadservices.com" />
<link rel="dns-prefetch" href="https://cdn.scarabresearch.com" />
<link rel="dns-prefetch" href="https://www.google.be" />
<link rel="dns-prefetch" href="https://www.google.co.uk" />
<link rel="dns-prefetch" href="https://gum.criteo.com" />

集成 WebLink 组件后,这就是我现在所拥有的。

{#
 # Use preconnect when you are sure it will be used every time (DNS + TCP + TLS)
 #}
<link rel="preconnect" href="{{ preconnect('https://www.googletagmanager.com') }}" />
<link rel="preconnect" href="{{ preconnect('https://www.googleadservices.com') }}" />
<link rel="preconnect" href="{{ preconnect('https://cdn.scarabresearch.com') }}" />

{#
 # Use dns_prefetch if you are NOT sure it will be used every time (DNS lookup only)
 #}
<link rel="dns-prefetch" href="{{ dns_prefetch('https://www.google.be') }}" />
<link rel="dns-prefetch" href="{{ dns_prefetch('https://www.google.co.uk') }}" />
<link rel="dns-prefetch" href="{{ dns_prefetch('https://gum.criteo.com') }}" />

所以问题很简单:
这是 Twig 中的正确表示法吗?

我不想预连接两次(如果可能的话?)

标签: symfonytwig

解决方案


推荐阅读