visual-framework / vf-wp

WordPress theme and plugins for Visual Framework integration
3 stars 6 forks source link

How to update Gutenberg/Nunjucks templates after theme customisation #84

Open dbushell opened 4 years ago

dbushell commented 4 years ago

Parts of a theme that exist as PHP templates – e.g. the vf-masthead – can adapt to customisation changes because the PHP is rendered on every page load.

There is a problem with Gutenberg blocks that are rendered with Nunjucks templates.

Say we add a vf-factoid block to a page within the Gutenberg editor. The factoid.njk template is rendered as the page/post is edited. HTML is saved to the post content. When that post is visited the pre-rendered HTML is outputted.

If the theme is later customised and vf-factoid blocks require different markup (e.g. a new modifier class), we cannot update those within post content. Only when each post is edited again will they be rendered with the new template.

This is because Nunjucks is a JavaScript library and it runs client-side in the browser.

Possible solutions

  1. ❌ Find a way to run Nunjucks server-side. This is not practical without a PHP library (which doesn't exist). Running Node on the server is a big ask. Also keeping track of which post content contains out-dated templates is not trivial. Then updating all posts after each customisation change would be an expensive operation.

  2. 🤮 Update posts client-side. This would require a lot of AJAX in the WordPress admin. We cannot guarantee the process will complete, and all the same problems as above.

  3. 🤔 Do not use Nunjucks and do not save rendered HTML to the post content (only the block attributes). Instead use PHP templates to render server-side on each page load. This would require a PHP version of the component .njk templates – in vf-core or vf-wp – either way adding to the maintenance.

  4. 🤷 Ignore the issue. Assume customisation will be done once when the theme is set up and therefore changes are unlikely. Posts will require a manual edit/save to update.

Thoughts

1 and 2 are non-starters. 3 would be easy to implement (in the theme at least). I'm leaning towards 4 for now until we know just how often the issue might occur.

There may also be some blocks where the style variant is chosen independent of any global theme customisation. We need to account for this use case too.

khawkins98 commented 4 years ago

I could see us eventually getting to No. 3 (PHP templates) but it's probably not going to happen soonish given all the other things to be done.

If we go with No. 4 for now (the only realistic option?) there are few reassurance thoughts:

dbushell commented 4 years ago

Yep I think we wait and see what changes will exist.

The difficulty with rebuilding each post is the necessity to run Nunjucks templates in a JavaScript environment.

khawkins98 commented 4 years ago

The difficulty with rebuilding each post is the necessity to run Nunjucks templates in a JavaScript environment.

Ooof.