Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
Problem/Motivation
We should support fonts preloading:
- https://www.freecodecamp.org/news/web-fonts-in-2018-f191a48367e8/#preloa...
- https://web.dev/preload-optional-fonts/
Proposed resolution
Add custom preload-fonts section in theme info file with a list of font paths that we want to preload.
Comment | File | Size | Author |
---|---|---|---|
#11 | add-fonts-preloading-support-3247543-11.patch | 2.53 KB | pivica |
#10 | interdiff-3247543-9-10.txt | 1.27 KB | pivica |
Comments
Comment #2
pivica CreditAttribution: pivica at MD Systems GmbH commentedHere is a first patch.
Documentation added in https://www.drupal.org/docs/8/themes/bs-base/additional-features/fonts-p....
Comment #3
Berdir$res seems a strange variable name for that, maybe $active_theme?
Overall looks good. You could also do this with a regex, less fiddling around with string functions and +1/-1 stuff:
https://www.phpliveregex.com/p/CB2
So that would be:
Also, I think you want to check that the first character is a /, if not, then we could support it being in the active theme, so you can specify just "font/myfont.woff2", so I'd add an elseif for that.
again, not a big deail, you could also use pathinfo() for this: pathinfo($font, PATHINFO_EXTENSION)
I think the second argument is expected to be a machine name, so I'd use something like 'bs_bse_preload_font'.
it's a weird syntax and IIRC meant to be used to identify certain things to alter them.
Comment #4
pivica CreditAttribution: pivica at MD Systems GmbH commented@Berdid thank you for the feedback.
1. to 3. make sense, i've implemented them.
> 4. I think the second argument is expected to be a machine name, so I'd use something like 'bs_bse_preload_font'.
I can't really find documentation about this in internet. However checking the source code of core `HtmlResponseAttachmentsProcessor::processHtmlHeadLink` and the comment of this method explains this differently:
Note the part:
So they are only specifying second argument to be of a type boolean and not a string (for example a machine name). And it is used to add this tags to response headers. I don't think this makes sense to do this for fonts preload links, tried with setting boolean to FALSE and it works fine.
Digging this a bit more I see that Drupal is emitting next thing in response headers:
This look weird to me, maybe somewhere in core the same canonical and shortlink links are added in the same way with second argument set to TRUE instead of FALSE? Can't really tell is this a bug or not, opinions?
Comment #5
BerdirThinking more about this, I think you want to use something like
file_url_transform_relative(file_create_url(drupal_get_path('module', 'ckeditor_test') . '/ckeditor_test.css'))
. file_create_url() has hooks that support things like separate domains for assets.Found this while I was researching the header links, and crossorigin is technicaly only required if the font is from a different domain, but if we combine it with file_create_url() then that might happen on some sites, so that makes sense and doesn't hurt.
Comment #6
pivica CreditAttribution: pivica at MD Systems GmbH commented> Thinking more about this, I think you want to use...
Make sense, added this function for both cases.
Comment #7
pivica CreditAttribution: pivica at MD Systems GmbH commentedOps, wrong patch in last comment with one error, fixed now.
Comment #8
Berdirnote that one case is left now that doesn't go through file_create_url(), which would be something like '/libraries/something/....woff2'.
One option would be to rename $href to $uri, then you could change this if/elseif to if/elseif/else and the else would then basically to a $uri = ltrim($font, '/'), if and elseif is just $uri = drupal_get_path(...
and then you can do a single $href = file_url_transform_relative(file_create_url($uri)) below, for all cases.
Comment #9
pivica CreditAttribution: pivica at MD Systems GmbH commentedYeah you are right, i forgot the last case. Added it now.
> and the else would then basically to a $uri = ltrim($font, '/'),
This does not work well, if we remove first '/' the file_url_transform_relative() will not get it back so the path will be invalid. I've dropped ltrim call.
Came to my mind that we should also support remote urls (google fonts, etc). Added support for it and also made if/elseif/...else a bit more formal. Changes needs a new review.
Comment #10
pivica CreditAttribution: pivica at MD Systems GmbH commentedMoved file_create_url call to file_url_transform_relative.
Comment #11
pivica CreditAttribution: pivica at MD Systems GmbH commentedAdded comment about 'Functions file_create_url() and file_url_transform_relative() are deprecated', see [#2940031].
Comment #13
pivica CreditAttribution: pivica at MD Systems GmbH commentedCommitted.