Feature: #101388 - Set the canonical URL from uncached plugins
See forge#101388
Description
The canonical URL generated by EXT:seo is now managed by the Meta Tag API.
A new meta tag manager handles the property
canonical
and renders it
as
<link rel="canonical" href="…">
.
The canonical URL is still determined while the cacheable part of the page
is generated, and the PSR-14 event
\TYPO3\ is still
dispatched. The resulting URL is stored together with the other meta tags in
the page cache and rendered after uncached content objects
(
USER_,
COA_, uncached Extbase actions)
have been processed.
Uncached plugins can therefore replace the canonical URL:
use TYPO3\CMS\Core\MetaTag\MetaTagManagerRegistry;
public function __construct(
private readonly MetaTagManagerRegistry $metaTagManagerRegistry,
) {}
public function filterAction(): ResponseInterface
{
$this->metaTagManagerRegistry
->getManagerForProperty('canonical')
->addProperty('canonical', 'https://example.com/products/');
// ...
}
A page has a single canonical URL, so adding the property always replaces the previous value. Removing the property suppresses the tag:
$this->metaTagManagerRegistry
->getManagerForProperty('canonical')
->removeProperty('canonical');
Impact
The canonical URL can be set, replaced and removed from cached and uncached plugins through the Meta Tag API, consistently on the first request and when the page is served from the page cache.
The
<link rel="canonical">
tag is now rendered together with the
meta tags instead of the additional header data, which changes its position
within the
<head>
section.