HtmlResponseAttachmentsProcessor::processHtmlHeadLink does not consider other attributes

Created on 20 June 2023, over 1 year ago

Problem/Motivation

I was attempting to follow the advice in https://web.dev/preconnect-and-dns-prefetch/ by providing some preconnect and preload link tags for some external web fonts our theme uses. However it seems that processHtmlHeadLink ignores additional attributes for uniqueness, and only considers the 'alternate' attribute.

Steps to reproduce

I used hook_page_attachements():

/**
 * Implements hook_page_attachments_alter().
 */
function custom_page_attachments_alter(&$attachments): void {
  $attachments['#attached']['html_head_link'][] = [
    [
      'rel' => 'dns-prefetch',
      'href' => 'https://www.example.com',
    ],
  ];
  $attachments['#attached']['html_head_link'][] = [
    [
      'rel' => 'preconnect',
      'href' => 'https://www.example.com',
    ],
  ];
  $attachments['#attached']['html_head_link'][] = [
    [
      'rel' => 'preconnect',
      'href' => 'https://www.example.com',
      'crossorigin' => TRUE,
    ],
  ];
  $attachments['#attached']['html_head_link'][] = [
    [
      'rel' => 'preload',
      'href' => 'https://www.example.com/my-web-font.woff',
      'as' => 'font',
      'type' => 'font/woff2',
      'crossorigin' => TRUE,
    ],
  ];
}

Results in:

<link rel="dns-prefetch" href="https://www.example.com" />
<link rel="preconnect" href="https://www.example.com" crossorigin /> <-- The second of the two links "won"
<link rel="preload" href="https://www.example.com/my-web-font.woff" as="font" type="font/woff2" crossorigin />

Expected result:

<link rel="dns-prefetch" href="https://www.example.com" />
<link rel="preconnect" href="https://www.example.com" />
<link rel="preconnect" href="https://www.example.com" crossorigin />
<link rel="preload" href="https://www.example.com/my-web-font.woff" as="font" type="font/woff2" crossorigin />

Proposed resolution

Can we just serialize or hash the additional attributes to ensure this won't happen to anything else?

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

πŸ› Bug report
Status

Active

Version

11.0 πŸ”₯

Component
RenderΒ  β†’

Last updated about 9 hours ago

Created by

πŸ‡ΊπŸ‡ΈUnited States dave reid Nebraska USA

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024