The code executed on each block render (thus on each page load) does impact the site performance negatively.

If we cache the block based on the path, we improve the performance immensely.

Comments

lolandese created an issue. See original summary.

lolandese’s picture

Status: Active » Needs review
StatusFileSize
new749 bytes

Appears to be as simple as defining a cache context in the render array. See Cache contexts | Drupal 8 guide on Drupal.org.

    // Cache the block based on the (alias) path.
    $build['#cache'] = ['contexts' => ['url.path']];
lolandese’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new48.06 KB
new51.71 KB
new56.29 KB

Patch applies cleanly

martin@martin-XPS-13-9370 /var/www/html/brown.localhost/web/modules/contrib/permalink_block (8.x-1.x *=) $ git apply -v permalink_block-cache_per_path-3133256-3.patch
Checking patch src/Plugin/Block/PermaLinkBlock.php...
Applied patch src/Plugin/Block/PermaLinkBlock.php cleanly.

Module enables successfully

martin@martin-XPS-13-9370 /var/www/html/brown.localhost/web/modules/contrib/permalink_block (8.x-1.x *=) $ drush en permalink_block
 [success] Successfully enabled: permalink_block
 [notice] Message: All non-admin pages now contain a permalink. Change the settings at Structure 
 > Block Layout > Permalink [1].

[1] http://brown.localhost/admin/structure/block/manage/permalink_block

Module uninstalls successfully

martin@martin-XPS-13-9370 /var/www/html/brown.localhost/web/modules/contrib/permalink_block (8.x-1.x *=) $ drush pmu permalink_block
 [success] Successfully uninstalled: permalink_block

PHPUnit tests pass

See above.
8.x-1.x: PHP 7.3 & MySQL 5.7, D8.8.5 2 pass
Test if the site still works, the Permalink block can be placed and works.

Manual functional testing

We change the title with 'Generate automatic URL alias' being unselected. That means the path (alias) stays unaltered. This should show the permalink block with the old title being used, thus showing the cached version of the block.

Screenshot

Screenshot

We now change the title again but this time with 'Generate automatic URL alias' being selected. That means the path (alias) alters. This should show the permalink block with the new title being used, thus showing the refreshed version of the block.

Screenshot

We did the same with the translated version of the node, just to be sure, and confirm the same expected behavior.

  • lolandese committed 13968d5 on 8.x-1.x
    Issue #3133256 by lolandese: Implement block caching
    
lolandese’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.