Skip to content
Snippets Groups Projects
Commit f58f55d0 authored by Ivica Puljic's avatar Ivica Puljic
Browse files

Issue #3435997 by pivica: Problem loading font preloads with query cache bust parameter

parent 026db6f0
Branches 2.x
No related tags found
1 merge request!1Issue #3435997 by pivica: Problem loading font preloads with query cache bust parameter
......@@ -139,9 +139,20 @@ function bs_base_preprocess_maintenance_page(&$variables) {
*/
function bs_base_preload_fonts(array $fonts, array &$attachments) {
foreach ($fonts as $font) {
// Font path can have ? query string for cache busting, which we need to
// remove when defining preload type and href.
if (str_contains($font, '?')) {
$parts = explode('?', $font, 2);
$font = $parts[0];
$query = $parts[1];
}
else {
$query = FALSE;
}
// Support relative font path from root like '/libraries/.../....woff2' or
// remote font paths.
if (strpos($font, '/') === 0 || strpos($font, 'http') === 0) {
if (str_starts_with($font, '/') || str_starts_with($font, 'http')) {
$uri = $font;
}
// Support twig expansion syntax like @custom_theme/fonts/font1.woff.
......@@ -149,7 +160,7 @@ function bs_base_preload_fonts(array $fonts, array &$attachments) {
$uri = \Drupal::service('extension.list.theme')->getPath($match[1]) . $match[2];
}
// Support relative font path from the current active theme.
elseif (strpos($font, '/') !== 0) {
elseif (!str_starts_with($font, '/')) {
$active_theme = \Drupal::theme()->getActiveTheme();
$uri = $active_theme->getPath() . '/' . $font;
}
......@@ -161,7 +172,7 @@ function bs_base_preload_fonts(array $fonts, array &$attachments) {
$attachments['#attached']['html_head_link'][] = [[
'rel' => 'preload',
'as' => 'font',
'href' => \Drupal::service('file_url_generator')->generateString($uri),
'href' => \Drupal::service('file_url_generator')->generateString($uri) . ($query ? '?' . $query : ''),
'type' => 'font/' . pathinfo($font, PATHINFO_EXTENSION),
'crossorigin' => 'anonymous',
], FALSE];
......
......@@ -88,11 +88,11 @@ $custom-file-text: () !default;
$bs-icon-use: true !default;
// Use font hash when you need to cache bust previous font.
// If you are using font preloading for icons font do not forget to add the same
// cache in font preloading definition, for example if your hash is '1' then
// for preloading you need to add '?1' like 'fonts/icons.woff2?1'.
// cache in font preloading definition, for example if your hash is '123' then
// for preloading you need to add '?v=123' like 'fonts/icons.woff2?v=123'.
$bs-icon-font-hash: false !default;
// Do not change icons font name or font preloader will fail.
$bs-icon-font-src: url("../../fonts/icons.woff2#{if($bs-icon-font-hash, '?'+$bs-icon-font-hash, '')}") format("woff2") !default;
$bs-icon-font-src: url("../../fonts/icons.woff2#{if($bs-icon-font-hash, '?v='+$bs-icon-font-hash, '')}") format("woff2") !default;
$bs-icon-font-family: 'BS Icons' !default;
$bs-icon-class: 'fa' !default;
// Additional icon shims.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment