Drupal core sets all link elements as HTTP headers, too. For example, if a page has the following in the element:

<link rel="canonical" href="/foo">
<link rel="shortlink" href="/node/1">

Then it might also have the following in the HTTP response headers:

Link: </foo>; rel="canonical", </node/1>; rel="shortlink"

This is done in `HtmlResponseAttachmentsProcessor.php`: http://cgit.drupalcode.org/drupal/tree/core/lib/Drupal/Core/Render/HtmlR... . That code takes every `html_head_link` item and adds it to _both_ the HTML head and the HTTP header.

But in Metatags, the 'canonical_url' and 'shortlink' items are removed from `$build['#attached']['html_head_link']` if they are set for nodes (see metatag_entity_view_alter()). It adds them to to the HTML head using its own methods I guess, and it doesn't set the HTTP header at the same time.

I think that Metatag should add these HTTP response header links just like core does.

Comments

jeffam created an issue. See original summary.

jeffam’s picture

StatusFileSize
new1.19 KB

Here's a patch that steals borrows some ideas from core to add HTTP Link headers for Canonical URL and/or Shortlinks set via Metatag.

jeffam’s picture

Issue summary: View changes
benstallings’s picture

The above patch appears to be incompatible with Drupal 8.4.

damienmckenna’s picture

Status: Active » Needs review

Status: Needs review » Needs work

The last submitted patch, 2: set_link_http_header-2908119-2.patch, failed testing. View results

benstallings’s picture

StatusFileSize
new1.22 KB

Here's an updated patch.

damienmckenna’s picture

Status: Needs work » Needs review

Thanks, lets see what the testbot says.

lealex’s picture

StatusFileSize
new1.35 KB

minor bugfix

Status: Needs review » Needs work

The last submitted patch, 9: set_link_http_header.patch, failed testing. View results

lealex’s picture

StatusFileSize
new1.35 KB
damienmckenna’s picture

Status: Needs work » Needs review
StatusFileSize
new1.46 KB

Rerolled, and a minor tweak.

damienmckenna’s picture

I wonder, though, should *all* link tags be added to the header? I guess we can deal with that later.

damienmckenna’s picture

Title: Also set `Link` HTTP Header » Set "link" HTTP header for canonical URL and shortlink tags

  • DamienMcKenna committed cab68db on 8.x-1.x authored by jeffam
    Issue #2908119 by LEalex, DamienMcKenna, jeffam, BenStallings: Set "link...
damienmckenna’s picture

Status: Needs review » Fixed

Committed. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.