Option to use pre-compiled Handlebars templates

Created on 15 December 2023, about 1 year ago

Problem/Motivation

In order to achieve faster template rendering, Handlebars provides a way to pre-compile the templates, see
https://handlebarsjs.com/installation/precompilation.html#precompiling-t... and
https://handlebarsjs.com/installation/integrations.html#babel-handlebars...
also see
https://handlebarsjs.com/api-reference/compilation.html#handlebars-compi...

Ps: when using Handlebars, the contents of the templates can be viewed in the Console tab when the user types window.HandlebarsTemplates
It is important that the templates do not contain any sensitive information, including comments.

Proposed resolution

When the config handlebars.settings.precompile is set to false and/or js aggregation is set to false, the templates will be served from raw files (current implementation).
When we set handlebars.settings.precompile to try and turn on js aggregation, we should serve the pre-compiled version of the templates.

What we need now is a way to pre-compile the templates.
As per Handlebars docs, one option is to get the handlebars bin using NPM and manually execute the command. We probably need a Drush command to do this for us.
The Drush command should work without bootstrapping Drupal. The reason for that is that on a multi-site setup we may have multiple Handlebars templates in the codebase.

We may have a template called 'example_menu' on the profile, but we may have a custom version of the template in an optional module. At compilation time, we cannot know if the module is enabled on which site, so we need to make sure that all templates are compiled. During run time, only one version of the 'example_menu' template will be added to the page.

Ideally the pre-compilation should happen before the code is deployed to a production server.

Remaining tasks

- Create a Drush command to pre-compile Handlebars templates
- The Drush command should not require Drupal bootstrap
- It should scan the codebase and pre-compile all Handlebars templates

ps: As an alternative for NPM, there are some PHP packages that could be explored:
- https://github.com/jbboehr/handlebars.php
- https://github.com/jbboehr/php-handlebars

Feature request
Status

Active

Version

1.0

Component

Code

Created by

🇬🇧United Kingdom marcelovani London

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Production build 0.71.5 2024