TryGhost / express-hbs

Express handlebars template engine with inheritance, partials, i18n and async helpers.
MIT License
459 stars 77 forks source link

Can't reuse a template file as a layout file #161

Open ErisDS opened 5 years ago

ErisDS commented 5 years ago

This only affects production mode where templates get cached.

Assuming you have a default.hbs file which is your base layout file.

In the case that you load and use default.hbs as a template before loading it as a layout, it will break the layout of all other pages that use the default.hbs layout. This issue will cause what appear to be intermittent layout bugs, because it depends on the order the files get used in...

If you load a page that uses default.hbs as a template first, you'll then get "Cannot read property 'match' of undefined" when trying to load the page that uses default.hbs as a layout.

If you load a page that uses default.hbs as a layout first, you'll get a blank page when trying to load any pages that use it as a template.

This is a super crazy edgecase and I don't think it necessarily needs fixing, just raising it as it caused https://github.com/TryGhost/Ghost/issues/10990 and it's useful to know if you're working with express-hbs.

There's a lot more detail about the weird behaviour in https://github.com/TryGhost/Ghost/issues/10990

ErisDS commented 4 years ago

New ref: https://forum.ghost.org/t/published-pages-result-in-500-error/13331

This has come up twice recently 🤔

ErisDS commented 4 years ago

Including the full (obscure) error that Ghost sees when this happens:

 InternalServerError: Cannot read property 'match' of undefined
        at new GhostError (/home/ghost/node_modules/@tryghost/errors/lib/errors.js:10:26)
        at _private.prepareError (/home/ghost/core/server/web/shared/middlewares/error-handler.js:53:19)
        at Layer.handle_error (/home/ghost/node_modules/express/lib/router/layer.js:71:5)
        at trim_prefix (/home/ghost/node_modules/express/lib/router/index.js:315:13)
        at /home/ghost/node_modules/express/lib/router/index.js:284:7
        at Function.process_params (/home/ghost/node_modules/express/lib/router/index.js:335:12)
        at next (/home/ghost/node_modules/express/lib/router/index.js:275:10)
        at handle404 (/home/ghost/core/frontend/apps/private-blogging/lib/middleware.js:166:20)
        at Layer.handle_error (/home/ghost/node_modules/express/lib/router/layer.js:71:5)
        at trim_prefix (/home/ghost/node_modules/express/lib/router/index.js:315:13)
        at /home/ghost/node_modules/express/lib/router/index.js:284:7
        at Function.process_params (/home/ghost/node_modules/express/lib/router/index.js:335:12)
        at next (/home/ghost/node_modules/express/lib/router/index.js:275:10)
        at Layer.handle_error (/home/ghost/node_modules/express/lib/router/layer.js:67:12)
        at trim_prefix (/home/ghost/node_modules/express/lib/router/index.js:315:13)
        at /home/ghost/node_modules/express/lib/router/index.js:284:7
        at Function.process_params (/home/ghost/node_modules/express/lib/router/index.js:335:12)
        at Immediate.next (/home/ghost/node_modules/express/lib/router/index.js:275:10)
        at Immediate._onImmediate (/home/ghost/node_modules/express/lib/router/index.js:635:15)
        at processImmediate (internal/timers.js:441:21)
        at process.topLevelDomainCallback (domain.js:131:23)

    TypeError: Cannot read property 'match' of undefined
        at ExpressHbs.declaredLayoutFile (/home/ghost/node_modules/express-hbs/lib/hbs.js:75:21)
        at parseLayout (/home/ghost/node_modules/express-hbs/lib/hbs.js:467:27)
        at /home/ghost/node_modules/express-hbs/lib/hbs.js:577:7
        at getSourceTemplate (/home/ghost/node_modules/express-hbs/lib/hbs.js:551:16)
        at compileFile (/home/ghost/node_modules/express-hbs/lib/hbs.js:573:5)
        at /home/ghost/node_modules/express-hbs/lib/hbs.js:660:12
        at ExpressHbs.loadDefaultLayout (/home/ghost/node_modules/express-hbs/lib/hbs.js:296:44)
        at ExpressHbs.___express (/home/ghost/node_modules/express-hbs/lib/hbs.js:648:8)
        at View.render (/home/ghost/node_modules/express/lib/view.js:135:8)
        at tryRender (/home/ghost/node_modules/express/lib/application.js:640:10)
        at Function.render (/home/ghost/node_modules/express/lib/application.js:592:3)
        at ServerResponse.render (/home/ghost/node_modules/express/lib/response.js:1012:7)
        at renderer (/home/ghost/core/frontend/services/routing/helpers/renderer.js:29:9)
        at renderEntry (/home/ghost/core/frontend/services/routing/helpers/render-entry.js:16:16)
        at then (/home/ghost/core/frontend/services/routing/controllers/entry.js:95:20)
        at bound (domain.js:420:14)
        at runBound (domain.js:433:12)
        at tryCatcher (/home/ghost/node_modules/bluebird/js/release/util.js:16:23)
        at Promise._settlePromiseFromHandler (/home/ghost/node_modules/bluebird/js/release/promise.js:547:31)
        at Promise._settlePromise (/home/ghost/node_modules/bluebird/js/release/promise.js:604:18)
        at Promise._settlePromise0 (/home/ghost/node_modules/bluebird/js/release/promise.js:649:10)
        at Promise._settlePromises (/home/ghost/node_modules/bluebird/js/release/promise.js:729:18)