[wp-trac] [WordPress Trac] #52465: Allow a relation type in resource hints to be used multiple times
WordPress Trac
noreply at wordpress.org
Sun Feb 7 17:37:28 UTC 2021
#52465: Allow a relation type in resource hints to be used multiple times
---------------------------+-----------------------------
Reporter: GeekPress | Owner: (none)
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Script Loader | Version:
Severity: normal | Keywords:
Focuses: |
---------------------------+-----------------------------
`wp_resource_hints` function restricts to one time the usage of a relation
type for a resource hint.
This can be a problem for `preconnect` which can be used with and without
`crossorigin` attribute at the same time for performance reason.
As an example, a CDN should have two preconnect like this:
{{{
<link href='//mycdn.test' rel='preconnect' />
<link href='//mycdn.test' rel='preconnect' crossorigin />
}}}
Please check this post as reference:
[https://crenshaw.dev/preconnect-resource-hint-crossorigin-
attribute/#:~:text=Summary,be%20downloaded%20from%20that%20origin.&text=If%20CORS%20won't%20be,two%20resource%20hints%20are%20necessary]
I attached a patch which will allow to use multiple times a resource with
different configurations. This patch also avoid a resource to be printed
multiple times if all attrs are the same.
e.g:
{{{#!php
<?php
function preconnect_mycdn( $hints, $relation_type ) {
$domains = [
[
'href' => '//mycdn.test/',
],
[
'href' => '//mycdn.test/',
'crossorigin' => 'use-credentials',
],
[
'href' => '//mycdn.test/',
],
[
'href' => '//mycdn.test/',
'crossorigin' => 'anonymous',
],
];
foreach ( $domains as $domain ) {
if ( 'preconnect' === $relation_type ) {
$hints[] = $domain;
}
}
return $hints;
}
add_filter( 'wp_resource_hints', 'preconnect_mycdn', 10, 2 );
}}}
will print:
{{{
<link href='//mycdn.test' rel='preconnect' />
<link href='//mycdn.test' crossorigin='anonymous' rel='preconnect' />
}}}
--
Ticket URL: <https://core.trac.wordpress.org/ticket/52465>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list