Skip to content
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

Check for empty regions directly in twig file for better performance #64

Closed
laurent-b4y opened this issue Sep 2, 2022 · 14 comments · Fixed by #120 or #124
Closed

Check for empty regions directly in twig file for better performance #64

laurent-b4y opened this issue Sep 2, 2022 · 14 comments · Fixed by #120 or #124
Labels
bug Something isn't working

Comments

@laurent-b4y
Copy link

Yves brought my attention on a possible performance hit Kiso has by checking the regions content in a preprocess iso directly in twig.

We should remove the preprocess used to check the "has_navigation" and "has_complementary" and replace them with something like this in the page.html.twig file

{% set page_navigaton = page.navigation|render %}
{% set page_complementary = page.complementary|render %}

{% if page_navigation|striptags|trim is not empty %}
	{% set has_navigation = true %}
{% else %}
	{% set has_navigation = false %}
{% endif %}

{% if page_complementary|striptags|trim is not empty %}
	{% set has_complementary = true %}
{% else %}
	{% set has_complementary = false %}
{% endif %}

The rest of the file doesn't need changes AFAIK.

@GyD
Copy link

GyD commented Sep 2, 2022

To give more detail <?php render function do not return the final output.
That could lead to false result, example: when a drupal placeholder return nothing.

This can be tested with facets module and facet option that do not render the facets when the facet source is not rendered.

Laurent solution is the one we put on werkenvoor but may not be the best since this issue need investigation.

Example, with facet we currently have this output in php on page where facet are not rendered:

'  <drupal-render-placeholder callback="Drupal\\block\\BlockViewBuilder::lazyBuilder" arguments="0=category&amp;1=full&amp;2" token="3H-ATSLp_IKUKYnhOjRX3yjcYQjgLT6WOepB79hscJI"></drupal-render-placeholder>
'

But when you render in twig it return

''

Please note that solution implemented in Kiso also render region and their content multiples times (at least twice), that could lead to performances issues.

@smillart smillart assigned smillart and unassigned smillart Sep 2, 2022
@smillart
Copy link
Collaborator

smillart commented Sep 2, 2022

Please investigate and provide me with the best solution or better than what we have now. In that case, I will add this issue in the Kiso (基礎) 2.7.3 milestone and I will implement this change request.

@smillart smillart self-assigned this Sep 2, 2022
@smillart smillart added the help wanted Extra attention is needed label Sep 2, 2022
@rutiolma
Copy link
Collaborator

Kiso should skip drupal generated HTML tags, like drupal-render-placeholder or drupal-entity (ideally, a rule to skip tags starting with "drupal-" would be the ideal). This would avoid false results as mentioned by GyD

@smillart
Copy link
Collaborator

@GyD @rutiolma I'm not quite sure exactly what I'm supposed to do. Should I delete the following PHP code and only use twig templates to test for empty regions?

// Add boolean variables detecting if regions are empty.
$theme = \Drupal::theme()->getActiveTheme()->getName();
$regions = system_region_list($theme);
foreach ($regions as $key => $value) {
$variables['has_' . $key] = _kiso_has_region(\Drupal::service('renderer')->render($variables['page'][$key]));
}

function _kiso_has_region(?string $markup, string $allowed_tags = '') {
$non_conditional_html_comments_pattern = '/<!--(.|\s)*?-->\s*|\r|\n/';
$cleaned_region_output = preg_replace($non_conditional_html_comments_pattern, '', $markup);
return !empty(_kiso_strip_tags($cleaned_region_output, $allowed_tags));
}
/**
* Strips html tags, except allowed, returning a trimmed clean markup.
*/
function _kiso_strip_tags(string $markup, string $allowed_tags = '') {
$allowed_tags .= '<drupal-render-placeholder><img>';
return trim(strip_tags($markup, $allowed_tags));
}

Could you please provide me with code example to implement in the twig template?

@smillart smillart added this to the Kiso (基礎) 3.0.3 milestone Jan 30, 2024
@smillart smillart removed their assignment Feb 2, 2024
@lramarojaona
Copy link

lramarojaona commented Mar 21, 2024

@smillart ,
I implemented the suggested way and removed related PHP code.

I did not add the strip_tags, I guess we'll need to add it somehow to prevent breaking existing sites.

So this patch is WIP.

kiso-64.patch

FYI, without this, my region is not outputed. Using Openfed 12.1.1

@lramarojaona
Copy link

lramarojaona commented Mar 21, 2024

There is a small typo in previous patch with page_foote which should be page_footer.

Here's the updated patch.

kiso-64-2.patch

@GyD
Copy link

GyD commented Mar 22, 2024

@lramarojaona I confirm you need to add |striptags('<drupal-render-placeholder><img><video><[...]>')|trim in order to have it work. Otherwise you end up with empty region displayed.

Maybe using https://www.drupal.org/project/twig_real_content could be a good idea if bosa agree!?

@lramarojaona
Copy link

Ok, maybe we should create custom twig functions or filters for that: https://drupaljournal.com/article/drupal/creating-custom-twig-functions-and-filters-drupal

@smillart smillart added bug Something isn't working and removed help wanted Extra attention is needed labels Jul 4, 2024
jclemmenb4y added a commit that referenced this issue Jul 4, 2024
Bugfix/#64: Replace old implementation to check empty regions with contrib module…
@rutiolma
Copy link
Collaborator

rutiolma commented Jul 4, 2024

I don't fully agree with the solution. Removing this twig function will require an update to hundreds of sites, which is not really super simple.
Why can't we keep the old function and adapt it to use real content?! Wouldn't that be more efficient?

@lramarojaona
Copy link

@rutiolma , good point, but it should be somehow marked as deprecated.

@rutiolma
Copy link
Collaborator

rutiolma commented Jul 5, 2024

@rutiolma , good point, but it should be somehow marked as deprecated.

IMHO we could keep the "has_region" function in Kiso and use it as we've been doing. It's way more clean to check for an empty region.
This
{% if has_tools %}
vs
{% if page.tools|render is real_content %}

If we mark it as deprecated, we should remove it for version 4.0.x and not before otherwise all existing projects will have to change their themes due to a minor release.

@jclemmenb4y
Copy link
Collaborator

jclemmenb4y commented Jul 5, 2024

I seems to be a safer approach for now.

I know I'm overriding the has_region function in almost all my themes as it was not working properly (especially for regions with content such as facets).

But does this approach solve anything performance wise? As this was the initial concern. @GyD what do you think?

@GyD
Copy link

GyD commented Jul 5, 2024

We should be able to create a has_{region} using php classes from twig_real_content within _kiso_has_region function. I’ll investigate that.

edit : that’s what @rutiolma is doing in Feature/#64 in fact.

@ninjadrupal
Copy link

To give more detail <?php render function do not return the final output. That could lead to false result, example: when a drupal placeholder return nothing.

This can be tested with facets module and facet option that do not render the facets when the facet source is not rendered.

Laurent solution is the one we put on werkenvoor but may not be the best since this issue need investigation.

Example, with facet we currently have this output in php on page where facet are not rendered:

'  <drupal-render-placeholder callback="Drupal\\block\\BlockViewBuilder::lazyBuilder" arguments="0=category&amp;1=full&amp;2" token="3H-ATSLp_IKUKYnhOjRX3yjcYQjgLT6WOepB79hscJI"></drupal-render-placeholder>
'

But when you render in twig it return

''

Please note that solution implemented in Kiso also render region and their content multiples times (at least twice), that could lead to performances issues.

i have the same issue in one of the sites :/ after the upgrade to openfed 12

@smillart smillart linked a pull request Jul 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
7 participants