Preload JavaScript files [not working]
-
Hi,
I found out that the preload feature does not work, because you are not setting crossorigin
Error from console:
(index):524 A preload for ‘xxxxxxxx’ is found, but is not used because the request credentials mode does not match. Consider taking a look at crossorigin attribute.
I fixed it on my site using simple snippet but it would be great if you could fix this on your end.
<?php
add_action('init', function () {
ob_start('fix_all_crossorigin_tags');
});
function fix_all_crossorigin_tags($html) {
// Fix <link rel="preload">
$html = preg_replace_callback(
'#<link\s+[^>]*rel=["\']preload["\'][^>]*>#i',
function ($matches) {
$tag = $matches[0];
if (stripos($tag, 'crossorigin') === false) {
$tag = preg_replace('/\/?>$/', ' crossorigin="anonymous">', $tag);
}
return $tag;
},
$html
);
// Fix <script src="...">
$html = preg_replace_callback(
'#<script\s+[^>]*src=["\'][^"\']+["\'][^>]*>#i',
function ($matches) {
$tag = $matches[0];
if (stripos($tag, 'crossorigin') === false) {
$tag = preg_replace('/<script\s+/i', '<script crossorigin="anonymous" ', $tag);
}
return $tag;
},
$html
);
return $html;
}
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
The topic ‘Preload JavaScript files [not working]’ is closed to new replies.