diff --git a/app/views/layouts/_head.html.haml b/app/views/layouts/_head.html.haml
index 9253a0652da..ac5916d129c 100644
--- a/app/views/layouts/_head.html.haml
+++ b/app/views/layouts/_head.html.haml
@@ -3,6 +3,11 @@
- site_name = "GitLab"
%head{ prefix: "og: http://ogp.me/ns#" }
%meta{ charset: "utf-8" }
+
+ - if Feature.enabled?('asset_host_prefetch') && ActionController::Base.asset_host
+ %link{ rel: 'dns-prefetch', href: ActionController::Base.asset_host }
+ %link{ rel: 'preconnnect', href: ActionController::Base.asset_host, crossorigin: '' }
+
%meta{ 'http-equiv' => 'X-UA-Compatible', content: 'IE=edge' }
-# Open Graph - http://ogp.me/
diff --git a/changelogs/unreleased/49107-prefetching-of-assets-and-cdn-domain.yml b/changelogs/unreleased/49107-prefetching-of-assets-and-cdn-domain.yml
new file mode 100644
index 00000000000..541b562adac
--- /dev/null
+++ b/changelogs/unreleased/49107-prefetching-of-assets-and-cdn-domain.yml
@@ -0,0 +1,5 @@
+---
+title: DNS prefetching if asset_host for CDN hosting is set
+merge_request: 20781
+author:
+type: performance
diff --git a/spec/views/layouts/_head.html.haml_spec.rb b/spec/views/layouts/_head.html.haml_spec.rb
index e8e6d2e7a75..9d1efcabb80 100644
--- a/spec/views/layouts/_head.html.haml_spec.rb
+++ b/spec/views/layouts/_head.html.haml_spec.rb
@@ -29,6 +29,39 @@ describe 'layouts/_head' do
expect(rendered).to match(%{content="foo" http-equiv="refresh"})
end
+ context 'when an asset_host is set and feature is activated in the config it will' do
+ let(:asset_host) { 'http://assets' }
+
+ before do
+ stub_feature_flags(asset_host_prefetch: true)
+ allow(ActionController::Base).to receive(:asset_host).and_return(asset_host)
+ end
+
+ it 'add a link dns-prefetch tag' do
+ render
+ expect(rendered).to match('')
+ end
+
+ it 'add a link preconnect tag' do
+ render
+ expect(rendered).to match('')
+ end
+ end
+
+ context 'when an asset_host is set and feature is not activated in the config it will' do
+ let(:asset_host) { 'http://assets' }
+
+ before do
+ stub_feature_flags(asset_host_prefetch: false)
+ allow(ActionController::Base).to receive(:asset_host).and_return(asset_host)
+ end
+
+ it 'not add a link dns-prefetch tag' do
+ render
+ expect(rendered).not_to match('')
+ end
+ end
+
def stub_helper_with_safe_string(method)
allow_any_instance_of(PageLayoutHelper).to receive(method)
.and_return(%q{foo" http-equiv="refresh}.html_safe)