Postleaf / postleaf

Simple, beautiful publishing with Node.js.
https://www.postleaf.org/
MIT License
505 stars 203 forks source link

Template caching breaks things when NODE_ENV=production #39

Closed claviska closed 7 years ago

claviska commented 7 years ago

Template caching isn't working in production because of a naming conflict with the admin and theme layout file (resolve in https://github.com/Postleaf/empower-theme/commit/04ed6bd163bf83b9272d727bb6ae71715a435e21).

Turns out, there are more issues related to this:

claviska commented 7 years ago

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 commented 7 years ago

Nice work!

claviska commented 7 years ago

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