Skip to content

Move handlebars template and partial compilation to the server #873

@philayres

Description

@philayres

Requirements

Assume that we've already run npm install --global handlebars

In the page code where we currently render the Handlebars templates (possibly ApplicationHelper#handlebars_template_tag), we should :

  • generate a filename prefix using a cache key common to all users relying on the:
    • Application.server_cache_version
    • @item_updates (refactor by extracting this from partial_cache_key method)

For each Handlebars template requested in handlebars_template_tag:

  • generate a filename for the Handlebars files
    • input file is rendered markup
    • output file is Handlebars compiled Javascript
  • render a javascript_include_tag to tell the browser to load the output file from public/handlebars
  • check if the output file already exists in public/handlebars
  • if it exists, just return!
  • otherwise, have the Handlebars templates render to the input file (not the action response) by:
    • calling the handlebars command line with the tmp/handlebars/<input filename> and "public/handlebars/" we generated above

We need to have good logging for errors that occur in this process.

In an initializer, we should:

  • check that the handlebars command is available and produce a configuration error if not
  • create the public/handlebars subdirectory if needed
  • create the tmp/handlebars subdirectory if needed
  • delete any files from tmp/handlebars
  • delete any files from the handlebars subdirectory older than 24 hours

NOTE: we should check the Rails partials that generate the Handlebars templates, to ensure they don't contain any other markup. If there is anything else (I know of one place where there is an inline <script> tag) then we should move that markup outside the partial into the calling page.

Resources

See https://handlebarsjs.com/installation/precompilation.html#getting-started

Deployment

As as starting point, npm is available on AWS Elastic Beanstalk servers. Within the initial platform setup files, we can do a simple:

npm install --global handlebars

This will ensure that handlebars is available to the webapp user that Rails runs under.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions