Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Template caching breaks things when NODE_ENV=production #39

Closed
claviska opened this issue May 1, 2017 · 3 comments
Closed

Template caching breaks things when NODE_ENV=production #39

claviska opened this issue May 1, 2017 · 3 comments
Assignees
Milestone

Comments

@claviska
Copy link
Member

claviska commented May 1, 2017

Template caching isn't working in production because of a naming conflict with the admin and theme layout file (resolve in Postleaf/empower-theme@04ed6bd).

Turns out, there are more issues related to this:

  • A race condition exists when using useThemeViews() and useSystemViews(). A proper renderer needs to be used for admin and theme views to avoid this.
  • Adaro doesn't support multiple view folders (patch submitted).
  • Even with multiple view folders, Dust caching is based on the template's name instead of its location, meaning you can't switch themes without restarting the app. (Dust cache can't be cleared because dust is internal to Adaro.)
  • Using chunk.partial() to render the theme toolbar in theme_helpers.js is causing theme views to hang.
  • Need to ensure custom error pages still work once a solution is in place.
@claviska claviska added the Bug label May 1, 2017
@claviska claviska added this to the 1.0.0-alpha.4 milestone May 1, 2017
@claviska claviska self-assigned this May 1, 2017
@claviska claviska changed the title NODE_ENV=production causes issues NODE_ENV=production issues May 3, 2017
@claviska claviska changed the title NODE_ENV=production issues NODE_ENV=production caching issue May 3, 2017
@claviska claviska changed the title NODE_ENV=production caching issue Template caching breaks things when NODE_ENV=production May 5, 2017
@claviska
Copy link
Member Author

claviska commented May 6, 2017

This issue was a 👹

Due to the way Dust.js caches templates using the template's name instead of its full path, switching themes wasn't possible in production without restarting the app.

One solution I tried was patching Adaro to support multiple view folders, but again, due to the way Dust.js uses the template name instead of its full path, the problem persisted.

To solve this, I created a custom rendering engine for Postleaf that disables both Express caching and Dust.js caching in lieu of its own:

https://github.com/Postleaf/postleaf/blob/master/source/modules/dust_engine.js

I also removed the useThemeViews() and useSystemViews() methods that caused the race condition. Now, the engine will try to load templates first from the theme directory, then from the system directory. As a side effect, fallback templates (such as not_found.dust and application_error.dust) work without extra code now. As another side effect, it's now possible for a theme to use its own Zen Mode template simply by providing zen_mode.dust.

To prevent template naming conflicts and accidental overloading, admin views have been moved from source/views to source/views/admin. (It's also technically possible, although not advisable and not supported, to overload admin templates by including them in an admin folder in your theme.)

The new engine could probably be split into its own project at some point. As far as I can tell, it's the only Dust.js engine for Express that supports dynamic views and multiple view folders.

Rolling my own solution wasn't my preferred approach, but after three days of working with Adaro and other existing engines to resolve this, it became necessary. I'm happy to say that template caching now works properly in production. 😎

@lukewatts
Copy link

Nice work!

@claviska
Copy link
Member Author

claviska commented May 6, 2017

Thanks. Did you have a chance to try master yet?

# for free to subscribe to this conversation on GitHub. Already have an account? #.
Projects
None yet
Development

No branches or pull requests

2 participants