-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Webfonts: scan content for webfonts and enqueue them #39399
Webfonts: scan content for webfonts and enqueue them #39399
Conversation
* that has been loaded in the front-end. | ||
*/ | ||
public function register_filter_for_current_template_webfonts_enqueuing() { | ||
$template_type_slugs = array_keys( get_default_block_template_types() ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will this work for custom templates?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it will only work for custom templates with the same slugs as those defaults, but I think would miss others that could be created by users or developers (like header-footer-only
for example isnt part of the default list.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if gutenberg_get_block_templates
would work to create a slug list like this from? I think that should be retrieving all templates both from the theme file structure and the database.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, if I got this right, get_default_block_template_types
returns a list of the "slots" where the templates are applied. If this is right, then I think we should be good to go -- we want to hook into the rendered templates and scan them for fonts as lazily as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gutenberg_get_block_templates
retrieves all templates, whether they're rendered or not, and that might be costly because "existing" doesn't mean that the template is actually being used. I think a good idea would be to be as lazy as possible and only cache the fonts for the templates are actively on the page.
/** | ||
* Set up the actions to invalidate webfont cache. | ||
*/ | ||
private function register_webfont_cache_invalidation_actions() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this function, I thought of two approaches:
- Hook into
save_post
and get the $post->post_type to determine the invalidation method (theme mod or meta attribute); - The one I picked. I feel it's easier to understand and it looks to fit more the WP filter/action definitions.
Test failures are not related to the changes proposed by the PR. |
c9ca5cf
to
fc3c8e9
Compare
18819cd
to
f66950b
Compare
be66425
to
7b4900a
Compare
f66950b
to
9440eba
Compare
I just wanted to add something to the conversation here: |
Hey @aristath, For the use-case you described: the user/developer can still programmatically enqueue webfonts at will -- they'll know beforehand which ones they want to preload, so when it's fetched through AJAX, for example, the font-face declaration will be there already. I don't see a problem with that. On a side note, though: the PRs that we're adding are in no way final. They could, and probably will, be iterated upon in the future. This PR is scanning for webfonts used in the default Gutenberg post types, which is about what we can do. It's really hard to give support to a million post types and ways to inject content since we do not know what the users/plugin developers are doing. We're adding a way to:
I think we're headed in a good direction with this right now, and I don't think this is the final form as well. Let's go step by step. First, separate between registering and enqueueing. Second, scan the content we know how to scan and look for fonts. Third, account for any edge cases that might appear once the API is published. Curious to understand what you think about it. |
88941bc
to
f763e10
Compare
9440eba
to
7f7dde5
Compare
f763e10
to
e497a0c
Compare
035093b
to
e1c4ea8
Compare
e497a0c
to
5ab6da7
Compare
e1c4ea8
to
35e6782
Compare
35e6782
to
64cc706
Compare
64cc706
to
dee80c1
Compare
We've since created an alternative approach that we find preferable to this PR. It can be found here. #39559 |
Close in detriment of #39593. |
We've since created an alternative approach that we find preferable to this PR. It can be found here. #39559
Part of #39332. Read #39332 (comment) to gather more context about the whole picture regarding the implementation.
What?
Now that we are not outputting all the registered fonts (#39327), we need to scan the content for actually used fonts and enqueue them. We are placing filters for content, global styles, and templates so we can crawl for webfonts and enqueue them. We are scanning all the post types that stock WordPress exposes through its API.
Why?
Performance. #39332 has more about it.
How?
We're hooking into every possible post type and enqueueing the webfonts that they are using. The webfonts found are then put in a cache (which is wiped on save) so it doesn't slow down the rendering process.
The process is a little different for templates and template parts since they might or might not be a post entry: if the template is pristine, there's no post, and the template is read from disk. That's why we cannot use meta attributes for them. Instead, they use a theme mod so the cache is wiped when the theme is switched.
Testing Instructions
Refresh the page and you should see the custom webfont there. It also works for global styles.
TODO